49 Python中巢狀迴圈

2021-10-04 06:33:34 字數 1436 閱讀 1007

目錄

巢狀迴圈

案例一案例二:

案例三:

乙個迴圈體內可以嵌入另乙個迴圈, 一般稱為」巢狀迴圈」, 或者」多重迴圈」;

for i in range(5):

for m in range(5):

print(i,end='\t')

print() #起到換行的作用

輸出效果:

d:\wwwroot\pyiteam\venv\scripts\python.exe d:/wwwroot/pyiteam/mypy01.py

0 0 0 0 0

1 1 1 1 1

2 2 2 2 2

3 3 3 3 3

4 4 4 4 4

process finished with exit code 0

利用巢狀迴圈列印九九乘法表

for x in range(1,10):

for y in range(1,x+1):

print("x=".format(x,y,x*y),end='\t')

print()

輸出效果

6x6=36	

7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49

8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64

9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81

process finished with exit code 0

用列表和字典儲存資訊,並列印出表中工資高於15000的資料

#定義個列表

tb =

#定義字典儲存資料

r1 = dict(name="小紅",age=18,salary=30000,city='北京')

r2 = dict(name="小明",age=16,salary=10000,city='南京')

r3 = dict(name="小藍",age=15,salary=20000,city='上海')

#將字典資料放入列表中

tb = [r1,r2,r3]

for i in tb:

if i.get("salary") > 15000:

print(i)

輸出:

d:\wwwroot\pyiteam\venv\scripts\python.exe d:/wwwroot/pyiteam/mypy01.py

process finished with exit code 0

騰訊精選49 python

class solution object defreversestring self,s type s list str rtype none do not return anything,modify s in place instead.i,n 0,len s while i s i s 1 ...

Python 迴圈巢狀

python 語言允許在乙個迴圈體裡面嵌入另乙個迴圈。python for 迴圈巢狀語法 foriterating var insequence foriterating var insequence statements s statements s python while 迴圈巢狀語法 whi...

Python 迴圈巢狀

python 語言允許在乙個迴圈體裡面嵌入另乙個迴圈。python for 迴圈巢狀語法 for iterating var in sequence for iterating var in sequence statements s statements s python while 迴圈巢狀語法...