登录
首页 >  文章 >  python教程

Day - 嵌套 for 循环和模式程序

来源:dev.to

时间:2024-12-11 08:48:34 310浏览 收藏

golang学习网今天将给大家带来《Day - 嵌套 for 循环和模式程序》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习文章或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!

Day - 嵌套 for 循环和模式程序

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 循环和模式程序》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布文章相关知识,快来关注吧!

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>