for循环
python中的for循环可以遍历任何序列的项目,如一个列表或者一个字符串
for 变量 in range(起始,终止 ,步长):
循环内容
嵌套循环
起始
例如:for letter in ‘Python’: # 第一个实例
print ‘当前字母 :’, letter
fruits = [‘banana’, ‘apple’, ‘mango’]
for fruit in fruits: # 第二个实例
print ‘当前水果 :’, fruit
print “Good bye!”
输出结果:当前字母 : P
当前字母 : y
当前字母 : t
当前字母 : h
当前字母 : o
当前字母 : n
当前水果 : banana
当前水果 : apple
当前水果 : mango
Good bye!
while循环:
while 终止表达式:
循环体
步长
while与for循环一般情况下课相互替换break:跳出循环 continue:结束当次循环
例如:count = 0
while (count < 9):
print ‘The count is:’, count
count = count + 1
print “Good bye!”
输出结果:The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!