Print Multiplication Table in Python

for i in range(1, 11): print n * i. The `or 10` is the sneaky bit.

BeginnerBasic Python ProgramsExample 22 of 25
print-multiplication-table.py
1# Program to print multiplication table of a number
2
3num = int(input("Enter a number: "))
4limit = int(input("Enter limit (default 10): ") or 10)
5
6for i in range(1, limit + 1):
7 print(f"{num} x {i} = {num * i}")

Output

Enter a number: 5
Enter limit (default 10): 
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50

In this one

  • range(1, limit + 1), not range(1, limit)
  • input() or 10 — empty string becomes 10

What's going on

for i in range(1, limit + 1) because range stops before the end. Want 10 rows? range(1, 11).

int(input(...) or 10) — empty input is "", which is falsy, so you get 10. That's why hitting Enter uses the default.

f-strings keep the 5 x 3 = 15 readable. Concatenating with commas also works and looks worse.

After this program

If the idea is still fuzzy, read Day 1: Introduction to Python in the Python course. For a short check, try the Python quiz (300 questions). Stay with all 25 basic python programs, or see every topic on Python exercises.