while语句,提供了编写通用循环的一种方法,而for语句是用来遍历序列对象内的元素,并对每个元素运行一个代码块。break,continue用在循环内,跳出整个循环或者跳出一次循环。
 一、while循环
 1、一般格式
 格式:首行以及测试表达式,有一列或多列缩进语句的主体以及一个选用的else部分(控制权离开循环时而没有碰到break语句时会执行)
 python会一直计算开投的测试,然后执行循环主体内的语句,直到测试返回假值为止。
 while <test>:
     <statements1>
 else:
     <statements2>
 2、例子
 >>> while True:
 ...  print "Type Ctrl+C to stop!"
 
 >>> while x:    
 ...     print x,
 ...     x=x[1:]
 ... 
 diege iege ege ge e
 注意 print末尾的逗号,会使所有输出都出现在同一行。
 >>> a,b=0,10 
 >>> while a<b:
 ...     print a,
 ...     a+=1
 ... 
 0 1 2 3 4 5 6 7 8 9
 Python并没有其他语言中所谓的"do until”循环语句,不过我们可以在循环主体底部以一个测试和break来实现类似的功能。
 while    True:
     do something
     if exitTest():break
 3、对比shell的while语句
 while 命令
 do
     命令1
     命令2
 done
 在系统管理时常用与逐行读取一个文件并处理。
 while read line
 do
         echo $line
 done < /etc/rc.conf
 shell中还有一个类似while的循环until
 until 条件
 do
         命令1
         命令2
 done 
 EG:
 IS_ROOT=`who |grep root`
 until [ "$IS_ROOT" ]
 do
         echo 'root online'
         sleep 2
 done             
 
 二、for循环
 for循环在Python中是一个通用的序列迭代器:可以遍历任何有序的序列对象内的元素。for语句可用于字符串,列表,元组,其他内置可迭代对象以及之后我们能够通过类所创建的新对象。
 1、一般格式
 Python for循环的首行定义了一个赋值目标(或【一些目标】),以及你想遍历的对象,首行后面是你想重复的语句块(一般都有缩进)
 for <target> in <object>:
     <statements>
 else:
     <statements>
 当ptyhon运行for循环时,会逐个将序列对象中的元素赋值给目标,然后为每个元素执行循环主体。循环主体一般使用赋值的目标来引用序列中当前的元素,就好像那事遍历序列的游标。
 
 for首行中用作赋值目标的变量名通常是for语句所在作用于的变量(可能是新的)。这个变量名没有什么特别的,甚至可以在循环主体中修改。但是当控制权再次回到循环顶端时,就会自动被设成序列的下一个元素。循环之后,这个变量一般都还是引用了最近所用过的元素,也就是序列中最后的元素,除非通过一个break语句退出了循环。
 
 for语句也支持一个选用的else块,它的工作就像在while循环中一样:如果循环离开时没有碰到break语句,就会执行(也就是序列所有元素都被访问过了)
 break和continue语句也可用在for循环中,就像while循环那样。for循环完整的格式如下:
 for <target> in <object>:
     <statements>
     if <test>:break
     if <test>:conitnue
 else:
     <statements>
 
 对比shell的for循环
 for  变量名 in 列表
 do
     命令1
     命令1
 done
 
 for i in 1 2 3 
 do 
         echo $i 
 don
 
 for i in `ls -1 /root`
 do 
         echo $i 
 done
 
 2、例子
 1)基本应用
 >>> x=[1,2,3,4,5]
 >>> for i in x:
 ...     print i
 ... 
 1
 2
 3
 4
 5
 >>> for i in x:  
 ...     if i==3:break
 ...     print i
 ... 
 1
 2
 >>> for i in x:      
 ...     print i
 ...     if i==3:break
 ... 
 1
 2
 3
 注意if语句的位置
 > D={'name':['diege','lily','kelly'],'class':2012,'number':48}
 >>> for i in D:     
 ...     print i,'=>',D[i]  
 ... 
 number => 48
 name => ['diege', 'lily', 'kelly']
 class => 2012
 多层
 >>> for i in D:                      
 ...     if type(D[i])==list:         
 ...             for l in D[i]:       
 ...                     print l      
 ...     else:
 ...             print  D[i]
 ... 
 48
 diege
 lily
 kelly
 2012
 for元组赋值
 首行定义了一个赋值【一些目标】
 >>> T=[(1,2),(3,4),(5,6)]
 >>> for (a,b) in T:
 ...     print a,b
 ... 
 1 2
 3 4
 5 6
 
 for循环嵌套
 遍历一个字典(或列表)包括字典,列表,元组的的语句
 D={'game':'sjgame','version':[1.0,1.1,1.2,1.3],'useid':(1000,1001,1002,1003,1004),'character':{'WS':'wushi','FS':'fashi','MS':'moshi
 '},'CP':'ice'}
 
 for i in D:
         if type(D[i])==list:
                 for l in D[i]:
                         print l
         elif type(D[i])==tuple:
                 for t in D[i]:
                         print t
         elif type(D[i])==dict:
                  for d in D[i]:
                         print d
         else:
                 print   D[i]
 代替break的
 >>> items=['diege',999,(3,7),1.3]
 >>> tests=[(3,7),3.14]
 >>> for key in tests: 
 ...     for item in items:             
 ...             if key==item:      
 ...                     print key,'was found'
 ...                     break
 ...     else:
 ...             print key,'not found!' 
 ... 
 (3, 7) was found
 3.14 not found
 有点类似查找的功能。
 收集两个序列中相同元素
 >>> seq1='diege'
 >>> seq2='decgl'
 >>> res=[]                  
 >>> for x in seq1:          
 ...     if x in seq2:          
 ...             res.append(x)
 ... 
 >>> 
 >>> res
 ['d', 'e', 'g', 'e']
 准确的说是显示seq1和seq2都用的在seq1一次顺序。
 
 3、为什么要在意“文件扫描”
 循环读取文件:while在文件结尾时使用break
 >>> fl=open('/etc/rc.conf') 
 >>> while True:
 ...     char=fl.read(1)
 ...     if not char:break
 ...     print char,
 MS for循环打印出来的字体正常点,呵呵。
 >>> for char in open('/etc/rc.conf'):for char in open('/etc/rc.conf').read():的缩写
 ...     print char
 ... 
 
 使用while按行处理
 >>> fl=open('/etc/rc.conf')
 >>> while True:             
 ...     line=fl.readline()
 ...     if not line:break
 ...     print line,
 ... 
 按行读取文件文件时,for循环是最容易编写及执行最快的选择
 >>> for line in open('/etc/rc.conf'):#默认read()
 ...     print line
 ... 
 >>> for line in open('/etc/rc.conf').readlines():
 ...     print line
 ... 
 readlines会一次把文件载入到行字符串的列表
 >>> for line in open('/etc/rc.conf').xreadlines():
 ...     print line
 ... 
 xreadlines则是按需求加载文字列,从而避免大型文件导致内存溢出。
 每个步骤所读取的数据越多,程序会员越快。