首頁 > 後端開發 > Python教學 > Day - 巢狀 for 迴圈和模式程序

Day - 巢狀 for 迴圈和模式程序

Mary-Kate Olsen
發布: 2024-12-26 20:16:11
原創
778 人瀏覽過

Day - Nested for loop and Pattern Programs

for row in range(5):
    for col in range(5-row):
        print(col+1, end=" ")
    print()
登入後複製
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
登入後複製
for row in range(5):
    for col in range(row+1):
        print(col+1, end=" ")
    print()
登入後複製
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
登入後複製
for row in range(5):
    for col in range(5-row):
        print(5-col, end=" ")
    print()
登入後複製
5 4 3 2 1 
5 4 3 2 
5 4 3 
5 4 
5 
登入後複製
for row in range(5):
    for col in range(row+1):
        print(5-col, end=" ")
    print()
登入後複製
5 
5 4 
5 4 3 
5 4 3 2 
5 4 3 2 1
登入後複製
no=1
for row in range(5):
    for col in range(5-row):
        print(no, end=" ")
        no+=1
    print()
登入後複製
1 2 3 4 5 
6 7 8 9 
10 11 12 
13 14 
15 
登入後複製
for row in range(5):
    for col in range(5-row):
        print((col+1)*(row+1), end=" ")
    print()
登入後複製
1 2 3 4 5 
2 4 6 8 
3 6 9 
4 8 
5 
登入後複製
for row in range(5): 
    for col in range(5-row):
        print((col+1)+(row+1), end=' ')
    print()
登入後複製
2 3 4 5 6 
3 4 5 6 
4 5 6 
5 6 
6
登入後複製
for row in range(5): 
    for col in range(5-row):
        print((col+1)//(row+1), end=' ')
    print()
登入後複製
1 2 3 4 5 
0 1 1 2 
0 0 1 
0 0 
0 
登入後複製
for row in range(5): 
    for col in range(5-row):
        print((row+1)-(col+1), end=' ')
    print()
登入後複製
0 -1 -2 -3 -4 
1 0 -1 -2 
2 1 0 
3 2 
4
登入後複製
for row in range(5): 
    for col in range(5-row):
        print((row+1)-(col+1)+5, end=' ')
    print()
登入後複製
5 4 3 2 1 
6 5 4 3 
7 6 5 
8 7 
9
登入後複製
for row in range(5): 
    for col in range(5-row):
        print((row+1)+(col+1)-1, end=' ')
    print()
登入後複製
1 2 3 4 5 
2 3 4 5 
3 4 5 
4 5 
5 
登入後複製
for row in range(5):
    for col in range(5-row):
        print(col,end=' ')
    print()
登入後複製
0 1 2 3 4 
0 1 2 3 
0 1 2 
0 1 
0 
登入後複製
for row in range(5): 
    for col in range(4-row):
        print(col+1, end=' ')
    print()
登入後複製
1 2 3 4 
1 2 3 
1 2 
1 

登入後複製
for row in range(5): 
    for col in range(4-row):
        print(col+1, end=' ')
    print("*" , end=" ")
    print()
登入後複製
1 2 3 4 * 
1 2 3 * 
1 2 * 
1 * 
* 
登入後複製
for row in range(5): 
    for col in range(4-row):
        print(col+1, end=' ')
    for col in range(5):
        print("*" , end=" ")
    print()
登入後複製
1 2 3 4 * * * * * 
1 2 3 * * * * * 
1 2 * * * * * 
1 * * * * * 
* * * * * 
登入後複製
for row in range(5): 
    for col in range(4-row):
        print(col+1, end=' ')
    for col in range(5):
        print(col, end=" ")
    print()
登入後複製
1 2 3 4 0 1 2 3 4 
1 2 3 0 1 2 3 4 
1 2 0 1 2 3 4 
1 0 1 2 3 4 
0 1 2 3 4 
登入後複製
for row in range(5): 
    for col in range(4-row):
        print(" ", end=' ')
    for col in range(5):
        print(col+1, end=" ")
    print()
登入後複製
        1 2 3 4 5 
      1 2 3 4 5 
    1 2 3 4 5 
  1 2 3 4 5 
1 2 3 4 5 
登入後複製
for row in range(5): 
    for col in range(4-row):
        print(" ", end=' ')
    for col in range(row+1):
        print(col+1,end=' ')
    print()
登入後複製
        1 
      1 2 
    1 2 3 
  1 2 3 4 
1 2 3 4 5 
登入後複製
for row in range(5): 
    for col in range(4-row):
        print("", end=' ')
    for col in range(row+1):
        print(col+1,end=' ')
    print()
登入後複製
    1 
   1 2 
  1 2 3 
 1 2 3 4 
1 2 3 4 5 
登入後複製
for row in range(5): 
    for col in range(4-row):
        print(" ", end=' ')
    for col in range(row+1):
        print(5-col,end=' ')
    print()
登入後複製
        5 
      5 4 
    5 4 3 
  5 4 3 2 
5 4 3 2 1
登入後複製
for row in range(5): 
    for col in range(4-row):
        print(" ", end=' ')
    for col in range(row+1):
        print(5+col-row,end=' ')
    print()
登入後複製
        5 
      4 5 
    3 4 5 
  2 3 4 5 
1 2 3 4 5 
登入後複製
name='ABCDE'

for row in range(len(name)):
    for col in range(row+1):
        print(name[col], end=" ")
    print()
登入後複製
A 
A B 
A B C 
A B C D 
A B C D E 
登入後複製
name='PRITHA'

for row in range(len(name)):
    for col in range(6-row):
        print(name[col], end=" ")
    print()
登入後複製
P R I T H A 
P R I T H 
P R I T 
P R I 
P R 
P 
登入後複製
for row in range(5):
    for col in range(5-row):
        print((col+1)%2, end=" ")
    print()
登入後複製
1 0 1 0 1 
1 0 1 0 
1 0 1 
1 0 
1 
登入後複製
for row in range(5):
    for col in range(5-row):
        print(((col+1)+(row))%2, end=" ")
    print()
登入後複製
1 0 1 0 1 
0 1 0 1 
1 0 1 
0 1 
1 
登入後複製

以上是Day - 巢狀 for 迴圈和模式程序的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板