Python 循环语句

While循环语句

Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:

1.while 判断条件:

    执行语句……

比如:
#!/usr/bin/env python3
count = 0 while (count < 3): print('hello baby:', count) count = count + 1
print("Good bye!")

2.while 死循环
#!/usr/bin/env python3
while True: print('hello baby')


 for 循环语句

for循环在Python中有遍历所有序列的项目,如列表、元组、字典、字符串。

语法:

for iterating_var in sequence:
   statements(s)

实例1: 遍历列表

#!/usr/bin/env python3
list1 = [1,2,3,4,5,6]

for i in range(len(list1)):
  print(i)

实例2: 遍历列表
#!/usr/bin/env python3

dict = {'a':1,'b':2,'c':3,'d':4,'e':5}
for Dict in dict:
  print("%s:%s" %(Dict(dict[Dict])))

 



posted @ 2016-01-20 15:22  Rainbower  阅读(165)  评论(0)    收藏  举报