Control the sequence of code execution
- Sequential structure : The code is executed from top to bottom , Each statement is executed only once ( By default )
- Branching structure : Choose to execute or not execute part of the code according to the conditions ( Use
if
sentence )- Loop structure : Let the code execute repeatedly (
for
perhapswhile
)
successively
perform if
Single branch structure :
If … Just ….
# Grammatical structure
if Conditional statements :
Code segment
explain :if
: keyword , Fixed writing Conditional expression
: You can any expression that has a result :
: Fixed writing Code segment
: Structurally, it is and if
One or more statements that hold an indent ( At least one ); Logically, it is the code that will be executed only after the conditional statement is satisfied
if
Two branch structure :
If … Just … otherwise …
# Grammatical structure
if Conditional statements :
Code segment 1 ( Code that meets the conditions to execute )
else:
Code segment 2 ( Code that does not meet the conditions to execute )
if
Multi branch structure :
If … Just … If … Just … … otherwise …
# Grammatical structure
if Conditional statements 1:
Code segment 1 ( Meet the conditions 1 Executed code )
elif Conditional statements 2:
Code block 2 ( Meet the conditions 2 Executed code )
...
else:
Code segment n ( Code executed without any conditions )
elif It could be any number ;else There can be or not .
for
loop
# Grammatical structure
for Variable in Sequence :
The loop body
explain :for
: keyword ;, Fixed writing Variable
: Valid variable name ( Can be defined , It can also be undefined )in
: keyword ; Fixed writing Sequence
; Data of container data type , Container data structures include : character string 、 list 、 Dictionaries 、 aggregate 、 Tuples 、 iterator 、 generator 、range etc. :
: Fixed writing The loop body
: and for One or more statements that hold an indent ; A loop body is a block of code that needs to be executed repeatedly
Execution process : Let the variable go out of the sequence , One by one , Until it's done ; Taking a value will execute the loop body once
·for loop · The number of cycles is related to the number of elements in the sequence
range() function
: To generate a sequence of equal differences ( Integers ).
for i in range(3)
print(i) # 1, 2, 3
range(n)
: produce [0——n]
Equal difference sequence of , The difference is 1
range(m,n)
: produce [m——n)
Equal difference sequence of , The difference is 1
range(m,n, setp)
: produce [m——n)
Equal difference sequence of , The difference is step
for
Basic application of circulation
Cumulative sum
It's usually 0 or 1
.Number of Statistics
The default is 0
+1
describe The computation of m
about Python I believe many pe
This blog post is used to reco