Python中的分支和循环
使用编程语言编写代码时,难免不遇到需要做逻辑判断或者循环处理事件的时候,有时还需要结合逻辑判断和循环体,在Python中也有相应的支持,和其它语言一样,也是for、if、while语句,只是用法稍微有区别,这篇文章讲解的具体内容有:
- for循环
- for_each方式
- enumerate方式
- 索引迭代方式
- for循环嵌套
- for … else用法
- break用法
- continue用法
- if条件语句
- if-elif-else方式
- if表达式中的小技巧
- while循环体
- 有限循环
- 无限循环
- while循环嵌套
- while...else用法
for循环
for_each方式
- 遍历字符串时,会把字符串中的每个字符遍历出来:
str = 'hello!'
for x in str:
print x
打印结果:
h
e
l
l
o
!
- 遍历列表时,会把列表中的每个元素遍历出来:
list = ['hello', 'world']
for x in list:
print x
打印结果:
hello
world
- 遍历字典时,会把字典中的每个key值遍历出来:
dict = {'a': 'hello', 'b': 'world'}
for x in dict:
print x
打印结果:
a
b
enumerate方式
有时候我们希望遍历一个列表时,把对应的下标也获取到,这时可以用到enumerate:
names = ['lilei', 'xiaoming', 'zhangdan']
for index, name in enumerate(names):
print (index, name)
打印结果:
(0, 'lilei')
(1, 'xiaoming')
(2, 'zhangdan')
思考:如何遍历字典中对应的key和value呢?
info = {'lilei': '170', 'xiaoming': '175', 'zhangdan': '165'}
for key, value in info.items():
print (key, value)
打印结果:
('lilei', '170')
('xiaoming', '175')
('zhangdan', '165')
索引迭代方式
遍历列表,依然可以使用取下标方式获取元素值,此时借助range()/xrange()函数:
fruits = ['banana', 'apple', 'mango']
for index in range(len(fruits)):
print fruits[index]
打印结果:
banana
apple
mango
for循环嵌套
像很多其它语言一样,Python也是支持循环体嵌套的,嵌套个数不限,这里只举嵌套两层的例子:
heights = ['170', '175', '180']
weights = ['55', '57', '60']
for height in heights:
for weight in weights:
print 'probability:' + height, weight
打印结果:
probability:170 55
probability:170 57
probability:170 60
probability:175 55
probability:175 57
probability:175 60
probability:180 55
probability:180 57
probability:180 60
for … else用法
for … else它表达了这样的功能,当for循环体正常执行完时,它执行
else里的代码块,当用break中断循环时,它不会执行
else里的代码:
a = [1, 2, 3, 4, 5]
for x in a:
if x == 3:
print '执行了break'
break
else:
print '执行了else'
打印结果:
执行了break
break用法
break代表立即中断当前循环体
,即忽略掉当前循环体后序过程,当有多层循环体嵌套时,并不影响上一层循环:
heights = ['170', '175', '180']
weights = ['55', '57', '60']
for height in heights:
for weight in weights:
if height == '175':
break
print 'probability:' + height, weight
打印结果:
probability:170 55
probability:170 57
probability:170 60
probability:180 55
probability:180 57
probability:180 60
continue用法
continue代表立即跳过当前循环体正在执行的过程
,执行当前循环体的下一个执行过程:
heights = ['170', '175', '180']
weights = ['55', '57', '60']
for height in heights:
for weight in weights:
if weight == '57':
continue
print 'probability:' + height, weight
打印结果:
probability:170 55
probability:170 60
probability:175 55
probability:175 60
probability:180 55
probability:180 60
if条件语句
if-elif-else方式
很多语言中都有if-else if-else语句,在Python中只是把else if简写成了elif,功能还是一样的,但是Python中并没有switch-case语句,切记,以下给出if-elif-else例子:
names = ['lilei', 'xiaoming', 'zhangdan']
height = 0
for name in names:
if name == 'lilei':
height = '170'
elif name == 'xiaoming':
height = '175'
elif name == 'zhangdan':
height = '180'
else:
pass
print height
pass代表什么也不做,占位用
打印结果:
170
175
180
if表达式中的小技巧
- 当if语句中需要判断多个条件时,如何写呢,可以用关键字
and
表示所有条件都满足,或者用关键字or
表示满足其中一个条件:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for x in a:
if x > 2 and x < 5:
print x
打印结果:
3
4
当然可以更简洁点写:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for x in a:
if 2< x < 5:
print x
- 如何判断一个字符串包含某个字符:
b = 'hello'
if 'o' in b:
print 'True'
else:
print 'False'
打印结果:
True
- 如何判断一个列表中包含某个元素:
a = [1, 2, 3, 4, 5, 6, 7, 8, 9]
if 1 in a:
print 'True'
else:
print ‘False’
打印结果:
True
当列表中元素为其它类型时,也是同样用法
while循环体
有限循环
打印从0到99个数:
i = 0
while i < 100:
print i
i += 1
无限循环
一直打印‘ha’,这里的1
也可以改成True
或者不为空的字符串、列表、字典
:
while 1:
print 'ha'
while循环嵌套
while循环嵌套和上面所说到的for循环嵌套类似:
i = 1
while i < 3:
j = 1
while j < 3:
print i * j
j += 1
i += 1
打印结果:
1
2
2
4
while循环体还可以和for循环体相互嵌套:
for i in range(1,3):
j = 1
while j < 3:
print i * j
j += 1
打印结果:
1
2
2
4
while...else用法
while...else用法和上面说到的for...else用法类似:
a = [1, 2, 3, 4, 5]
i = 1
while i < 5:
if i == 3:
print '执行了break'
break
i += 1
else:
print '执行了else'
打印结果:
执行了break