How to Make a Multiplication Table in Python

A multiplication table for a number displays all the number’s multiples and their corresponding factors; usually in the ascending order of their greatness. We normally do this manually by writing down a set of ordered numbers and multiplying each of them with the number concerned then writing down the product we get next to them. In this post, we will write a program that creates a multiplication table using the Python programming language.

The best way to create a multiplication table for a number in Python is to use any type of loop – most probably a for loop – to iterate over a range of numbers, each time multiplying the number with a number from the range of numbers and printing the product on the console. 

If that does not make sense, the next few chapters will make it all clear as we will learn how to make a multiplication table in python using code and examples.

Making a Multiplication Table for a Number in Python

In this post, we will create a Python program that when given a number, will return a list of the number’s multiples from 1 to 12. In other words, we will learn how to make a multiplication table in Python. We will create it using Python for loops as it is the easiest method to do. Let’s start with the following block of code.

➊def multi_table(num):
    
    βž‹for i in range(1, 13):
        print(f'{i} X {num} = {i*num}')
        
➌num = float(input('Enter a number to print its Multiplication table: '))
➍multi_table(num)

At ➊, we create a function called multi_table using the def keyword which takes in an argument called num. Inside the function is a for loop which iterates over a range of numbers from 1 to 12. For each iteration, we use the multiplication operator to multiply the range number with the parameter passed in the function, and the product is printed to the console. We make use of the Python f strings to format our output to look orderly and nice.

At ➌, we ask the user to ‘Enter a number to print its multiplication table’ using the input function. But we immediately change that input to a floating point number because it is by default in string format. That floating point number is going to multiply the integer in the range() which is perfectly fine in Python.

At ➍ we call the function and we pass the number that we get from the input.

When you run the program, it will prompt you to enter a number, and then it will print its table of multiplications in the format shown below:

Enter a number to print its Multiplication table: 7
1 X 7.0 = 7.0
2 X 7.0 = 14.0
3 X 7.0 = 21.0
4 X 7.0 = 28.0
5 X 7.0 = 35.0
6 X 7.0 = 42.0
7 X 7.0 = 49.0
8 X 7.0 = 56.0
9 X 7.0 = 63.0
10 X 7.0 = 70.0
11 X 7.0 = 77.0
12 X 7.0 = 84.0

That was how to make a multiplication table with Python. If you have any questions, let me know in the comments section below.

Stephen Mclin
Stephen Mclin

Hey, I'm Steve; I write about Python and Django as if I'm teaching myself. CodingGear is sort of like my learning notes, but for all of us. Hope you'll love the content!

Articles: 106