Python (5) Control Statement

if statement

##
Note : Indentation rules for Python code. Codes with the same indentation are regarded as code blocks .
Indentation should be written strictly in accordance with Python's habit: 4 spaces , do not use Tab, and do not mix Tab and spaces, otherwise it is easy to cause syntax errors caused by indentation.
Note : An if statement is followed by an expression and then a : to indicate the start of a block of code.

age = 20
if age >= 18:
    print 'your age is', age
    print 'adult'
print 'END'

if-else

if age >= 18:
    print 'adult'
else:
    print 'teenager'

if-elif-else

score = 85

if score>=90:
    print 'excellent'
elif score>=80:
    print 'good'
elif score>=60:
    print 'passed'
else:
    print 'failed'

Note: When the first condition is satisfied, the latter will not be executed.

for loop

L = ['Adam', 'Lisa', 'Bart']
for name in L:
    print name

Note: The variable name is defined in the for loop, which means that each element in the list is taken out in turn, and the element is assigned to name, and then the for loop body (that is, the indented code block) is executed.

L = [75, 92, 59, 68]
sum = 0.0
for score in L:
    sum=sum+score
print (sum / 4

while loop

Another type of loop that is different from the for loop is the while loop. The while loop does not iterate over the elements of a list or tuple, but instead determines whether the loop ends based on an expression.

Find the sum of odd numbers up to 100

sum = 0
x = 1
while x<100:
    sum=x+sum
    x=x+2
print sum

break

Find the sum of the first 20 terms

sum = 0
x = 1
n = 1
while True:
    if n > 20:
        break
    sum = sum + x
    x = x * 2
    n = n + 1
print sum

continue to continue the loop

Suppose we have written the code to calculate the average score using a for loop:

L = [75, 98, 59, 81, 66, 43, 69, 85]
sum = 0.0
n = 0
for x in L:
    sum = sum + x
    n = n + 1
print sum / n

Now the teacher only wants to count the average score of the passing scores (you are bad, teacher~~), so the score with x < 60 should be eliminated. At this time, using continue, it can be done that when x < 60, the loop will not continue to be executed The subsequent code of the body directly enters the next loop:

for x in L:
    if x < 60:
        continue
    sum = sum + x
    n = n + 1

only odd numbers and

sum = 0
x = 0
while True:
    x = x + 1
    if x > 100:
        break
    if x % 2 == 0:
        continue
    sum = sum + x
print sum

multiple loops

for x in ['A', 'B', 'C']:
    for y in ['1', '2', '3']:
        print x + y
x 每循环一次,y 就会循环 3 次,这样,我们可以打印出一个全排列:
A1
A2
A3
B1
B2
B3
C1
C2
C3

Exercise:
For two-digit numbers up to 100, use a double loop to print out all the tens digits that are less than the ones digit, for example, 23 (2 < 3).

for x in [ 1,2,3,4,5,6,7,8,9]:#十位
    for y in [ 0,1,2,3,4,5,6,7,8,9]:#个位
        if x<y :
            print (10*x+y) 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325733634&siteId=291194637