原文: https://www.programiz.com/python-programming/examples/multiplication-table
要理解此示例,您应该了解以下 Python 编程主题:
在下面的程序中,我们使用了for循环来显示 12 的乘法表。
# Multiplication table (from 1 to 10) in Python
num = 12
# To take input from the user
# num = int(input("Display multiplication table of? "))
# Iterate 10 times from i = 1 to 10
for i in range(1, 11):
print(num, 'x', i, '=', num*i) 输出
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120在这里,我们将for循环与range()函数一起使用进行了 10 次迭代。range()函数内部的参数为(1,11)。 表示大于或等于 1 且小于 11。
我们已经显示了变量num的乘法表(本例中为 12)。 您可以在上述程序中更改num的值以测试其他值。