How to create a multiplication table for any number in Python

Key takeaways:

  • The input() function gathers user input as a string, which can be converted into integers for calculations.

  • The range() function generates a sequence of numbers, useful for iteration in loops like for and while.

  • A for loop is ideal for iterating over a specific range of numbers, whereas a while loop requires a condition and counter for controlling iterations.

  • Both loops can be used to generate multiplication tables, with the choice of loop depending on the structure of the problem.

Learning to create a multiplication table in Python is an excellent starting point for mastering loops and automation. With Python, you can quickly generate tables for any number and streamline your calculations.

Multiplication table in Python

To create a multiplication table for any number, we can combine two methods: the input() and range() functions with a loop statement.

The input() function

The input() function in Python is used to take input from the user. By default, the value entered by the user is stored as a string, unless a specific data type is defined.

Syntax

input(prompt)

Parameter

  • prompt: A string enclosed in single or double quotes. Its presence makes our code more interactive. It is an optional parameter.

The range() function

The range() function generates a sequence of numbers automatically. We can loop through this sequence, running specific code for each number until we reach the final number in the series.

Syntax

range(start, stop, step)

Parameters

  • start: The first number in the series. It is an optional parameter.

  • stop: The last number in the series. It is a required parameter.

  • step: The step size by which we want to increment or decrement our series. It is an optional parameter.

Note: If only one parameter is specified, that parameter is classified as the stop parameter, while the start and step parameters are taken as 0 and 1, respectively.

Loops

Loops are useful when we want to execute a line or block of code more than once, provided a condition is met or until we hit the last value in a series.

We’ll use for and while loops in this task. There is a slight difference between the for and while loop syntax. The for loop works within a range, and the while loop works only when one or more conditions are met.

Syntax

# for loop
for x in series:
   Do something

# while loop
while condition is met:
    Do something

Create the multiplication table

We’ll create our multiplication table based on the flowchart below:

Creating the multiplication table

Our flowchart above translates into the following algorithm:

  1. Start the program.
  2. Get an integer input from the user.
  3. Next, we’ll define a range or a condition. The range will be used in the for loop, and the condition will be used in the while loop.
  4. Finally, the code will test our item or condition. The code within the loop will continue to execute until items are outside the range or conditions are not met.

1. Using a for loop to generate a multiplication table

Let’s explore the following code to generate a multiplication table using the for loop.

tableNum = int(input("Enter a number to generate its multiplication table: "))
tableSize = range(1,8)
for x in tableSize:
result = tableNum * x
print(tableNum," * ",x," = ",result)

Enter the input below

Explanation

Let’s explain our code:

  • Line 1: We request a number from the user. The multiplication table will be created for this number. We convert the number to an integer data type by enclosing our input() in an int().

  • Line 2: We define tableSize which includes numbers from 1 to 8.

  • Line 3: We initiate our for loop and define x as a variable to hold the items stored in the tableSize variable.

  • Line 4: We use the result variable to hold the value of the product of the user-given number and the current item in the range.

  • Line 5: We display the user-given number, a multiplication sign, the current item in the series, an equals sign, and the value held by the result variable in each iteration.

2. Using a while loop to generate a multiplication table

Let’s explore the following code to generate a multiplication table using a while loop.

tableNum = int(input("Enter a number to generate its multiplication table: "))
p = 1
while p < 8:
result = tableNum * p
print(tableNum, " * ", p," = ",result)
p = p + 1

Enter the input below

Explanation

Unlike the for loop, our while loop needs a counter to make its iteration.

  • Line 1: We request an integer from the user.

  • Line 2: We set p as the variable that holds our counter, and set its initial value to 1.

  • Line 3: We state that the code block within our while loop will only execute as long as p is less than 8.

  • Line 4: We get the product of the number we input and the current number in the series.

  • Line 5: We display a row of our multiplication table.

  • Line 6: We increase our counter by 1.

Become a Python developer with our comprehensive learning path!

Ready to kickstart your career as a Python Developer? Our “Become a Python Developer” path is designed to take you from your first line of code to landing your first job.

This comprehensive journey offers essential knowledge, hands-on practice, interview preparation, and a mock interview, ensuring you gain practical, real-world coding skills. With our AI mentor by your side, you’ll overcome challenges with personalized support, building the confidence needed to excel in the tech industry.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How to write a program to print the multiplication table of a given number?

The following code is used to print the multiplication table of a given number:

tableNum = int(input("Enter the input number"))
tableSize = range(1,8)
for x in tableSize:
    result = tableNum * x
    print(tableNum," * ",x," = ",result)

How do I multiply numbers in Python?

You can use the * operator to multiply numbers:

result = num1 * num2

How to display tables in Python?

To display tables, use loops or libraries (such as pandas, PrettyTable, etc.) for more advanced formatting:

from prettytable import PrettyTable

num = int(input("Enter the number: "))
table = PrettyTable(["Multiplication", "Result"])
for i in range(1, 11):
    table.add_row([f"{num} x {i}", num * i])
print(table)


Free Resources