Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Print Multiplication Table in Python

Make a Python times table that stops at the requested row. Learn range’s exclusive stop, a blank-input default, and readable f-strings in a beginner loop.

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}")

Sample Output

5 x 10 = 50

The last actual row uses multiplier 10; the sample omits intermediate rows for space.

In this one

  • Include the last multiplier with limit + 1
  • Supply a default for empty input
  • Interpolate values into an f-string

How it works

  1. 1

    Read num as an integer.

  2. 2

    Use the blank-response fallback while converting limit.

  3. 3

    Visit i from 1 through limit and print num * i on each row.

Edge cases

Blank limit

Press Enter at the limit prompt.

The empty string selects the default 10.

Zero limit

Enter 0.

The nonempty string converts to zero; range(1, 1) is empty and prints no rows.

What's going on

range stops before its end. Want rows 1 through 10? Use range(1, 11), not range(1, 10). Here limit + 1 keeps the last requested multiplier in the table.

The input expression for limit has a small shortcut: an empty response is the empty string, which is false in a Boolean context. input(...) or 10 therefore chooses 10 on a blank response, and int converts the chosen value. The text "0" is not empty, so it does not trigger the default.

Each iteration prints one f-string containing num, i, and their product. The sample abbreviates middle rows with dots; the program itself prints every row and no dots. A zero or negative limit produces no rows because the forward range is empty.

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.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Why add 1 to limit in range?

Pick an answer to check it.

Question 2

Does entering "0" select the default limit?

Pick an answer to check it.

Questions people ask

Why add 1 to limit in range?

The stop value is excluded, so limit + 1 includes the desired final multiplier.

Does entering "0" select the default limit?

No. "0" is a nonempty string, so it converts to the integer zero.

Will the program print the sample dots?

No. The dots abbreviate the displayed example; the loop prints all actual rows.