[Python Examples] 1. Creating a Multiplication Table

03 May 2024 By Code Bricks

Python Tutorial: Creating a Multiplication Table Program

Welcome to this fun Python tutorial! Today, we’re going to learn how to create a multiplication table program. Don’t worry if you’re new to coding, we’ll take it step-by-step.

What is a Multiplication Table?

A multiplication table is a table that shows the result of multiplying two numbers. For example, if you multiply 2 and 3, you get 6. The multiplication table helps you see the results of different multiplications quickly.

Let’s Start Coding!

Step 1: Open Your Python Environment

First, make sure you have Python installed on your computer. You can use an environment like IDLE, or any other code editor. Open it up, and let’s start coding!

Step 2: Print a Single Multiplication

Let’s start by printing a single multiplication result. Type the following code:

# This is a simple multiplication
print(2 * 3)

When you run this code, you should see 6 printed on the screen.

Step 3: Using a Loop to Print a Table

Now, let’s use a loop to print the multiplication table for a number. A loop helps us repeat actions without writing the same code again and again. Type the following code to print the multiplication table for the number 2:

# This prints the multiplication table for 2
for i in range(1, 10):  # Loop from 1 to 9
    print(2, '*', i, '=', 2 * i)

When you run this code, you will see:

2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
2 * 7 = 14
2 * 8 = 16
2 * 9 = 18

Step 4: Multiplication Table for Any Number

Let’s make our program a bit more flexible. We can ask the user for a number and print the multiplication table for that number. Type the following code:

# Ask the user for a number
number = int(input("Enter a number: "))

# Print the multiplication table for the entered number
for i in range(1, 10):
    print(number, '*', i, '=', number * i)

When you run this code, it will ask you to enter a number. After you enter a number, it will print the multiplication table for that number.

Step 5: Multiplication Tables from 1 to 9

Finally, let’s print the multiplication tables from 1 to 9. We will use two loops, one inside the other (nested loops). Type the following code:

# Print multiplication tables from 1 to 9
for number in range(1, 10):  # Loop for numbers 1 to 9
    for i in range(1, 10):  # Loop for each multiplication
        print(number, '*', i, '=', number * i)
    print()  # Print a blank line between tables

When you run this code, it will print the multiplication tables from 1 to 9.

1 * 1 = 1
1 * 2 = 2
1 * 3 = 3
...
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81

Congratulations!

You’ve just created a multiplication table program in Python! Great job! Keep practicing and have fun coding!