Print Multiplication Table of a Number in Python
Printing a multiplication table is a classic programming exercise for beginners.
It helps in understanding loops, arithmetic operations, and formatted output.
Concept Overview
A multiplication table shows the product of a number with integers from 1 to 10 (or more).
We use loops to iterate through numbers and compute results.
Example
Input:
Number: 5
Output:
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
Method 1: Using for Loop
num = 5
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
Method 2: Using while Loop
num = 5
i = 1
while i <= 10:
print(f"{num} x {i} = {num * i}")
i += 1
Method 3: Using List Comprehension (Advanced)
num = 5
[print(f"{num} x {i} = {num * i}") for i in range(1, 11)]
Output
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
...
5 x 10 = 50
Detailed Explanation
The loop runs from 1 to 10 and multiplies each value with the given number.
Formatted strings (f-strings) help display clean output.
While loops provide more control over iteration.
Applications
Used in basic math programs.
Helps understand iteration and arithmetic operations.
Advantages
Simple and beginner-friendly.
Multiple implementation methods.
Limitations
Fixed range unless modified dynamically.
Improvements You Can Make
Take input from the user.
Print tables up to n numbers.
Display tables in grid format.
This exercise strengthens your understanding of loops and formatted output in Python.
Codecrown