We can display the python multiplication table with the help of the for loop and the while loop in Python. Consider the examples below:
How do you make a Multiplication Table in Python?
In the below example, the input for the program is taken from the user. The input given by the user at the time output is 10. The range given for the loop is (1,11)which means that the number must be greater than equal to one and lesser than 11. In the first iteration, the number is multiplied by 1. In the second iteration, the number is multiplied by 2, and so on until 10.
Source Code for Python Multiplication Table using the for Loop
numb = int(input(" Enter a number : "))
# using the for loop to generate the multiplication tables
print("Table of: ")
for a in range(1,11):
print(num,'x',a,'=',num*a)
Output
Enter the number : 7
Multiplication Table of :
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
How do you Create a Table in Python?
As mentioned above, one can create a multiplication table in python using the for loop or the while loop. The multiplication table can also be created using some pre-defined functions such as the def function.
How do you make a while Loop in Python using Multiplication Tables?
In the example discussed below, we have used the while loop to create the multiplication table. We declared a variable ‘i’ and initialized the variable by 1. The loop will keep iterating until the i value reaches 10. Each time the while loop is iterated, the value of i will be increased by 1. When the value of i becomes greater than 10, the loop will be terminated.
Source Code for Python Multiplication table using the while loop
#include
int main()
{
int a, b;
printf("Enter a Number ");
scanf("%d",&a);
b=1;
while(b<=10){
printf("%d * %d = %d \n", a, b, a*b);
++b;
}
getch();
}
Output:
Enter a Number
4
4 * 1 = 4
4 * 2 = 8
4 * 3 = 12
4 * 4 = 16
4 * 5 = 20
4 * 6 = 24
4 * 7 = 28
4 * 8 = 32
4 * 9 = 36
4 * 10 = 40
What is the Function for Multiplication in Python?
The function def is used for deriving the python multiplication table. The def function used 2 parameters as input and returns the value of the two numbers. Let us look at the example given below for more details:
def multiply(a,z):
return a*z;
numb1=15
numb2=5
print("The product is: ",multiply(numb1,numb2))
Output:
75
Multiply lists in python
In python, we can multiply two equal lists using the zip(), once they are multiplied together they will be appended into a new list. The element of one list is multiplied by the element in the other list.
Source code
list1 = [5, 2, 3]
list2 = [1, 5, 4]
multiply = []
for number1, number2 in zip(list1, list2):
multiply.append(number1 * number2)
print(multiply)
Output:
[5, 10, 12]
math.prod function
Lists in python can be multiplied using the prod function by importing the math module.
Look at the example given below:
import math
list1 = [2, 5, 3]
list2 = [4, 1, 5]
a1 = math.prod(list1)
z2 = math.prod(list2)
print("The product of list1 is: ",a1)
print("The product of list2 is: ",z2)
Output
The product of list1 is: 30
The product of list2 is: 20
Leave a Reply