Conditional Judgment and circulation
1. Conditional judgment
If < condition judgment 1>:
< Executive 1>
Elif < condition judgment 2>:
< Executive 2>
Elif < condition judgment 3>:
< Executive 3>
Else
< Executive 4>
2. Cycle
There are two types of Python loops, one is the for...in loop and the other is the while loop.
2.1 for...in Cycle
A for x in ... Loop is a statement that puts each element into a variable and x then executes the indented block.
names = [' A ', ' B ', ' C ']
For name in Names:
Print (name)
The code prints all the elements of the names, in turn, A,b,c.
sum = 0
For x in [1,2,3,4]:
sum = sum + x
Print (sum)
The code calculates the 1+2+3+4 and promises the final result, that is, 55.
sum = 0
For x in range (5):
sum = sum + x
Print (sum)
This code is the same as the previous section of code, where range (n) refers to a sequence of integers starting from 0 and less than n .
2.2while Cycle
sum = 0
n = 1
While n < 5:
sum = SUM + N
n = n+1
Print (sum)
This code is similar to the for...in loop.
Python Learning Note 2