# 程序员都必须了解的18个Python模式程序片段

5 篇文章 0 订阅

1

2 2

3 3 3

4 4 4 4

5 5 5 5 5

Python

rows=6

fornuminrange(rows):

foriinrange(num):

print(num,end=”“)# print number

# line after each row to display pattern correctly

print(”“)

1 1 1 1 1

2 2 2 2

3 3 3

4 4

5

rows=5

b=0

foriinrange(rows,0,-1):

b+=1

forjinrange(1,i+1):

print(b,end=’‘)

print(‘\r’)

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Python

rows=5

forrowinrange(1,rows+1):

forcolumninrange(1,row+1):

print(column,end=’‘)

print(“”)

5 5 5 5 5

4 4 4 4

3 3 3

2 2

1

Python

rows=5

foriinrange(rows,0,-1):

num=i

forjinrange(0,i):

print(num,end=’‘)

print(“\r”)

5 5 5 5 5

5 5 5 5

5 5 5

5 5

5

Python

rows=5

num=rows

foriinrange(rows,0,-1):

forjinrange(0,i):

print(num,end=’‘)

print(“\r”)

2 1

3 2 1

4 3 2 1

5 4 3 2 1

Python

rows=6

forrowinrange(1,rows):

forcolumninrange(row,0,-1):

print(column,end=’‘)

print(“”)

0 1 2 3 4 5

0 1 2 3 4

0 1 2 3

0 1 2

0 1

Python

rows=5

foriinrange(rows,0,-1):

forjinrange(0,i+1):

print(j,end=’‘)

print(“\r”)

2 3 4

5 6 7 8 9

Python

currentNumber=1

stop=2

rows=3# Rows you want in your pattern

foriinrange(rows):

forcolumninrange(1,stop):

print(currentNumber,end=’‘)

currentNumber+=1

print(“”)

stop+=2

1

3 2

6 5 4

10 9 8 7

Python

start=1

stop=2

currentNumber=stop

forrowinrange(2,6):

forcolinrange(start,stop):

currentNumber-=1

print(currentNumber,end=’‘)

print(“”)

start=stop

stop+=row

currentNumber=stop

1 2 1

1 2 3 2 1

1 2 3 4 3 2 1

1 2 3 4 5 4 3 2 1

Python

rows=6

foriinrange(1,rows+1):

forjinrange(1,i–1):

print(j,end=”“)

forjinrange(i–1,0,-1):

print(j,end=”“)

print()

5 4 3 2 1 1 2 3 4 5

5 4 3 2 2 3 4 5

5 4 3 3 4 5

5 4 4 5

5 5

Python

rows=6

foriinrange(0,rows):

forjinrange(rows–1,i,-1):

print(j,”,end=”)

forlinrange(i):

print(‘‘,end=”)

forkinrange(i+1,rows):

print(k,”,end=”)

print(‘\n’)

10

10 8

10 8 6

10 8 6 4

10 8 6 4 2

Python

rows=5

LastEvenNumber=2*rows

evenNumber=LastEvenNumber

foriinrange(1,rows+1):

evenNumber=LastEvenNumber

forjinrange(i):

print(evenNumber,end=’‘)

evenNumber-=2

print(“\r”)

0

0 1

0 2 4

0 3 6 9

0 4 8 12 16

0 5 10 15 20 25

0 6 12 18 24 30 36

Python

rows=7

foriinrange(0,rows):

forjinrange(0,i+1):

print(i*j,end=’‘)

print()

3 3

5 5 5

7 7 7 7

9 9 9 9 9

Python

rows=5

i=1

whilei<=rows:

j=1

whilej<=i:

print((i*2–1),end=”“)

j=j+1

i=i+1

print()

1

1 2

1 2 3

1 2 3 4

1 2 3 4 5

Python

rows=6

forrowinrange(1,rows):

num=1

forjinrange(rows,0,-1):

ifj>row:

print(”“,end=’‘)

else:

print(num,end=’‘)

num+=1

print(“”)

*

* *

* * *

* * * *

* * * * *

* * * * * *

* * * * * * *

print(“PrintequilateraltrianglePyramidusingstars“)

size=7

m=(2*size)–2

foriinrange(0,size):

forjinrange(0,m):

print(end=”“)

m=m–1#decrementingmaftereachloop

forjinrange(0,i+1):

#printingfullTrianglepyramidusingstars

print(“*“,end=’‘)

print(”“)

* * * * * *

* * * * *

* * * *

* * *

* *

*

Python

rows=5

k=2*rows–2

foriinrange(rows,-1,-1):

forjinrange(k,0,-1):

print(end=”“)

k=k+1

forjinrange(0,i+1):

print(“*”,end=”“)

print(“”)

* *

* * *

* * * *

* * * * *

Python

rows=5

foriinrange(0,rows):

forjinrange(0,i+1):

print(“*”,end=’‘)

print(“\r”)

C语言C++编程学习交流圈子，QQ群：921427443【点击进入】微信公众号：C语言创作者中心

• 1
点赞
• 20
收藏
觉得还不错? 一键收藏
• 打赏
• 2
评论

MR_sasa

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。