云学编程的第13天—【微软官方python入门教程 P27-P28笔记】2021-11-13 循环Loops

本文介绍了如何使用Python的for循环遍历列表,通过range()实现固定次数的循环,以及结合while循环处理条件操作。实例涵盖了列表迭代、范围计数和输入驱动的条件循环。适合初学者理解基本的循环结构。

P27Loops

 

Loop through a collection

for name in ['christopher','susan',str(10),int(1000),float(1)]:
    print(name)

how do i then loop x number of times? The way you do that is by using a built-in little function called range. range will do for you is it will automatically create a list of numbers for you. 

Looping a number of times

for index in range(1,6):
    print(index)

 Looping with a condition

a = input('what you want to do ?')
name = ['na','bi','ss',a]
index = 1
while index < len(name):
    print(name[index]) 
    index = index + 1 #change the condition!!

 Typically you would want to use a while loop in situations where something is going to change automatically. So if I was maybe reading through a list of lines, inside of a file or someting like that, that's perfect for a while loop.

 

P28实操

people = ['Christopher','Susan']
for name in people:
    print(name)
print()
index = 0
while index < len(people):
    print(people[index])
    index = index + 1

 Python While 循环语句 | 菜鸟教程 GIF动图讲解while

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值