>백엔드 개발 >파이썬 튜토리얼 >Day - 루프 및 패턴 프로그램에 대해 중첩됨

Day - 루프 및 패턴 프로그램에 대해 중첩됨

Mary-Kate Olsen
Mary-Kate Olsen원래의
2024-12-26 20:16:11800검색

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 - 루프 및 패턴 프로그램에 대해 중첩됨의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.