search
HomeBackend DevelopmentPython TutorialWhat are the reserved words in Python?

What are the reserved words in Python?

Jun 12, 2019 pm 01:54 PM
pythonreserved words

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"

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

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
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

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!"

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


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

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

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

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())

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

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!

Statement
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
Python and Time: Making the Most of Your Study TimePython and Time: Making the Most of Your Study TimeApr 14, 2025 am 12:02 AM

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python: Games, GUIs, and MorePython: Games, GUIs, and MoreApr 13, 2025 am 12:14 AM

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python vs. C  : Applications and Use Cases ComparedPython vs. C : Applications and Use Cases ComparedApr 12, 2025 am 12:01 AM

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

The 2-Hour Python Plan: A Realistic ApproachThe 2-Hour Python Plan: A Realistic ApproachApr 11, 2025 am 12:04 AM

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python: Exploring Its Primary ApplicationsPython: Exploring Its Primary ApplicationsApr 10, 2025 am 09:41 AM

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

How Much Python Can You Learn in 2 Hours?How Much Python Can You Learn in 2 Hours?Apr 09, 2025 pm 04:33 PM

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics in project and problem-driven methods within 10 hours?How to teach computer novice programming basics in project and problem-driven methods within 10 hours?Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading?Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function