Home > Backend Development > Python Tutorial > What are the reserved words in Python?

What are the reserved words in Python?

爱喝马黛茶的安东尼
Release: 2019-06-20 09:42:16
Original
105529 people have browsed it

What are the reserved words in Python?

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 finally

For example:    

class MyException(Exception):pass      
try:        
    #some code here        
    raise MyException      
except MyException:        
    print "MyException encoutered"      
finally:        
    print "Arrive finally"
Copy after login

4 is 

# Objects in Python contain three elements: id, type, value


Among them:

id: 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.

==: It determines whether the value of the a object is equal to the value of the b object. It is judged by value

Related recommendations: "

python video tutorial"

For example:   

>>> a = 1      
>>> b = 1.0      
>>> a is b      
False      
>>> a == b
True
>>> id(a)
12777000
>>> id(b)
14986000
Copy after login

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.

For example: None is not the empty string. Comparing None with any other data type always returns False. None has its own data type NoneType. We can copy None to any variable, but we cannot create other NoneType objects.

For example:    

    def fnc1(x,y):         
print x+y      
    当函数没有显示return,默认返回none值,以下测试:
      >>> result = fnc1(2, 3)        
    >>> result is None        
    True
Copy after login
7 continue 

#The continue statement is used to tell Python to skip the remaining statements in the current loop block and continue. Next cycle.

8 for

#The for loop can traverse any sequence of items, such as a list or a string


For example:

>>> type(None)  
<class &#39;NoneType&#39;>  
>>> None == 0  
False  
>>> None == &#39;&#39;  
False  
>>> None == None  
True  
>>> None == False  
False
Copy after login

9 lambda

#Anonymous function is a very fashionable concept, which improves the simplicity of the code.


For example: ​​exists for the definition of a single-line function.

10 try

#Programmers can use the try...except statement to handle exceptions. Put the normal statement blocks in the try block, and put the error handling statements in the except block.

11 true

#A Boolean type value, indicating true, which is the opposite of false.

12 def

# used to define functions. For example:

for letter in &#39;Python&#39;: # 第一个实例       
print &#39;当前字母 :&#39;, letter       

fruits = [&#39;banana&#39;, &#39;apple&#39;, &#39;mango&#39;]       
for fruit in fruits: # 第二个实例       
print &#39;当前水果 :&#39;, fruit       
print "Good bye!"
Copy after login

13 from

#Use import in python Or from...import to import the corresponding module.

14 nonlocal

#The nonlocal keyword is used to use outer (non-global) variables in functions or other scopes.  

For example:   

g = lambda x:x+1
Copy after login


15 while

#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
Copy after login

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)
Copy after login

21 as

# Used in conjunction with with.
22 elif

# Used with if

23 if

#if statement is used to test an Condition, if the condition is true, we run a block of statements (called if... block), otherwise we process another block of statements (called else... block). The else clause is optional.

24 or

#Logical judgment, if one of the two sides of or is true, the judgment result is true.

25 yield

#yield is used like return. Yield tells the program to require the function to return a generator

For example:

def hello()        
    print(&#39;hello,hongten&#39;)      
调用:      
hello()      
结果:      
>>>hello,hongten
Copy after login

26 assert

#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())
Copy after login

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)
Copy after login

29 pass
#pass的意思是什么都不要做,作用是为了弥补语法和空定义上的冲突,它的好处体现在代码的编写过程之中,比如你可以先写好软件的整个框架,然后再填好框架内具体函数和class的内容,如果没有pass编译器会报一堆的错误,让整个开发很不流畅。
例如:      

def f(arg): pass # a function that does nothing (yet)      
class C: pass    # a class with no methods(yet)
Copy after login

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(&#39;MyException encoutered&#39;)      
finally:        
    print(&#39;Arrive finally&#39;)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template