# 从零开始学习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

## 星型程序

### 金字塔图案程序

``````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()``````

## 数字模式程序

### 简单数字程序

``````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)``````

## 字符模式程序

### 右字母三角

``````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)``````

1.2k 声望
1.7k 粉丝
0 条评论