Definition
Times table
Rows of products for one base number.
A times table is a short loop: for each row index i, print base x i = base * i. This tutorial covers fixed and interactive bases, a custom row limit, a live preview, worked Python examples, edge cases, and complexity.
Times table
Rows of products for one base number.
range(1, last+1)
Repeat the same print pattern for each row.
base x i =
Clear f-string rows that match school tables.
Validate n
Read a positive integer and reject bad text.
Any base
Print rows 1–10 for a base you choose.
O(1) space
Linear in the number of printed rows.
A multiplication table for a base n is the list of products n×1, n×2, … up to some limit (often 10). In code, that is a single loop that prints one formatted row per multiplier.
Interviews use this to check loops, f-strings, and simple input validation — not fancy math. Once the helper exists, you can swap a fixed base for user input or change how many rows you print.
It is one of the cleanest ways to show you understand for loops, range, and readable console output.
Each i prints one product row.
Reuse print_multiplication_table.
Catch non-integers and non-positive bases.
Pass last as 10, 12, or 20.
In short: for i from 1 to last, print base x i = base * i.
Given a base number and a row limit, print each product from 1 through that limit in a readable format.
# base = 5, last = 10
# 5 x 1 = 5
# 5 x 2 = 10
# ...
# 5 x 10 = 50 | Item | Type | Description |
|---|---|---|
base | int | The number whose table you print (often positive). |
last | int | How many rows to print (commonly 10). |
| Output | text | One line per row: base x i = product. |
procedure print_table(base, last):
print heading
for i from 1 to last:
print base, i, and base * i | Method | Idea | Notes |
|---|---|---|
for + range | Known row count | Interview default — clearest |
while | Increment a counter | Works; slightly more boilerplate |
| Hard-coded prints | Ten separate lines | Avoid — does not scale with last |
| Goal | Pattern |
|---|---|
| Loop rows | for i in range(1, last + 1): |
| Print row | print(f"{base} x {i} = {base * i}") |
| Fixed demo | base = 5, last = 10 |
| Read base | n = int(input(...).strip()) |
| Reject bad input | except ValueError / if n <= 0 |
| Align columns | f"{i:2}", f"{base * i:4}" |
Same table — different ways to drive the rows.
for i in range(...)This page — clearest for fixed counts
i = 1; while i <= lastFine alternate; remember to increment
10 print linesBreaks as soon as last changes
helper(base, last)Reusable function beats one-off scripts
Reach for a times-table loop whenever you need repeated formatted product rows.
First programs that combine range and print.
Print the table for a number the user types.
Build readable rows with interpolation.
Handle ValueError and non-positive bases.
One base only — nested loops are a different problem.
Key benefit: a tiny reusable helper that teaches loops, formatting, and validation in one place.
Default base is 5 to match Example 1. Change it and click Print table.
Three complete Python programs — fixed table for 5, user-entered base, and a custom row limit with a while loop. Click View Output to reveal sample console results.
A reusable helper and the classic 5-times table.
Classic 5-times table from 1 to 10.
def print_multiplication_table(base: int, last: int) -> None:
print(f"Multiplication table for {base}:")
for i in range(1, last + 1):
print(f"{base} x {i} = {base * i}")
def main() -> None:
base = 5
last = 10
print_multiplication_table(base, last)
if __name__ == "__main__":
main() range(1, last + 1) produces multipliers 1 through 10. Each iteration prints one formatted product line for base 5.
Read a positive integer and print its table up to 10.
Reads a positive integer and prints its table up to 10.
def print_multiplication_table(base: int, last: int) -> None:
print(f"Multiplication table for {base}:")
for i in range(1, last + 1):
print(f"{base} x {i} = {base * i}")
def main() -> None:
try:
n = int(input("Enter a positive integer: ").strip())
except ValueError:
print("Could not read an integer.")
return
if n <= 0:
print("Please enter a positive integer.")
return
print_multiplication_table(n, 10)
if __name__ == "__main__":
main() Conversion errors become a clear message via except ValueError. Non-positive values are rejected before printing, matching typical school-table rules.
Let the caller choose how many rows to print — here with a while loop.
Prints the 7-times table through 12 using a while counter.
def print_multiplication_table_while(base: int, last: int) -> None:
print(f"Multiplication table for {base} (up to {last}):")
i = 1
while i <= last:
print(f"{base} x {i} = {base * i}")
i += 1
def main() -> None:
print_multiplication_table_while(7, 12)
if __name__ == "__main__":
main() A while loop needs an explicit counter and i += 1 each pass. Prefer for + range when the row count is known; use while when the stop condition is more open-ended.
Fix them in code or read them from input.
Walk each multiplier in order.
product = base * i for the current row.
Output base x i = product, then continue.
Trace the first few rows for base = 5, last = 10.
| i | base * i | Printed row |
|---|---|---|
1 | 5 | 5 x 1 = 5 |
2 | 10 | 5 x 2 = 10 |
3 | 15 | 5 x 3 = 15 |
… | … | same pattern |
10 | 50 | 5 x 10 = 50 |
After i = 10, the loop ends — matching Example 1.
Where printing a times table shows up beyond the interview prompt.
First clean for-loop with range.
Example: table of 5.
User types a base; you print 1–10.
Example: Example 2 flow.
Practice f-strings and alignment.
Example: width fields.
Reject text and non-positive bases.
Example: try / except.
Same output, two loop styles.
Example: Example 3.
Another loop that multiplies repeatedly.
Example: related topic.
Pro Tip: write the helper first with fixed args, then wrap it with input — easier to debug.
Why the loop-based table approach works well for beginners and interviews.
A few lines that anyone can dry-run on paper.
Change 10 to 12 or 20 without rewriting prints.
One helper serves fixed demos and input programs.
O(last) time and O(1) extra memory.
Pro Tip: prefer for + range in interviews unless asked specifically for while.
Small habits that keep times-table programs interview-ready.
Keep printing in print_multiplication_table(base, last).
Remember range(1, last + 1) is inclusive of last.
Handle ValueError and non-positive bases before the loop.
Do not hard-code 10 inside the helper if assignments vary.
Use width fields for neat columns in demos.
Pro Tip: dry-run 5×1 through 5×3 aloud — if those rows match, the loop is correct.
Mistakes that commonly break times-table programs.
Using range(1, last) and missing the last row.
→ Use range(1, last + 1).
Infinite loop when i never increases.
→ Always i += 1 inside while.
Calling int() on non-numeric text crashes.
→ Wrap input conversion in try / except.
Copy-pasted lines that cannot change last.
→ Always use a loop.
Nesting loops when only one base was asked.
→ One loop for one times table.
Handle these before claiming the table program is complete.
Catch conversion errors and show a clear message.
Decide whether to allow or reject based on assignment rules.
Loop prints nothing — validate if that is unexpected.
Still O(last); output volume grows with rows.
Products are all 0; math works, school rules may reject it.
Products flip sign; allow only if the problem says so.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
base * i for each row index.Quick Takeaway: loop i from 1 to last, print base x i = base * i, validate input when needed.
| Task | Time | Extra space |
|---|---|---|
Print last rows | O(last) | O(1) |
| Input + print | O(last) | O(1) |
| while version | O(last) | O(1) |
Ignoring the size of printed text, cost grows with how many rows you emit.
A multiplication table is a short loop that prints base x i = base * i for each multiplier. Prefer a reusable helper, validate interactive input, and parameterize last when assignments ask for 12 or 20 rows.
Practice the three examples above, then continue to checking whether a number is a natural number.
for i in range(1, last + 1): print f"{base} x {i} = {base * i}".
base and lastfor + rangebase x i = rowslast + 1 in rangei += 1 in whilePrint a times table the interview-friendly way.
One row per i
Patternbase x i =
OutputValidate n
SafetyParameterize rows
FlexibleO(last) / O(1)
AnalysisA multiplication table for a number n is the list n x 1, n x 2, .... A loop prints these rows automatically instead of writing each line by hand.
Learn how to check whether a number is a natural number in Python.
8 people found this page helpful