Python入门笔记 循环for和while

for和while循环语句


前言

  1. while 循环 在给定的判断条件为 true 时执行循环体,否则退出循环体。

  2. for 循环 重复执行语句

  3. 嵌套循环 你可以在while循环体中嵌套for循环

  4. break 语句 在语句块执行过程中终止循环,并且跳出整个循环

  5. continue 语句 在语句块执行过程中终止当前循环,跳出该次循环,执行下一次循环。

  6. pass 语句 pass是空语句,是为了保持程序结构的完整性。


一、While

注意使用语句的

while 判断条件(condition):
    执行语句(statements)……
list = ['time', 'second','year','month','week']
index = 0

while index < len(list):
    print(list[index])
    index = index + 1

while 语句时还有另外两个重要的命令 continue,break 来跳过循环continue 用于跳过该次循环,break 则是用于退出循环,此外"判断条件"还可以是个常值,表示循环必定成立

二、for 循环

for iterating_var in sequence:
   statements(s)

下为for循环的2种方式

list = ['time', 'second','year','month','week']

# 第一种最简单的情况
# for a in list:
#     print(a) 

# 第二种用索引的方法
# for index in range(len(list)):
#     print('this is', list[index])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值