In this article we have prepared a program to calculate and display multiplication table, using for loop for any number provided by user.
Program to get product table using range function
# Multiplication table (from 1 to 10) in Python
# Uncomment below line to take input from the user
# num = int(input("Get multiplication table for: "))
# Comment the below line and uncomment above line if you want to take input from user take
num = 12
# Below loop will iterate 10 times starting from 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
Uncomment num = int(input("Get multiplication table for? ")) line if you want to take input from user. For illustrating example weโve assign a fix value to num variable i.e 12.
The range(1, 11) function will iterate from 1 to 10 i.e 10 times. First argument(1) specified in range(1, 11) function will be inclusive and second argument(11) will be excluded from range.
You can also try with different loops.
Hope you like this!
Keep helping and happy ๐ coding
Related Articles
Deepen your understanding with these curated continuations.
How to Generate Random Numbers in Python: A Quick Guide
Learn how to generate random integers and floats in Python using the random library. Master randint() and random() functions with simple code examples.
How to Check if a Variable is an Integer in Python
Learn how to check if a variable is an integer in Python using the type() and isinstance() functions. Essential for data validation and type-safe coding.
Swapping Variable Values in Python
Discover multiple ways to swap the values of two variables in Python, including the elegant tuple unpacking method and the traditional third-variable approach.