How to create multiplication table in Python
This is a Python tutorial, where you will learn how to make multiplication table in Python. It’s very easy to create a multiplication table in Python if you understand the working principle of a for loop.
Here we will use these:
- Input from the user
- for loop
Create multiplication table in Python
We can create two types of multiplication table using Python.
- Simple Multiplication table
- A multiplication table with a user-defined range
In simple multiplication table, there will be only one option for the user to input the desired number. The program will give you an output with a multiplication table of that number.
In user-defined range – There will be two options for the user to input the numbers. The first number will be the number of which he/she wants to create the multiplication table. And the second number is for the range.
If we enter
Then the output of the program will be like this:
2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10
Python Program to create a multiplication table
Here is the Python Code to make a multiplication table
# Python code to create multiplication table a = int(input()) b = int(input()) for i in range(1,b+1): print (str(a)+" x "+str(i)+" = "+str(a*i))
If you run this program you will have to enter two numbers.
Output will look like this if you enter 10 and 6
$ python codespeedy.py 10 6 10 x 1 = 10 10 x 2 = 20 10 x 3 = 30 10 x 4 = 40 10 x 5 = 50 10 x 6 = 60
range() is used to bind the loop in a certain range. In this case we have bind our loop from 1 to user-defined range.
range(1,n+1) – This is the syntax to create a range between 1 to n.
If you wish to create a simple multiplication table then set the variable b=10.
Thus, you will get the default multiplication table of any number you enter.
You can see that we have used str() method. Because we can’t concatenate or add strings with int value in print().
That’s why we have to convert int value to string first.