python3之四条语句(2)for循环

for循环

for循环的作用:用来遍历可迭代对象的数据元素
什么是可迭代对象:就是指可以被依次获取数据元素的对象
可迭代对象如:字符串,列表,元组,集合等
获取可迭代对象的元素一般使用for循环
for循环的语法:

for 变量列表 in 可迭代对象:
    语句块1
else:
    语句块2

说明:
else不是必须的

举个简单的小栗子

>>> s = [1, 2, 3, 4]
>>> for i in s:
    print(i, end=" ")

1 2 3 4 
>>> for i in range(5):
    print(i, end=' ')

0 1 2 3 4 

当然现在只是一个变量,也可以多个,这个以后会说,现在只放个栗子吧

numbers = [10086, 10010, 10000, 95588]
names = ['中国移动', '中国联通', '中国电信']

for No, number, name in zip(range(1, 100), numbers, names):
    print('序号', No, name, '的客服电话是:', number)

打印结果是:

序号 1 中国移动 的客服电话是: 10086
序号 2 中国联通 的客服电话是: 10010
序号 3 中国电信 的客服电话是: 10000

猜你喜欢

转载自blog.csdn.net/geek_xiong/article/details/82053565
今日推荐