标签:
这一节,我们将学习Python的控制流语句,主要包括if、for、while、break、continue 和pass语句
1. If语句
x=input("please input an integer:")
if x<0:
print ‘x<0‘
elif x==0:
print ‘x=0‘
elif x>0:
print ‘x>0‘
else:
print ‘ x is not an number‘
运行结果:
>>>
please input an integer:10
x>0
2.for 循环语句
words=[‘hello‘,‘hao are you‘,10,‘ha‘]
for w in words:
print w
运行结果:
>>>
hello
hao are you
10
ha
words=[‘hello‘,‘hao are you‘,10,‘ha‘,1,2,3,4,5]
for w in words[1:6:2]:
print w
运行结果:
>>>
hao are you
ha
2
for i in range(1,10,2):
print i
运行结果:
>>>
1
3
5
7
9
3.while循环语句
a=5
while a>0:
print a
a=a-1
运行结果:
>>>
5
4
3
2
1
lists=["hello",1,2,3]
while lists:
print lists[0]
lists.pop()
运行结果:
>>>
hello
hello
hello
hello
4. break和continue语句
for i in range(5):
if i==2:
break
else:
print i
运行结果:
>>>
0
1
for i in range(5):
if i==2:
continue
else:
print i
运行结果:
>>>
0
1
3
4
5.pass语句
for i in range(4):
pass
6.补充知识点
for i in range(5):
print i
else:
print " for...else"
运行结果:
>>>
0
1
2
3
4
for...else
for i in range(5):
if i==2:
break
else:
print i
else:
print " for...else"
运行结果:
>>>
0
1
标签:
原文地址:http://www.cnblogs.com/Lival/p/4391003.html