Python入门学习六:python循环语句

本帖主要分析python中的循环语句:

循环语句

Python 提供了 for 循环和 while 循环(在 Python 中没有 do…while 循环):

循环类型描述
while 循环在给定的判断条件为 true 时执行循环体,否则退出循环体。
for 循环重复执行语句
嵌套循环你可以在while循环体中嵌套for循环

循环控制语句

循环控制语句可以更改语句执行的顺序。Python支持以下循环控制语句:

控制语句描述
break 语句在语句块执行过程中终止循环,并且跳出整个循环
continue 语句在语句块执行过程中终止当前循环,跳出该次循环,执行下一次循环。
pass 语句pass是空语句,是为了保持程序结构的完整性。

1 Python While 循环语句

Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:

1.1 while语句

while 判断条件(condition):
    执行语句(statements)……

例1 :

a = 1
while a < 10:
    print(a)
    a+=2#每次循环后执行加2操作
    #打印结果
    1
    3
    5
    7
    9

例2:

numbers = [12, 37, 5, 42, 8, 3]
even = []
odd = []
while len(numbers) > 0:
    number = numbers.pop()#从numbers列表末尾开始访问
    if(number % 2 == 0):#除2求余
        even.append(number)#如果余数为0,则为偶数
    else:
        odd.append(number)#否则就为奇数
print(even)
print(odd)
#打印结果
[8, 42, 12]
[3, 5, 37]

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

1.2 python break 和 continue 语句

break语句用来终止循环语句,即循环条件没有False条件或者序列还没被完全递归完,也会停止执行循环语句。
break语句用在while和for循环中。
如果您使用嵌套循环,break语句将停止执行最深层的循环,并开始执行下一行代码。

Python continue 语句跳出本次循环,而break跳出整个循环。
continue 语句用来告诉Python跳过当前循环的剩余语句,然后继续进行下一轮循环。
continue语句用在while和for循环中。
例3:

i = 1
while i < 10:   
    i += 1
    if i%2 > 0:     # 非双数时跳过输出
        continue
    print(i)         # 输出双数2、4、6、8、10
 
i = 1
while 1:            # 循环条件为1必定成立
    print(i)        # 输出1~10
    i += 1
    if i > 10:     # 当i大于10时跳出循环
        break      # 输出1 2 3 4 5 6 7 8 9 10

1.3 无限循环

如果条件判断语句永远为 true,循环将会无限的执行下去:

var = 1
while var == 1 :  # 该条件永远为true,循环将无限执行下去
    num = input("Enter a number  :")
    print ("You entered: ", num)
print ("Good bye!")
#打印结果
Enter a number  :34
You entered:  34
Enter a number  :56
You entered:  56
Enter a number  :67
You entered:  67
Enter a number  :668
You entered:  668
Enter a number  :#无限循环下去

1.4 循环使用 else 语句

在 python 中,while … else 在循环条件为 false 时执行 else 语句块:

count = 0
while count < 5:
    print(count, " is  less than 5")
    count = count + 1
else:
    print(count, " is not less than 5")
    #打印结果
    0  is  less than 5
    1  is  less than 5
    2  is  less than 5
    3  is  less than 5
    4  is  less than 5
    5  is not less than 5

2 Python for循环语句

for 循环语句格式:

for iterating_var in sequence:
   statements(s)

详情请见我的博客:
https://blog.csdn.net/qq_40074819/article/details/104208269

3 Python 循环嵌套

Python for 循环嵌套语法:

for iterating_var in sequence:
   for iterating_var in sequence:
      statements(s)
   statements(s)

Python while 循环嵌套语法:

while expression:
   while expression:
      statement(s)
   statement(s)

同时还可以在循环体内嵌入其他的循环体,如在while循环中可以嵌入for循环, 反之,你可以在for循环中嵌入while循环。

4 Python pass 语句

Python pass 是空语句,是为了保持程序结构的完整性。
pass 不做任何事情,一般用做占位语句。
Python 语言 pass 语句语法格式如下:

# 输出 Python 的每个字母
for letter in 'Python':
    if letter == 'h':
        pass
        print ('这是 pass 块')
    print ('当前字母 :', letter)
print("Good bye!")
#打印结果
当前字母 : P
当前字母 : y
当前字母 : t
这是 pass 块
当前字母 : h
当前字母 : o
当前字母 : n
Good bye!

参考博客:https://blog.csdn.net/qq_40074819/article/details/104208269
参考链接:https://www.runoob.com/python/python-pass-statement.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值