|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
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
break continue pass和循环的else
break
跳出最近所在的循环(跳出整个循环语句)
continue
跳到最近所在循环的开头处(来到循环的首行,跳过本次循环)
pass
什么事也不做,只是空占位语句
循环else块
只有当循环正常离开时才会执行(也就是没有碰到break语句)
1、一般循环格式
加入break和continue语句后,while循环的一般格式如下:
while <test>:
<statements1>
if <test2>:break
if <test3>:continue
if <test4>:pass
else:
<statements2>
break和continue可以出现在while(或for)循环主体的任何地方,但通常会进一步嵌套在if语句中,根据某些条件来采取对应的操作。
2、列子
pass
>>> while 1:pass
...
pass可用于空类,有时有指的是"以后会填上”,只是暂时用于填充函数主体而已:
>>> def func1():
... pass
continue
continue语句会立即跳到循环的顶端,开始下一次循环。
>>> while x:
... x=x-1
... if x%2!=0:continue
... print x,
...
8 6 4 2 0
这个例子中,如果是奇数就返回循环顶部,不会打印.是偶数就打印。
这个下面这个结果一样
>>> while x:
... x=x-1
... if x%2==0:
... print x,
...
8 6 4 2 0
注意这两个例子的print位置,第一个print是属于while块的,测试不通过下执行,测试通过就回到循环顶端,第二个是属于if块的,只有测试通过才打印
>>> while x:
... x=x-1
... if x%2==0:
... print x,
...break
break语句会立刻离开循环。碰到break,位于其后的循环代码都不会执行。
while 1:
name=raw_input("Enter name:")
if name=='stop':break
age=raw_input("Enter age:")
print 'Hello',name,'=>',int(age)**2
当age不输入任何值时会报错。可以使用try解决
while 1:
name=raw_input("Enter name:")
if name=='stop':break
age=raw_input("Enter age:")
try:
print 'Hello',name,'=>',int(age)**2
except:
print 'Please input age!'
else 只有当循环正常离开时才会执行(也就是没有碰到break语句【break的条件不满足】)
>>> while x:
... x=x-1
... print x
... else:
... print "over"
...
9
8
7
6
5
4
3
2
1
0
over
>>> x=10
>>> while x:
... x=x-1
... if x==5:break
... print x
... else:
... print "over"
...
9
8
7
6
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一次顺序。 |
评分
-
查看全部评分
|