python 循环

for 循环

result = 'hello,world'
for i in result:
    print(i)

    
l = [1,2,3,4,5]
for i in l:
    print(i)


info = {
    'name': 'aaa',
    'age': 18,
    'job': 'it'
}
for i in info.values(): #默认循环key,加.values()循环values
    print(i)

t = (1,2,3,4,5)
for i in t:
    print(i)

for i in range(0,10,2):#2是步长
    print(i)

for i in range(10):
    if i == 7:
        continue
    print(i)

while循环(break,pass)

count = 0
while count < 10:
    count += 1  # count = count + 1
    print('ha-%s' % count)

i = 0
l = ['a', 'b', 'c', 'd','e']
while i < len(l):
    if i == 2:
        pass
    print(l[i])
    i += 1
while i < len(l):
    if i == 2:
        break
    print(l[i])
    i += 1
count = 0
while True:
    print('ha-%s' % count)

break语句

for letter in 'holleworld':     
   if letter == 'e':
      break
   print('当前字母 :', letter)

输出e之前的

continue语句

for letter in 'holleworld': 
   if letter == 'e':
      continue
   print('当前字母 :', letter)

跳过e

pass语句

for letter in 'holleworld':    
   if letter == 'e':
      pass
   print('当前字母 :', letter)
原文地址:https://www.cnblogs.com/wangxudong/p/10058590.html