n = 5 while n > 0: print(n) n = n - 1 print('Blastoff!') print(n)
# 5 # 4 # 3 # 2 # 1 # Blastoff! # 0
‘break’
The ‘break’ statement ends the loop and jumps to the statement after the loop.
1 2 3 4 5 6
whileTrue: line = input('> ') if line == 'done': break print(line) print('Done!')
‘continue’
Go to the next iteration The ‘continue’ statement quits the current iteration and goes to the next iteration.
1 2 3 4 5 6 7 8 9 10 11 12
whileTrue: line = input('> ') if line[0] == '#': continue if line == 'done': break print(line) print('Done')
# > # don't print this # > done # Done
For Loop
1 2 3
for i in [5, 4, 3, 2, 1]: print(i) print('Done!')
‘in’
Like forEach
1 2 3 4
friends = ['Ann', 'Bob', 'Cathy'] for friend in friends: print('Merry Christmas', friend + '!') print('Done')
1 2 3 4 5 6 7 8 9 10 11 12 13
# 'None' keyword: absense of value # 'is': check if two objects refer to the same object # 'is not': also a logical operator # '==': check if two objects have the same value found = False min_new = None# we haven't see the value print('Before', found) for number in [2, 3, 34, 123, 100]: if min_new isNone: # see the min first time min_new = number elif min_new > number: min_new = number print('min_new:', min_new)
近期评论