網站首頁 編程語言 正文
前言
Python 有 while 語句和 for 語句作為循環處理。雖然 for 語句具有一定數量的進程,但 while 語句是『直到滿足條件』類型的循環進程。
對于無限迭代 while,循環執行的次數沒有事先明確指定。相反,只要滿足某些條件指定的塊就會重復執行。
使用定義迭代 for,指定塊將被執行的次數在循環開始時已經倍明確指定。
除了 while 語句的一般特性之外,Python 也有自己的規范,例如對 do while 語句的支持不足。循環處理是編程的基本語法。
while循環
while <布爾計算的表達式>:
? ? <執行的python語句> # 循環體
控制表達式 ,<布爾計算的表達式> 通常涉及一個或多個變量,這些變量在開始循環之前被初始化,然后在循環體的某處可能會被修改。
當 while 遇到循環時,首先在 Boolean context 中 <布爾計算的表達式> 進行評估。
n = 5
while n > 0:
n -= 1
print(n)
輸出:
4
3
2
1
0
while 首先測試循環的控制表達式。假設開始就為假,則循環體將永遠不會被執行。
n = 5
while n > 5:
n -= 1
print(n)
break語句 和 continue語句
while 循環的整個主體都在每次迭代中執行,Python 提供了兩個過早終止循環迭代的關鍵字。
- break 語句立即完全終止循環。程序執行繼續到循環體之后的第一條語句。
- continue 語句立即終止當前循環迭代。執行跳轉到循環的頂部,并重新評估控制表達式以確定循環是再次執行還是終止。
# break 舉例
n = 5
while n > 0:
n -= 1
if n == 2:
break
print(n)
print('循環結束。')
輸出:
4
3
循環結束。
# continue 舉例
n = 5
while n > 0:
n -= 1
if n == 2:
continue
print(n)
print('循環結束。')
輸出:
4
3
1
0
循環結束。
else 子句
Python 允許在循環else結束時使用可選子句。
while <布爾計算的表達式>:
<執行的python語句> # 循環體
else:
<循環終止后執行語句>
n = 5
while n > 0:
n -= 1
print(n)
else:
print('Loop done.')
輸出:
4
3
2
1
0
Loop done.
# 如果有break某些情況下就不會倍執行
n = 5
while n > 0:
n -= 1
print(n)
if n == 2:
break
else:
print('循環結束。')
輸出:
4
3
2
無限循環
假設編寫了一個while理論上永遠不會結束的循環。
while True:
print('真·三國無雙')
真·三國無雙
真·三國無雙
.
.
.
真·三國無雙
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyboardInterrupt
這樣的循環只能手動停止。
單實際上也有它的應用,例如循環刪除列表中的元素。
list_ = ['真·三國無雙', '真·三國無雙', '真·三國無雙']
while True:
if not list_ :
break
print(list_ .pop(-1))
輸出:
真·三國無雙
真·三國無雙
真·三國無雙
可以 break 在循環中指定多個語句。可以通過 break 從幾個不同的位置結束循環,而不必在循環頭中指定所有終止條件。
while True:
? ? if <布爾計算的表達式1>: ?# 條件判斷1
? ? ? ? break
? ??
? ? if <布爾計算的表達式2>: ?# 條件判斷2
? ? ? ? break
? ??
? ? if <布爾計算的表達式3>: ?# 條件判斷3
? ? ? ? break
嵌套while循環
Python 控制結構可以相互嵌套。
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循環可以包含在另一個while循環中。
list_ = ['父親', '母親']
while len(list_ ):
print(list_.pop(0))
list__ = ['おじいさん', '祖母']
while len(list__ ):
print('>', list__.pop(0))
輸出:
父親
> おじいさん
> 祖母
母親
> おじいさん
> 祖母
在嵌套循環中找到的 break 語句適用于最近的封閉循環。
while <布爾計算的表達式1>:
statement
statement
while <布爾計算的表達式2>:
statement
statement
break # 適用于 while <布爾計算的表達式2>: 循環
break # 適用于 while <布爾計算的表達式1>: 循環
while循環可以嵌套在 if、elif、else 語句中。
if <布爾計算的表達式1>:
? ? <python執行語句1>
? ? while <布爾計算的表達式2>:
? ? ? ? <python執行語句2>
? ? ? ? <python執行語句3>
else:
? ? while <布爾計算的表達式3>:
? ? ? ? <python執行語句4>
? ? ? ? <python執行語句5>
? ? <python執行語句6>
while <布爾計算的表達式1>:
? ? if <布爾計算的表達式2>:
? ? ? ? <python執行語句1>
? ? elif <布爾計算的表達式3>:
? ? ? ? <python執行語句2>
? ? else:
? ? ? ? <python執行語句3>
? ? if <布爾計算的表達式4>:
? ? ? ? <python執行語句4>
單行 while 循環
與 if 語句一樣,while 可以在一行中指定循環。也可以用 ;組成多個循環體語句。
n = 5
while n > 0: n -= 1; print(n)
輸出:
4
3
2
1
0
兩個復合語句組合成簡寫方式是不可以的。
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
相關推薦
- 2022-05-13 使用openmp為循環邏輯提速
- 2022-01-09 Echarts|Stacked Line Chart(折線圖堆疊)Y軸數據不正確問題
- 2022-08-20 python深入講解魔術方法_python
- 2022-05-29 Python格式化字符串的案例方法_python
- 2022-09-08 Go語言中的Struct結構體_Golang
- 2022-03-25 Redis分布式鎖如何實現續期_Redis
- 2022-06-21 C語言實現順序表的全操作詳解_C 語言
- 2022-06-18 Android實現歷史搜索記錄_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支