日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中for循环语句格式_Python基础-10循环语句

發(fā)布時間:2025/3/15 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中for循环语句格式_Python基础-10循环语句 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Python

Python開發(fā)

Python語言

Python基礎(chǔ)-10循環(huán)語句

10.循環(huán)語句

對于循環(huán)語句,Python僅提供了while和for兩個,并未像其他編程語言一樣提供for...each/do...while/while...do等。

10.1 while循環(huán)

while在Python中的基本格式如下所示:

while condition:

doSomething

示例代碼如下所示:

startNum,endNum=0,5

while startNum< endNum:

print(f"cur start number is {startNum}")

startNum+=1

輸出結(jié)果如下所示:

cur start number is 0

cur start number is 1

cur start number is 2

cur start number is 3

cur start number is 4

10.2 for循環(huán)

1.基本for循環(huán)

在Python中for循環(huán)的基本格式如下所示:

for item in [列表、元組、字典、集合、字符等]:

doSomething

示例代碼如下所示:

a={1,2,3,4,5}

for item in a:

print(f"current item is {item}")

輸出結(jié)果如下所示:

current item is 1

current item is 2

current item is 3

current item is 4

current item is 5

2.for..else循環(huán)

for...else表示for中的循環(huán)正常完成之后,再運行else中的語句。其基本語法格式如下所示:

for item in [列表、元組、字典、集合、字符等]:

doSomething

else:

doSomething

有時候我們需要判斷程序循環(huán)是否正常退出,還是中途退出,可以使用下面的代碼:

a = [1, 2, 3, 4]

flag = True

for i in a:

if i == 2:

flag = False

break

if flag:

print('yes')

else:

print('no')

針對以上這種寫法,可以使用for...else來簡化寫法,如下所示:

a = [1, 2, 3, 4]

for i in a:

if i == 2:

break

else:

print('yes')

print('no')

既然Python提供了兩種形式的循環(huán)語句,那兩者有什么區(qū)別,何時采用while循環(huán),何時采用for循環(huán)了?

當循環(huán)迭代次數(shù)不確定時,使用while循環(huán),循環(huán)迭代次數(shù)確定時,使用for循環(huán)

10.3 中斷語句

在使用循環(huán)語句時,當滿足某個條件之后,循環(huán)則會自動停止,但如果想提前退出循環(huán)時,則可以使用中斷語句,常用的中斷語句為

break:中斷整個循環(huán),即滿足條件時,則立即中止循環(huán),后續(xù)循環(huán)不再繼續(xù)

continue:中斷本次循環(huán),即滿足條件后,則中止當前的循環(huán),后續(xù)滿足條件時繼續(xù)循環(huán)

示例代碼如下所示:

print("break 循環(huán)")

for i in range(6):

if i == 3:

break

print(f"current value is {i}")

print("continue 循環(huán)")

for i in range(6):

if i == 3:

continue

print(f"current value is {i}")

輸出結(jié)果如下所示:

break 循環(huán)

current value is 0

current value is 1

current value is 2

continue 循環(huán)

current value is 0

current value is 1

current value is 2

current value is 4

current value is 5

10.4 遍歷容器類數(shù)據(jù)

1.range()函數(shù)

range()函數(shù)常用于生成一系列的數(shù)字,其基本使用格式如下所示:

range(start,end,step)

start:開始值

end:結(jié)束值

step:為步長,可以為正值,也可以為負值,也可以省略,當省略時,默認步長為1

使用range()函數(shù)生成數(shù)據(jù),也遵循含前不含后,如range(0,3),生成的數(shù)據(jù)為0,1,2

示例代碼如下所示:

list(range(0,10,2))

# 輸出結(jié)果

[0, 2, 4, 6, 8]

list(range(10,0,-2))

# 輸出結(jié)果

[10, 8, 6, 4, 2]

2.遍歷字符串數(shù)據(jù)

字符串可以看成是很多單個字符組成的一串數(shù)據(jù),因為也是可以進行迭代循環(huán)的,示例如下所示:

for i in "abcdef":

print(f"current char is {i}")

輸出結(jié)果如下所示:

current char is a

current char is b

current char is c

current char is d

current char is e

current char is f

3.遍歷元組

示例代碼如下所示:

for i in tuple(range(0,5)):

print(f"current value is {i}")

輸出結(jié)果如下所示:

current value is 0

current value is 1

current value is 2

current value is 3

current value is 4

4.遍歷列表

示例代碼如下所示:

for i in list(range(0,5)):

print(f"current value is {i}")

輸出結(jié)果如下所示:

current value is 0

current value is 1

current value is 2

current value is 3

current value is 4

5.遍歷集合

示例代碼如下所示:

for i in set(range(0,5)):

print(f"current value is {i}")

輸出結(jié)果如下所示:

current value is 0

current value is 1

current value is 2

current value is 3

current value is 4

6.遍歷字典

示例代碼如下所示:

dic={

"a":1,

"b":2,

"c":3,

"d":4,

}

for k,v in dic.items():

print(f"key is {k} , value is {v}")

輸出結(jié)果如下所示:

key is a , value is 1

key is b , value is 2

key is c , value is 3

key is d , value is 4

本文同步在微信訂閱號上發(fā)布,如各位小伙伴們喜歡我的文章,也可以關(guān)注我的微信訂閱號:woaitest,或掃描下面的二維碼添加關(guān)注:

內(nèi)容來源于網(wǎng)絡(luò),如有侵權(quán)請聯(lián)系客服刪除

總結(jié)

以上是生活随笔為你收集整理的python中for循环语句格式_Python基础-10循环语句的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。