用Python打印金字塔图案的程序

可以使用simple for循环在python中打印图案。 第一外环 用来处理 行数 内嵌套循环 用于处理 列数 .通过操纵打印语句,可以打印不同的数字模式、字母模式或星形模式。 本文展示了其中一些模式。

null
  • 简单金字塔图案

Python3

# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern
def pypart(n):
# outer loop to handle number of rows
# n in this case
for i in range ( 0 , n):
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range ( 0 , i + 1 ):
# printing stars
print ( "* " ,end = "")
# ending line after each row
print ( "
"
)
# Driver Code
n = 5
pypart(n)


输出

* * * * * * * * * * * * * * * 

  • 另一种方法: 使用Python3中的List,可以以更简单的方式完成

python

# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern
def pypart(n):
myList = []
for i in range ( 1 ,n + 1 ):
myList.append( "*" * i)
print ( "" .join(myList))
# Driver Code
n = 5
pypart(n)


输出

***************

  • 180度旋转后

Python3

# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern
def pypart2(n):
# number of spaces
k = 2 * n - 2
# outer loop to handle number of rows
for i in range ( 0 , n):
# inner loop to handle number spaces
# values changing acc. to requirement
for j in range ( 0 , k):
print (end = " " )
# decrementing k after each loop
k = k - 2
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range ( 0 , i + 1 ):
# printing stars
print ( "* " , end = "")
# ending line after each row
print ( "
"
)
# Driver Code
n = 5
pypart2(n)


输出

        *       * *     * * *   * * * * * * * * * 

  • 打印三角形

Python3

# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern triangle
def triangle(n):
# number of spaces
k = n - 1
# outer loop to handle number of rows
for i in range ( 0 , n):
# inner loop to handle number spaces
# values changing acc. to requirement
for j in range ( 0 , k):
print (end = " " )
# decrementing k after each loop
k = k - 1
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range ( 0 , i + 1 ):
# printing stars
print ( "* " , end = "")
# ending line after each row
print ( "
"
)
# Driver Code
n = 5
triangle(n)


输出

    *    * *   * * *  * * * * * * * * * 

  • 数字模式

Python3

# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern of numbers
def numpat(n):
# initialising starting number
num = 1
# outer loop to handle number of rows
for i in range ( 0 , n):
# re assigning num
num = 1
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range ( 0 , i + 1 ):
# printing number
print (num, end = " " )
# incrementing number at each column
num = num + 1
# ending line after each row
print ( "
"
)
# Driver code
n = 5
numpat(n)


输出

1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 

  • 没有重新分配的数字

Python3

# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern of numbers
def contnum(n):
# initializing starting number
num = 1
# outer loop to handle number of rows
for i in range ( 0 , n):
# not re assigning num
# num = 1
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range ( 0 , i + 1 ):
# printing number
print (num, end = " " )
# incrementing number at each column
num = num + 1
# ending line after each row
print ( "
"
)
n = 5
# sending 5 as argument
# calling Function
contnum(n)


输出

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 

  • 字符模式

Python3

# Python 3.x code to demonstrate star pattern
# Function to demonstrate printing pattern of alphabets
def alphapat(n):
# initializing value corresponding to 'A'
# ASCII value
num = 65
# outer loop to handle number of rows
# 5 in this case
for i in range ( 0 , n):
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range ( 0 , i + 1 ):
# explicitly converting to char
ch = chr (num)
# printing char value
print (ch, end = " " )
# incrementing number
num = num + 1
# ending line after each row
print ( "
"
)
# Driver Code
n = 5
alphapat(n)


输出

A B B C C C D D D D E E E E E 

  • 连续字符模式

Python3

# Python code 3.x to demonstrate star pattern
# Function to demonstrate printing pattern of alphabets
def contalpha(n):
# initializing value corresponding to 'A'
# ASCII value
num = 65
# outer loop to handle number of rows
- for i in range ( 0 , n):
# inner loop to handle number of columns
# values changing acc. to outer loop
for j in range ( 0 , i + 1 ):
# explicitly converting to char
ch = chr (num)
# printing char value
print (ch, end = " " )
# incrementing at each column
num = num + 1
# ending line after each row
print ( "
"
)
# Driver code
n = 5
contalpha(n)


  • 输出:
A B C D E F G H I J K L M N O

本文由 曼吉特·辛格(努布尔南部) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞8 分享