Python code execution logic

1, Python statement execution logic
1. Execute sequentially from top to bottom.

2. The same statement: execute from right to left, that is, execute the right side first. A, b= b. A+ b. Equal to n; b. M= A+ b. A= n. B= M.

3. Conditional control execution:
If a:
Statement Elif b:
Statement Else:
statement.

1) Conditional control can have only one if statement. Without else, it can be executed
2) If can be nested.

4. Loop execution, while and for
1) While a:
Statement 2) The while loop uses the else statement. (I personally think that else is not very effective. Code is inherently executed from top to bottom. Without else, statements within else can still be executed.)
While count< 6:
Print (count, less than 6)
Count= Count+ 1
Else:
Print (count, greater than or equal to 6).

3) The for loop can traverse any sequence of items, such as a list or a string. The three types of data in Python, string list tuple, are sequence types.

For< Variable> In< Sequence>:
< Statements>
Else:
< Statements>

4) The break statement can break out of the loop body of for and while. If you terminate from a for or while loop, any corresponding loop else block will not be executed. The example is as follows:

for i in range(1,10) :
for j in range(1,10) :
  if i<j :
      print("%s<%s" %(i,j))
  elif i == j:
      print("%s=%s,break" %(i,j))
      break
      print("termination i==j the cycle of time, i.e i==j when, jump out of the current loop")
      print("illustrate :2== at 2 o'clock, execute break afterwards, break the following statements and j> the loop of 2 is not executed ; next cycle from i=3,j= 1 start")
  else:
      print("%s>%s" %(i,j))
  print("j")
print("i")

5) The continue statement is used to tell Python to skip the remaining statements in the current loop block and proceed to the next loop.

for i in range(1,10) :
for j in range(1,10) :
  if i<j :
      print("%s<%s" %(i,j))
  elif i == j:
      print("%s=%s,continue" %(i,j))
      continue
      print("skip the remaining statements in the current loop block and continue to the next loop")
      print("illustrate :2== at 2 o'clock, execute continue afterwards, continue the following statements are not executed ; next cycle from i=2,j= starting from 3")
  else:
      print("%s>%s" %(i,j))
  print("j")
print("i")

5. Pass statement: Python pass is an empty statement to maintain the integrity of the program structure.

6. Import will import other py files and execute the imported file during execution. If there are statements in the imported file that can be directly executed.

7. Standard modules (. py files), Python itself comes with some standard module libraries, and some modules are directly built into the parser, such as sys.

Related articles