Python中的关键字

 

 

Python关键字python编程语言的保留字。这些关键字不能用于其他目的。

Python中有35个关键字-下面列出了它们的用法。

KeywordDescription
andlogical AND operator. Return True if both statements are True.

 

= (5 3 and 5 10)

print(x)    # True

orlogical OR operator. Returns True if either of two statements is true. If both statements are false, the returns False.

 

= (5 3 or 5 10)

print(x)    # True

asIt is used to create an alias.

 

import calendar as c

print(c.month_name[1])  #January

assertIt can be used for debugging the code. It tests a condition and returns True , if not, the program will raise an AssertionError.

 

= "hello"

 

assert == "goodbye""x should be 'hello'"  # AssertionError

asyncIt is used to declare a function as a coroutine, much like what the @asyncio.coroutine decorator does.

 

async def ping_server(ip):

awaitIt is used to call async coroutine.

 

async def ping_local():

    return await ping_server('192.168.1.1')

classIt is used to create a class.

 

class User:

  name = "John"

  age = 36

defIt is used to create or define a function.

 

def my_function():

  print("Hello world !!")

 

my_function()

delIt is used to delete objects. In Python everything is an object, so the del keyword can also be used to delete variables, lists, or parts of a list, etc.

 

= "hello"

 

del x

ifIt is used to create conditional statements that allows us to execute a block of code only if a condition is True.

 

= 5

 

if x > 3:

  print("it is true")

elifIt is used in conditional statements and is short for else if.

 

= 5

 

if i > 0:

    print("Positive")

elif == 0:

    print("ZERO")

else:

    print("Negative")

elseIt decides what to do if the condition is False in if..else statement.

 

= 5

 

if i > 0:

    print("Positive")

else:

    print("Negative")

It can also be use in try...except blocks.

= 5

 

try:

    x > 10

except:

    print("Something went wrong")

else:

    print("Normally execute the code")

tryIt defines a block of code ot test if it contains any errors.
exceptIt defines a block of code to run if the try block raises an error.

 

try:

    x > 3

except:

    print("Something went wrong")

finallyIt defines a code block which will be executed no matter if the try block raises an error or not.

 

try:

    x > 3

except:

    print("Something went wrong")

finally:

     print("I will always get executed")

raiseIt is used to raise an exception, manually.

 

= "hello"

 

if not type(x) is int:

    raise TypeError("Only integers are allowed")

FalseIt is a Boolean value and same as 0.
TrueIt is a Boolean value and same as 1.
forIt is used to create a for loop. A for loop can be used to iterate through a sequence, like a list, tuple, etc.

 

for in range(19):

    print(x)

whileIt is used to create a while loop. The loop continues until the conditional statement is false.

 

= 0

 

while x < 9:

    print(x)

    = + 1

breakIt is used to break out a for loop, or a while loop.

 

= 1

 

while i < 9:

    print(i)

    if == 3:

        break

    += 1

continueIt is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.

 

for in range(9):

    if == 3:

        continue

    print(i)

importIt is used to import modules.

 

import datetime

fromIt is used to import only a specified section from a module.

 

from datetime import time

globalIt is used to create global variables from a no-global scope, e.g. inside a function.

 

def myfunction():

    global x

    = "hello"

in1. It is used to check if a value is present in a sequence (list, range, string etc.).
2. It is also used to iterate through a sequence in a for loop.

 

fruits = ["apple""banana""cherry"]

 

if "banana" in fruits:

    print("yes")

 

for in fruits:

    print(x)

isIt is used to test if two variables refer to the same object.

 

= ["apple""banana""cherry"]

= ["apple""banana""cherry"]

= a

 

print(a is b)   # False

print(a is c)   # True

lambdaIt is used to create small anonymous functions. They can take any number of arguments, but can only have one expression.

 

= lambda a, b, c : a + + c

 

print(x(562))

NoneIt is used to define a null value, or no value at all. None is not the same as 0, False, or an empty string.
None is a datatype of its own (NoneType) and only None can be None.

 

= None

 

if x:

  print("Do you think None is True")

else:

  print("None is not True...")      # Prints this statement

nonlocalIt is used to declare that a variable is not local. It is used to work with variables inside nested functions, where the variable should not belong to the inner function.

 

def myfunc1():

    = "John"

    def myfunc2():

        nonlocal x

        = "hello"

    myfunc2()

    return x

 

print(myfunc1())

notIt is a logical operator and reverses the value of True or False.

 

= False

 

print(not x)    # True

passIt is used as a placeholder for future code. When the pass statement is executed, nothing happens, but you avoid getting an error when an empty code is not allowed.

 

Empty code is not allowed in loops, function definitions, class definitions, or in if statements.

for in [012]:

            pass

returnIt is to exit a function and return a value.

 

def myfunction():

            return 3+3

withUsed to simplify exception handling
yieldTo end a function, returns a generator

学习愉快!

 

  • 5
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

量慧实工作室LHStudio

LHStudio感谢您的支持

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值