How to make a multiplication table in python
Learn how to make a multiplication table in Python, with an easy-to-follow example.
Creating a Multiplication Table in Python
A multiplication table is a great way to practice and review multiplication facts. Creating one in Python is a great way to learn the language, and have some fun at the same time. In this tutorial, we will learn how to create a multiplication table in Python.
First, we need to create a function that will generate the table. The function will take two parameters: the number of rows and columns, and will return a two-dimensional array of the multiplication table.
def multiplication_table(rows, columns):
# create an empty list
table = []
# loop through the rows
for i in range(1, rows+1):
# create an empty list
row = []
# loop through the columns
for j in range(1, columns+1):
# append the product of i and j to the row
row.append(i*j)
# append the row to the table
table.append(row)
# return the table
return table
Now, let's call the function and print the table. We can specify the number of rows and columns:
# call the function
table = multiplication_table(10, 10)
# print the table
for row in table:
print(row)
This will produce the following output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]
[4, 8, 12, 16, 20, 24, 28, 32, 36, 40]
[5, 10, 15, 20, 25, 30, 35, 40, 45, 50]
[6, 12, 18, 24, 30, 36, 42, 48, 54, 60]
[7, 14, 21, 28, 35, 42, 49, 56, 63, 70]
[8, 16, 24, 32, 40, 48, 56, 64, 72, 80]
[9, 18, 27, 36, 45, 54, 63, 72, 81, 90]
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
And there you have it! We have successfully created a multiplication table in Python. This is a great way to practice and review multiplication facts, and learn Python at the same time. Happy coding!