What are the reserved words in Python? Let me introduce to you a summary of Python’s 33 reserved words:
1 false
#Boolean type value, indicating false, corresponding to true
2 class
#Keywords for defining classes
##3 finally
#Exception The keyword used in processing can be used to specify the code that is always executed. The specified code is in finallyFor example:class MyException(Exception):pass try: #some code here raise MyException except MyException: print "MyException encoutered" finally: print "Arrive finally"
4 is
# Objects in Python contain three elements: id, type, valueid: used to uniquely identify an object
type: identifies the type of the object
value: Is the value of the object
is: It is used to determine whether the a object is the b object. It is judged by the id.
python video tutorial"
For example:>>> a = 1 >>> b = 1.0 >>> a is b False >>> a == b True >>> id(a) 12777000 >>> id(b) 14986000
5 return#Python function returns the value return. There must be a return value in the function to be a complete function. If you do not define a function return value in Python, then you will get a None object as a result, and None means there is no value.
def fnc1(x,y): print x+y 当函数没有显示return,默认返回none值,以下测试: >>> result = fnc1(2, 3) >>> result is None True
8 for
#The for loop can traverse any sequence of items, such as a list or a string>>> type(None)
<class 'NoneType'>
>>> None == 0
False
>>> None == ''
False
>>> None == None
True
>>> None == False
False
9 lambda
For example: exists for the definition of a single-line function.
10 try
12 def
# used to define functions. For example: for letter in 'Python': # 第一个实例
print '当前字母 :', letter
fruits = ['banana', 'apple', 'mango']
for fruit in fruits: # 第二个实例
print '当前水果 :', fruit
print "Good bye!"
13 from
14 nonlocal
For example:
g = lambda x:x+1
#The while statement repeatedly executes a block of statements. while is a type of loop statement that has an optional else clause.
16 and
#Logical judgment statement, if both the left and right sides of and are true, then the judgment result is true, otherwise both are false
17 del
#del is used for list operations to delete one or several consecutive elements.
For example: g(1)
>>>2
g(2)
>>>3
g(7)
>>>8
18 global
#Define a global scalar.
19 not
#Logical judgment, the meaning of inversion
20 with
#with is available after python2.5 Yes, it is essentially a control flow statement. with can be used to simplify the try...finally statement. Its main usage is to implement a class _enter_() and _exit_() method.
For example: >>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
>>>
>>> print filter(lambda x: x % 3 == 0, foo)
[18, 9, 24, 12, 27]
>>>
>>> print map(lambda x: x * 2 + 10, foo)
[14, 46, 28, 54, 44, 58, 26, 34, 64]
>>>
>>> print reduce(lambda x, y: x + y, foo)
21 as
# Used in conjunction with with.
22 elif
# Used with if
24 or
25 yield
#yield is used like return. Yield tells the program to require the function to return a generator
For example:
def hello() print('hello,hongten') 调用: hello() 结果: >>>hello,hongten
#Assertion is used to check the correctness of the program during operation, and has the same function as other languages.
For example:
def make_counter(): count = 0 def counter(): nonlocal count count += 1 return count return counter def make_counter_test(): mc = make_counter() print(mc()) print(mc()) print(mc())
27 else
#Use with if
28 import
#at Python uses import or from...import to import the corresponding module. For example:
from sys import * print(‘path’,path)
29 pass
#pass的意思是什么都不要做,作用是为了弥补语法和空定义上的冲突,它的好处体现在代码的编写过程之中,比如你可以先写好软件的整个框架,然后再填好框架内具体函数和class的内容,如果没有pass编译器会报一堆的错误,让整个开发很不流畅。
例如:
def f(arg): pass # a function that does nothing (yet) class C: pass # a class with no methods(yet)
30 break
#break语句是用来终止循环语句的,即使哪怕循环条件没有称为false或者序列还没有被完全递归,也会停止循环语句。提示,如果break的是for或while循环,任何对应的循环else块将不执行。
31 except
#使用try和except语句来铺货异常。
32 in
#for…in是另外一个循环语句,它在一序列的对象上递归即逐一使用队列中的每个项目。
33 raise
#raise抛出异常。
例如:
class MyException(Exception):pass try: #some code here raise MyException except MyException: print('MyException encoutered') finally: print('Arrive finally')
The above is the detailed content of What are the reserved words in Python?. For more information, please follow other related articles on the PHP Chinese website!