摘要: 在本文中,咱们将学习 python 中的各种模式程序。
本文分享自华为云社区《从零开始学习 python | 通过示例学习如何制作 Python 模式程序
》,原文作者:Yuchuan。
Python 编程语言很容易学习。易于语法实现的各种库使其怀才不遇,这是它成为本世纪最风行的编程语言的泛滥起因之一。尽管学习很容易,但拜访员通常会在建设模式程序逻辑时寻求您的办法。只管听起来很辣手,但应用 python 却是小菜一碟。在本文中,咱们将学习 python 中的各种模式程序。此博客涵盖以下主题:
- Star Pattern Programs
- Pyramid Pattern Program
- Half-Pyramid Pattern Program
- Diamond Shaped Pattern Program
- Start Pattern Program
- Hourglass Pattern Program
- Number Pattern Programs
- Simple Numbers In A Pyramid
- Pascal’s Triangle Pattern
- Diamond Pattern Program
- Characters Patter Programs
模式程序蕴含许多嵌套循环。因而,如果您不相熟 python 中的循环,请确保查看无关 python 中的循环的具体教程。
星型程序
以下是 python 中的一些星型程序。
金字塔图案程序
def pattern(n):
k = 2 * n - 2
for i in range(0,n):
for j in range(0,k):
print(end=" ")
k = k - 1
for j in range(0, i+1):
print("*", end=" ")
print("r")
pattern(5)
输入:
反金字塔图案程序
def pattern(n):
k = 2*n -2
for i in range(n,-1,-1):
for j in range(k,0,-1):
print(end=" ")
k = k +1
for j in range(0, i+1):
print("*", end=" ")
print("r")
pattern(5)
输入:
正确启动模式程序
def pattern(n):
for i in range(0, n):
for j in range(0, i + 1):
print("*", end="")
print("r")
for i in range(n, 0 , -1):
for j in range(0, i + 1):
print("*", end="")
print("r")
pattern(5)
输入:
左启动模式程序
def pattern(n):
k = 2 * n - 2
for i in range(0, n-1):
for j in range(0, k):
print(end=" ")
k = k - 2
for j in range(0, i + 1):
print("*", end="")
print("r")
k = -1
for i in range(n-1,-1,-1):
for j in range(k,-1,-1):
print(end=" ")
k = k + 2
for j in range(0, i + 1):
print("*", end="")
print("r")
pattern(5)
输入:
沙漏图案程序
def pattern(n):
k = n - 2
for i in range(n, -1 , -1):
for j in range(k , 0 , -1):
print(end=" ")
k = k + 1
for j in range(0, i+1):
print("*" , end="")
print("r")
k = 2 * n - 2
for i in range(0 , n+1):
for j in range(0 , k):
print(end="")
k = k - 1
for j in range(0, i + 1):
print("*", end="")
print("r")
pattern(5)
输入:
半金字塔图案程序
def pattern(n):
for i in range(0,n):
for j in range(0, i+1):
print("*" , end="")
print("r")
pattern(5)
输入:
左半金字塔图案程序
def pattern(n):
k = 2 * n - 2
for i in range(0, n):
for j in range(0, k):
print(end=" ")
k = k - 2
for j in range(0, i + 1):
print("*", end="")
print("r")
pattern(5)
输入:
向下半金字塔图案程序
def pattern(n):
for i in range(n, -1, -1):
for j in range(0, i + 1):
print("*", end="")
print("r")
pattern(5)
输入:
菱形图案程序
def pattern(n):
k = 2 * n - 2
for i in range(0, n):
for j in range(0 , k):
print(end=" ")
k = k - 1
for j in range(0 , i + 1):
print("*", end="")
print("r")
k = n - 2
for i in range(n , -1, -1):
for j in range(k , 0 , -1):
print(end=" ")
k = k + 1
for j in range(0 , i + 1):
print("*", end="")
print("r")
pattern(5)
输入:
钻石星型打算
for i in range(5):
for j in range(5):
if i + j == 2 or i - j == 2 or i + j == 6 or j - i == 2:
print("*", end="")
else:
print(end=" ")
print()
输入:
数字模式程序
以下是一些带有数字模式的 Java 程序。
简略数字程序
def pattern(n):
x = 0
for i in range(0 , n):
x += 1
for j in range(0, i + 1):
print(x , end=" ")
print("r")
pattern(5)
输入:
帕斯卡三角打算
def pascal(n):
for i in range(0, n):
for j in range(0, i + 1):
print(function(i, j),"", end="")
print()
def function(n, k):
res = 1
if (k > n - k):
k = n - k
for i in range(0, k):
res = res * (n - i)
res = res // (i + 1)
return res
pascal(7)
输入:
半金字塔图案与数字
def pattern(n):
for i in range(1, n):
for j in range(1, i + 1):
print(j, end= " ")
print("r")
pattern(5)
输入:
菱形图案与数字
def pattern(n):
k = 2 * n - 2
x = 0
for i in range(0, n):
x += 1
for j in range(0, k):
print(end=" ")
k = k - 1
for j in range(0, i + 1):
print(x, end=" ")
print("r")
k = n - 2
x = n + 2
for i in range(n, -1, -1):
x -= 1
for j in range(k, 0, -1):
print(end=" ")
k = k + 1
for j in range(0, i + 1):
print(x, end=" ")
print("r")
pattern(5)
输入:
降序模式程序
def pattern(n):
for i in range(n, 0, -1):
for j in range(1, i + 1):
print(j, end=" ")
print("r")
pattern(5)
输入:
二进制数字模式程序
def pattern(n):
k = 2 * n - 2
for i in range(0, n):
for j in range(0, k):
print(end=" ")
k = k - 1
for j in range(0, i + 1):
print('10', end="")
print("r")
pattern(5)
输入:
字符模式程序
这是一些带有字符的 python 模式程序。
右字母三角
def pattern(n):
x = 65
for i in range(0, n):
ch = chr(x)
x += 1
for j in range(0, i + 1):
print(ch, end=" ")
print("r")
pattern(5)
输入:
角色模式程序
def pattern(n):
k = 2 * n - 2
x = 65
for i in range(0, n):
for j in range(0, k):
print(end=" ")
k = k - 1
for j in range(0, i + 1):
ch = chr(x)
print(ch, end=" ")
x += 1
print("r")
pattern(7)
输入:
K 形字符程序
for i in range(7):
for j in range(7):
if j == 0 or i - j == 3 or i + j == 3:
print("*", end="")
else:
print(end=" ")
print()
输入:
三角形字符图案程序
def pattern(n):
k = 2 * n - 2
x = 65
for i in range(0, n):
ch = chr(x)
x += 1
for j in range(0, k):
print(end=" ")
k = k - 1
for j in range(0, i + 1):
print(ch, end=" ")
print("r")
pattern(5)
输入:
菱形字符图案程序
def pattern(n):
k = 2 * n - 2
for i in range(0, n):
for j in range(0, k):
print(end=" ")
k = k - 1
x = 65
for j in range(0, i + 1):
ch = chr(x)
print(ch, end=" ")
x += 1
print("r")
k = n - 2
x = 65
for i in range(n, -1, -1):
for j in range(k, 0, -1):
print(end=" ")
k = k + 1
for j in range(0, i + 1):
ch = chr(x)
print(ch, end=" ")
x += 1
print("r")
pattern(5)
输入:
这使咱们到了本文的结尾,在这里咱们理解了如何在 python 的循环帮忙下应用星号,数字和字符在 python 中实现不同的模式。我心愿您对本教程中与您共享的所有内容一清二楚。
点击关注,第一工夫理解华为云陈腐技术~