Python is similar to Java's control and looping statements with if, while, and for statements
If statement
If condition:
An expression
Elif Conditions:
An expression
Else
An expression
While statement
While judging condition:
EXECUTE statement
While judging condition:
EXECUTE statement
Else
EXECUTE statement
The While...else statement is primarily used to determine if a block of code is running, for example if the while condition is false, and the Else statement is executed if the while condition is true;
For statement
Just like Java, you don't have to specify the I type and the initialization of I
For <variable> in <sequence>: <statements>else: <statements> # Simple instance for I in range: print (i)
Describe the range () function, which is often used
Range (Start,stop,step)
Start is the starting position, stop is the end, step is step, default is 1
>>>list (Range (0, 5)) [0, 5, ten,, 25]>>> list (range (0,, 2)) [0, 2, 4, 6, 8]>>> list ( Range (0, -10,-1)) [0,-1,-2, -3, -4, -5,-6,-7,-8, -9]>>> list (range (1, 0)) []>>>[x * x for x in range (1,10,2)] [1,9,25,49,81]
Using break and continue to control flow in loops
Break is encountered in the loop, the statement can exit the loop in advance
Encounter continue in the loop, skip the current cycle and start the next cycle directly
Beginner python essay--Control and looping statements