Python实现不同格式打印九九乘法表
一、介绍
九九乘法表是我们在学习数学的初中阶段就会遇到的一个问题,也是一个经典的编程练习题。它用九行九列的形式展示了从1乘1到9乘9的乘法表。
本文将使用Python来实现不同格式的打印九九乘法表,通过简单的代码,我们可以以表格、行和列的形式展示九九乘法表。接下来,将分别介绍具体的实现方法。
二、表格格式打印九九乘法表
表格格式是最常见的展示形式,每个结果都以表格的形式对齐显示。下面是用Python实现的表格格式打印九九乘法表的代码:
def print_multiplication_table():
for i in range(1, 10):
for j in range(1, i+1):
result = i * j
print(f"{j} × {i} = {result}", end="\t")
print()
# 调用函数打印九九乘法表
print_multiplication_table()
运行上述代码,将得到如下输出结果:
1 × 1 = 1
1 × 2 = 2 2 × 2 = 4
1 × 3 = 3 2 × 3 = 6 3 × 3 = 9
1 × 4 = 4 2 × 4 = 8 3 × 4 = 12 4 × 4 = 16
1 × 5 = 5 2 × 5 = 10 3 × 5 = 15 4 × 5 = 20 5 × 5 = 25
1 × 6 = 6 2 × 6 = 12 3 × 6 = 18 4 × 6 = 24 5 × 6 = 30 6 × 6 = 36
1 × 7 = 7 2 × 7 = 14 3 × 7 = 21 4 × 7 = 28 5 × 7 = 35 6 × 7 = 42 7 × 7 = 49
1 × 8 = 8 2 × 8 = 16 3 × 8 = 24 4 × 8 = 32 5 × 8 = 40 6 × 8 = 48 7 × 8 = 56 8 × 8 = 64
1 × 9 = 9 2 × 9 = 18 3 × 9 = 27 4 × 9 = 36 5 × 9 = 45 6 × 9 = 54 7 × 9 = 63 8 × 9 = 72 9 × 9 = 81
从结果可以看出,输出的表格格式九九乘法表精确对齐,并且每一行的个数逐行增加。
三、行格式打印九九乘法表
行格式是将每一行的乘法式子连接起来打印,每个式子之间用制表符分隔开。下面是用Python实现的行格式打印九九乘法表的代码:
def print_multiplication_table():
for i in range(1, 10):
for j in range(1, i+1):
result = i * j
print(f"{j} × {i} = {result}", end="\t")
print()
# 调用函数打印九九乘法表
print_multiplication_table()
运行上述代码,将得到如下输出结果:
1 × 1 = 1
1 × 2 = 2 2 × 2 = 4
1 × 3 = 3 2 × 3 = 6 3 × 3 = 9
1 × 4 = 4 2 × 4 = 8 3 × 4 = 12 4 × 4 = 16
1 × 5 = 5 2 × 5 = 10 3 × 5 = 15 4 × 5 = 20 5 × 5 = 25
1 × 6 = 6 2 × 6 = 12 3 × 6 = 18 4 × 6 = 24 5 × 6 = 30 6 × 6 = 36
1 × 7 = 7 2 × 7 = 14 3 × 7 = 21 4 × 7 = 28 5 × 7 = 35 6 × 7 = 42 7 × 7 = 49
1 × 8 = 8 2 × 8 = 16 3 × 8 = 24 4 × 8 = 32 5 × 8 = 40 6 × 8 = 48 7 × 8 = 56 8 × 8 = 64
1 × 9 = 9 2 × 9 = 18 3 × 9 = 27 4 × 9 = 36 5 × 9 = 45 6 × 9 = 54 7 × 9 = 63 8 × 9 = 72 9 × 9 = 81
运行结果与表格格式打印九九乘法表的结果是一致的,只是输出的格式稍有不同。
四、列格式打印九九乘法表
列格式是将每一列的乘法式子连接起来打印,每个式子之间用换行符分隔开。下面是用Python实现的列格式打印九九乘法表的代码:
def print_multiplication_table():
for i in range(1, 10):
for j in range(1, i+1):
result = i * j
print(f"{j} × {i} = {result}")
print()
# 调用函数打印九九乘法表
print_multiplication_table()
运行上述代码,将得到如下输出结果:
1 × 1 = 1
1 × 2 = 2
2 × 2 = 4
1 × 3 = 3
2 × 3 = 6
3 × 3 = 9
1 × 4 = 4
2 × 4 = 8
3 × 4 = 12
4 × 4 = 16
1 × 5 = 5
2 × 5 = 10
3 × 5 = 15
4 × 5 = 20
5 × 5 = 25
1 × 6 = 6
2 × 6 = 12
3 × 6 = 18
4 × 6 = 24
5 × 6 = 30
6 × 6 = 36
1 × 7 = 7
2 × 7 = 14
3 × 7 = 21
4 × 7 = 28
5 × 7 = 35
6 × 7 = 42
7 × 7 = 49
1 × 8 = 8
2 × 8 = 16
3 × 8 = 24
4 × 8 = 32
5 × 8 = 40
6 × 8 = 48
7 × 8 = 56
8 × 8 = 64
1 × 9 = 9
2 × 9 = 18
3 × 9 = 27
4 × 9 = 36
5 × 9 = 45
6 × 9 = 54
7 × 9 = 63
8 × 9 = 72
9 × 9 = 81
可以看到,列格式的九九乘法表将同一列的乘法式子连接起来,使得每一列中的结果对齐。
五、总结
通过Python语言的简洁性和灵活性,我们可以用几行代码就实现了不同格式的九九乘法表的打印。无论是表格格式、行格式还是列格式,我们都能轻松地通过循环来实现。这样的练习题不仅能巩固我们对循环和字符串格式化的理解,还能提升我们的编程逻辑和代码实现能力。
希望本文对你理解和掌握Python打印九九乘法表的不同格式有所帮助。感谢阅读!
参考代码和运行结果
表格格式打印九九乘法表:
def print_multiplication_table():
for i in range(1, 10):
for j in range(1, i+1):
result = i * j
print(f"{j} × {i} = {result}", end="\t")
print()
# 调用函数打印九九乘法表
print_multiplication_table()
运行结果:
1 × 1 = 1
1 × 2 = 2 2 × 2 = 4
1 × 3 = 3 2 × 3 = 6 3 × 3 = 9
1 × 4 = 4 2 × 4 = 8 3 × 4 = 12 4 × 4 = 16
1 × 5 = 5 2 × 5 = 10 3 × 5 = 15 4 × 5 = 20 5 × 5 = 25
1 × 6 = 6 2 × 6 = 12 3 × 6 = 18 4 × 6 = 24 5 × 6 = 30 6 × 6 = 36
1 × 7 = 7 2 × 7 = 14 3 × 7 = 21 4 × 7 = 28 5 × 7 = 35 6 × 7 = 42 7 × 7 = 49
1 × 8 = 8 2 × 8 = 16 3 × 8 = 24 4 × 8 = 32 5 × 8 = 40 6 × 8 = 48 7 × 8 = 56 8 × 8 = 64
1 × 9 = 9 2 × 9 = 18 3 × 9 = 27 4 × 9 = 36 5 × 9 = 45 6 × 9 = 54 7 × 9 = 63 8 × 9 = 72 9 × 9 = 81
行格式打印九九乘法表:
def print_multiplication_table():
for i in range(1, 10):
for j in range(1, i+1):
result = i * j
print(f"{j} × {i} = {result}", end="\t")
print()
# 调用函数打印九九乘法表
print_multiplication_table()
运行结果:
1 × 1 = 1
1 × 2 = 2 2 × 2 = 4
1 × 3 = 3 2 × 3 = 6 3 × 3 = 9
1 × 4 = 4 2 × 4 = 8 3 × 4 = 12 4 × 4 = 16
1 × 5 = 5 2 × 5 = 10 3 × 5 = 15 4 × 5 = 20 5 × 5 = 25
1 × 6 = 6 2 × 6 = 12 3 × 6 = 18 4 × 6 = 24 5 × 6 = 30 6 × 6 = 36
1 × 7 = 7 2 × 7 = 14 3 × 7 = 21 4 × 7 = 28 5 × 7 = 35 6 × 7 = 42 7 × 7 = 49
1 × 8 = 8 2 × 8 = 16 3 × 8 = 24 4 × 8 = 32 5 × 8 = 40 6 × 8 = 48 7 × 8 = 56 8 × 8 = 64
1 × 9 = 9 2 × 9 = 18 3 × 9 = 27 4 × 9 = 36 5 × 9 = 45 6 × 9 = 54 7 × 9 = 63 8 × 9 = 72 9 × 9 = 81
列格式打印九九乘法表:
def print_multiplication_table():
for i in range(1, 10):
for j in range(1, i+1):
result = i * j
print(f"{j} × {i} = {result}")
print()
# 调用函数打印九九乘法表
print_multiplication_table()