Python-for循环

循环语句 for语法结构:

for  target  in  sequences :

  • statement
  • statement
  • etc...

sequences的数据类型

  • list
  • tuple (元组)
  • strings
  • files 

 



循环for遍历 string 和 list

Python-for循环



循环体for遍历文件 和 元组

list 每个数据项可以修改,但是元组不可以修改

#元组
tup = (1,2,3,4,5)
for t in tup:
    print t
else:
    print 'out  tup'

遍历文件

 Python-for循环

file.read 返回的是字符串

for n in open("a.txt" , "r").readline():

  print format(n , "2d")

 Python-for循环