A conditional judgment
if < condition judgment 1>: < execute 1>elif < condition 2> :< execute 2>elif < condition judgment 3>: < execute 3>else: < Execute 4>
ifThe judging conditions can also be abbreviated, such as writing:
if x: print(‘True‘)
As long as a non- x 0 value, a non-empty string, a non-empty list, etc., it is judged True , otherwise False .
Two cycles
For...in cycle
# List of loops names = ['Michael'Bob'Tracy ' for in names: print(name) # for-in range (5): print(i)
While loop
While loop, as long as the condition is satisfied, it keeps looping and exits the loop when the condition is not satisfied. For example, we want to calculate the sum of all the odd numbers within 100, which can be implemented with a while loop:
sum == while n > 0: = sum + n = n-2print(sum)
Continue
Jump out of this loop and execute the next loop.
Break
Jump out of the entire loop body.
Pay special attention to not abusing break and continue statements. breakand continue will cause the code to perform logical fork too much, error prone.
Python note four (conditional judgment/cyclic/break and Continue)