Back to Python
2025-12-135 min read

Display the multiplication Table (Python Programming)

Learn Display the multiplication Table (Python Programming) step by step with clear examples and exercises.

Title: Displaying Multiplication Tables in Python Programming (Expanded Lesson)

Why This Matters

In this Python programming lesson, we will learn how to create a multiplication table. This skill is essential for anyone who wants to develop strong problem-solving abilities and master control structures like loops in Python. Understanding how to generate a multiplication table can help you prepare for coding interviews, real-world programming projects, or even debugging issues that might arise during your journey as a programmer.

Prerequisites

Before diving into the core concept, it's crucial to have a good understanding of Python fundamentals such as variables, data types, and basic input/output operations. Additionally, familiarity with control structures like loops (for loop) will be beneficial for this lesson. If you need a refresher on these topics, consider checking out our Python Basics tutorials.

Core Concept

To create a multiplication table in Python, we'll use a for loop to iterate through the range of numbers that we want to multiply with our chosen number (in this case, 12). The for loop will execute a specific block of code for each iteration, allowing us to print out the results.

Here's an example of how you can create a multiplication table in Python:

num = 12
table_length = 10

Iterate from 1 to the specified length using the range() function

for i in range(1, table_length + 1):

print(f"{num} x {i} = {num * i}")


In this code:
- We first set `num` to 12, which will be our multiplier.
- We also initialize `table_length` to 10, which specifies the number of rows in our table. You can modify this value to change the length of your table.
- The `range()` function generates a sequence of numbers from the start (inclusive) to the end (exclusive). In this case, we're using `range(1, table_length + 1)`, which means that the loop will iterate from 1 up to (but not including) `table_length + 1`.
- Within the for loop, we print out each multiplication result by concatenating the variables with the appropriate operators and using f-strings for better formatting.

Worked Example

Let's walk through a worked example together:

num = 7
table_length = 15

for i in range(1, table_length + 1):
print(f"{num} x {i} = {num * i}")

When you run this code, the output will be:

7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
7 x 11 = 77
7 x 12 = 84
7 x 13 = 91
7 x 14 = 98
7 x 15 = 105

As you can see, the for loop iterates through each number from 1 up to (but not including) table_length + 1, and for each iteration, it multiplies num by i and prints out the result using f-strings for better formatting.

Common Mistakes

  1. ### Forgetting to initialize num or table_length

In order to create a multiplication table, you need to set the values of both num and table_length. If you forget to do this, your program will not produce any output.

  1. ### Using an incorrect range() function argument

Make sure that the second argument in the range() function is less than table_length + 1 (or whatever number you're using as your multiplier and table length) to ensure that the loop iterates through all the necessary numbers. For example, if you set num = 5 and table_length = 10, you should use range(1, table_length + 1).

  1. ### Misaligning the print statements or not using f-strings for better formatting

Ensure that your print statements are properly aligned for better readability. In our example, we have used spaces to separate the multiplier, multiplicand, and result, and f-strings for better formatting.

  1. ### Not handling edge cases (e.g., when table_length is less than 1)

If you set table_length to a value less than 1, your program will not produce the desired output. Make sure to handle such edge cases by checking if table_length is greater than or equal to 1 before starting the loop.

Practice Questions

  1. Write a Python program to display the multiplication table of 9 for a table length of 12.
  2. Modify the given code to create a multiplication table up to 15 times the multiplier (instead of 10).
  3. Create a multiplication table for an input number that is provided by the user and ask the user to specify the table length.
  4. (Advanced) Write a Python program that generates all possible multiplication tables (up to a specified maximum number) for numbers from 1 to 20.

FAQ

### Why do we use range(1, table_length + 1) instead of range(0, table_length)?

We use range(1, table_length + 1) because we want to start from 1 and go up to (but not include) table_length + 1. Using range(0, table_length) would result in an extra iteration for the zero multiplication result, which is unnecessary in this case.

### Can I create a multiplication table for numbers other than integers?

Yes, you can create a multiplication table for floating-point numbers as well. However, keep in mind that the results will also be floating-point numbers, and the table might not be as clean or easy to read due to decimal points.

### How can I create a multiplication table for all numbers up to 100?

To create a multiplication table for all numbers up to 100, you'll need to use a larger range in your loop. For example:

num = 5
table_length = 100

for i in range(1, table_length + 1):
print(f"{num} x {i} = {num * i}")

This code will create a multiplication table for numbers up to 100. Keep in mind that this might take longer to run due to the larger number of iterations.

Display the multiplication Table (Python Programming) | Python | XQA Learn