Python code execution sequence, you will understand after reading it

Article directory

Preface

1. When executing Python code, follow the following principles

2. Basic classification of operators and operation rules

Summarize



Preface

If you are not very familiar with the order of code execution, you can take a closer look at this article. If you are a newbie, reading this article will be of great help to your growth.

This article tells you in detail the execution order of code in Python.

1. When executing Python code, follow the following principles

1. Ordinary statements are executed line by line, top to bottom. When encountering an assignment statement, proceed from right to left.

Case:

str='我爱中华人民共和国'  #第一行
print(str)             #第二行

#输出: 我爱中华人民共和国

Execution order: Execute the first line first, which is the assignment statement, and execute it from right to left; after execution is completed, execute the second line.

2. The execution process of the function is from top to bottom. The function name is executed first, and the inside of the function is not executed. It is executed only when the function is called.

Case:

def hanshu1():                       #第一行
    print('我爱中华人民共和国!')       #第二行
print('函数的执行顺序!')              #第三行
def hanshu2():                       #第四行
    print('我爱写代码!')              #第五行
hanshu2()                            #第六行
print('好好工作,好好学习!')           #第七行
hanshu1()                            #第八行

#---输出:---
# 函数的执行顺序!
# 我爱写代码!
# 好好工作,好好学习!
# 我爱中华人民共和国!

 Execution sequence: Execute the first line → Execute the third line → Execute the fourth line → Execute the sixth line → Execute the fifth line → Execute the seventh line → Execute the eighth line → Execute the second line.

3. The execution of a class is also from top to bottom, but there are many magic methods and ordinary methods in the class, but the order is to execute __new__ first, then __init__, then the ordinary methods, and finally __del__ 

Case:

class People:                  #第一行
    print('开始')              #第二行
    def run(self):            #第三行
        print('跑')            #第四行
    def __init__(self):       #第五行
        print('init')         #第六行
    def eat(self):             #第七行
        print('吃')            #第八行

print('吃西瓜!')               #第九行
one = People()                 #第十行
one.run()                      #第十一行


#---输出----
# 开始
# 吃西瓜!
# init
# 跑

Execution order: Line 1 → Line 2 → Line 3 → Line 5 → Line 7 → Line 9 → Line 10 → Line 11 → Line 6 → Line 4

2. Basic classification of operators and operation rules

Here I will list common operators, which are roughly divided into arithmetic operators (such as addition, subtraction, multiplication and division), relational operators (such as greater than, less than or equal to), and logical operators (AND, NOT, or). The operation rules of operators are: those with higher priority are executed first, those with higher priority are executed first, and those with lower priority are executed later. Operations with the same priority are executed from left to right. If there are parentheses, the operations within the parentheses Executed first.

Level (decreasing from top to bottom) symbol type specific symbols
1 arithmetic mark ** (power operation, returns the power of a number)
2 * (multiplication), / (division), % (remainder), // (division)
3 + (plus), - (subtract)
4 relational symbol <, <=, >, >=, == (equal sign), != (not equal)
5 Logic operator not (not)
6 and (with)
7 or(或)

 Case:

print(3 != 5 or 4 < 2)
#输出: True

 order:! = and < symbols have higher priority than or, so 3!=5 and 4<2 are calculated first, and the results are True and False respectively. Finally, if Ture or False, the final operation result is True.

Case:

print(5 == 5 and 7 < 12)
#输出: True

Order: == and < have higher priority than and, so 5==5 and 7<12 are calculated first, the results are both True, and finally the result of True and True is True.


Summarize


The above is a summary of the order of code execution in Python. Thank you for reading. I hope it will be helpful to you.

Guess you like

Origin blog.csdn.net/weixin_44793743/article/details/126336444