網(wǎng)站首頁 編程語言 正文
前言
Python 有 while 語句和 for 語句作為循環(huán)處理。雖然 for 語句具有一定數(shù)量的進程,但 while 語句是『直到滿足條件』類型的循環(huán)進程。
對于無限迭代 while,循環(huán)執(zhí)行的次數(shù)沒有事先明確指定。相反,只要滿足某些條件指定的塊就會重復(fù)執(zhí)行。
使用定義迭代 for,指定塊將被執(zhí)行的次數(shù)在循環(huán)開始時已經(jīng)倍明確指定。
除了 while 語句的一般特性之外,Python 也有自己的規(guī)范,例如對 do while 語句的支持不足。循環(huán)處理是編程的基本語法。
while循環(huán)
while <布爾計算的表達式>:
? ? <執(zhí)行的python語句> # 循環(huán)體
控制表達式 ,<布爾計算的表達式> 通常涉及一個或多個變量,這些變量在開始循環(huán)之前被初始化,然后在循環(huán)體的某處可能會被修改。
當 while 遇到循環(huán)時,首先在 Boolean context 中 <布爾計算的表達式> 進行評估。
n = 5
while n > 0:
n -= 1
print(n)
輸出:
4
3
2
1
0
while 首先測試循環(huán)的控制表達式。假設(shè)開始就為假,則循環(huán)體將永遠不會被執(zhí)行。
n = 5
while n > 5:
n -= 1
print(n)
break語句 和 continue語句
while 循環(huán)的整個主體都在每次迭代中執(zhí)行,Python 提供了兩個過早終止循環(huán)迭代的關(guān)鍵字。
- break 語句立即完全終止循環(huán)。程序執(zhí)行繼續(xù)到循環(huán)體之后的第一條語句。
- continue 語句立即終止當前循環(huán)迭代。執(zhí)行跳轉(zhuǎn)到循環(huán)的頂部,并重新評估控制表達式以確定循環(huán)是再次執(zhí)行還是終止。
# break 舉例
n = 5
while n > 0:
n -= 1
if n == 2:
break
print(n)
print('循環(huán)結(jié)束。')
輸出:
4
3
循環(huán)結(jié)束。
# continue 舉例
n = 5
while n > 0:
n -= 1
if n == 2:
continue
print(n)
print('循環(huán)結(jié)束。')
輸出:
4
3
1
0
循環(huán)結(jié)束。
else 子句
Python 允許在循環(huán)else結(jié)束時使用可選子句。
while <布爾計算的表達式>:
<執(zhí)行的python語句> # 循環(huán)體
else:
<循環(huán)終止后執(zhí)行語句>
n = 5
while n > 0:
n -= 1
print(n)
else:
print('Loop done.')
輸出:
4
3
2
1
0
Loop done.
# 如果有break某些情況下就不會倍執(zhí)行
n = 5
while n > 0:
n -= 1
print(n)
if n == 2:
break
else:
print('循環(huán)結(jié)束。')
輸出:
4
3
2
無限循環(huán)
假設(shè)編寫了一個while理論上永遠不會結(jié)束的循環(huán)。
while True:
print('真·三國無雙')
真·三國無雙
真·三國無雙
.
.
.
真·三國無雙
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyboardInterrupt
這樣的循環(huán)只能手動停止。
單實際上也有它的應(yīng)用,例如循環(huán)刪除列表中的元素。
list_ = ['真·三國無雙', '真·三國無雙', '真·三國無雙']
while True:
if not list_ :
break
print(list_ .pop(-1))
輸出:
真·三國無雙
真·三國無雙
真·三國無雙
可以 break 在循環(huán)中指定多個語句。可以通過 break 從幾個不同的位置結(jié)束循環(huán),而不必在循環(huán)頭中指定所有終止條件。
while True:
? ? if <布爾計算的表達式1>: ?# 條件判斷1
? ? ? ? break
? ??
? ? if <布爾計算的表達式2>: ?# 條件判斷2
? ? ? ? break
? ??
? ? if <布爾計算的表達式3>: ?# 條件判斷3
? ? ? ? break
嵌套while循環(huán)
Python 控制結(jié)構(gòu)可以相互嵌套。
if age < 18:
if gender == 'M':
print('子供')
else:
print('娘')
elif age >= 18 and age < 65:
if gender == 'M':
print('父親')
else:
print('母親')
else:
if gender == 'M':
print('おじいさん')
else:
print('祖母')
while循環(huán)可以包含在另一個while循環(huán)中。
list_ = ['父親', '母親']
while len(list_ ):
print(list_.pop(0))
list__ = ['おじいさん', '祖母']
while len(list__ ):
print('>', list__.pop(0))
輸出:
父親
> おじいさん
> 祖母
母親
> おじいさん
> 祖母
在嵌套循環(huán)中找到的 break 語句適用于最近的封閉循環(huán)。
while <布爾計算的表達式1>:
statement
statement
while <布爾計算的表達式2>:
statement
statement
break # 適用于 while <布爾計算的表達式2>: 循環(huán)
break # 適用于 while <布爾計算的表達式1>: 循環(huán)
while循環(huán)可以嵌套在 if、elif、else 語句中。
if <布爾計算的表達式1>:
? ? <python執(zhí)行語句1>
? ? while <布爾計算的表達式2>:
? ? ? ? <python執(zhí)行語句2>
? ? ? ? <python執(zhí)行語句3>
else:
? ? while <布爾計算的表達式3>:
? ? ? ? <python執(zhí)行語句4>
? ? ? ? <python執(zhí)行語句5>
? ? <python執(zhí)行語句6>
while <布爾計算的表達式1>:
? ? if <布爾計算的表達式2>:
? ? ? ? <python執(zhí)行語句1>
? ? elif <布爾計算的表達式3>:
? ? ? ? <python執(zhí)行語句2>
? ? else:
? ? ? ? <python執(zhí)行語句3>
? ? if <布爾計算的表達式4>:
? ? ? ? <python執(zhí)行語句4>
單行 while 循環(huán)
與 if 語句一樣,while 可以在一行中指定循環(huán)。也可以用 ;組成多個循環(huán)體語句。
n = 5
while n > 0: n -= 1; print(n)
輸出:
4
3
2
1
0
兩個復(fù)合語句組合成簡寫方式是不可以的。
if True: print('data')
data
while n > 0: n -= 1; if True: print('data')
SyntaxError: invalid syntax
原文鏈接:https://blog.csdn.net/qq_20288327/article/details/124490435
相關(guān)推薦
- 2022-07-08 python?csv實時一條一條插入且表頭不重復(fù)問題_python
- 2022-10-21 C++?smart?pointer全面深入講解_C 語言
- 2022-02-28 Uncaught Error: Module parse failed: Unexpected to
- 2022-05-17 python的列表生成式,生成器和generator對象你了解嗎_python
- 2022-07-26 正則表達式規(guī)則
- 2022-05-07 Python中的集合一起來學習一下_python
- 2022-05-06 docker?save與docker?export的區(qū)別_docker
- 2022-11-14 Python實現(xiàn)腳本轉(zhuǎn)換為命令行程序_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支