Table of Contents

## Python Multiplication Table For Loop

To calculate the multiplication table for a given `number`

, iterate over all values `i=0, 1, ..., limit`

in a for loop and use the following statement as a loop body: `print(number, 'x', i, '=', number * i)`

. This prints all equations, line by line, in the form `i x j = k`

.

The following code prints the multiplication table for 11, i.e., `0x11, 1x11, ..., 9x11`

to an upper limit of 10 (excluded):

# Calculate Multiplication Table # all multiples of this number number = 11 # up to this number, excluded limit = 10 for i in range(limit): print(number, 'x', i, '=', number * i)

The output is as follows:

11 x 0 = 0 11 x 1 = 11 11 x 2 = 22 11 x 3 = 33 11 x 4 = 44 11 x 5 = 55 11 x 6 = 66 11 x 7 = 77 11 x 8 = 88 11 x 9 = 99

The code performs the following steps:

- Set
`number=11`

for which the multiplication table should be calculated. - Set
`limit=10`

to restrict the number of consecutive factors to 0, 1, …, 9. - Iterate over all factors 0, 1, …, 9 using a for loop.
- In the loop body, print the multiplication equation and its result using a comma-separated list in the
`print()`

statement.

But what if you’re required to use a while loop to print the multiplication table? Let’s modify our program accordingly!

## Python Multiplication Table While Loop

To calculate the multiplication table for a given `number`

, iterate over all values `i=0, 1, ..., limit`

in a while loop and use the following statement as a loop body: `print(number, 'x', i, '=', number * i)`

. This prints all equations, line by line, in the form `i x j = k`

.

The following code prints the multiplication table for 11, i.e., `0x11, 1x11, ..., 9x11`

to an upper limit of 10 (excluded):

# Calculate Multiplication Table # all multiples of this number number = 11 # up to this number, excluded limit = 10 # set loop variable i = 0 while i<limit: print(number, 'x', i, '=', number * i) i += 1

Again, the output is as follows:

11 x 0 = 0 11 x 1 = 11 11 x 2 = 22 11 x 3 = 33 11 x 4 = 44 11 x 5 = 55 11 x 6 = 66 11 x 7 = 77 11 x 8 = 88 11 x 9 = 99

The code performs the following steps:

- Set
`number=11`

for which the multiplication table should be calculated. - Set
`limit=10`

to restrict the number of consecutive factors to 0, 1, …, 9. - Iterate over all factors 0, 1, …, 9 using a while loop by explicitly defining a loop variable
`i`

. - In the loop body, print the multiplication equation and its result using a comma-separated list in the
`print()`

statement. Then increase the loop variable using the inline addition operator`i += 1`

.

## Python Multiplication Table Nested For Loop

You can create a full multiplication table where cell `(i,j)`

corresponds to the product `i*j`

by using a nested for loop as follows:

number = 10 for i in range(number): print() for j in range(number): print(i*j, end='\t')

The output is the full multiplication table:

0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 0 2 4 6 8 10 12 14 16 18 0 3 6 9 12 15 18 21 24 27 0 4 8 12 16 20 24 28 32 36 0 5 10 15 20 25 30 35 40 45 0 6 12 18 24 30 36 42 48 54 0 7 14 21 28 35 42 49 56 63 0 8 16 24 32 40 48 56 64 72 0 9 18 27 36 45 54 63 72 81

**Explanation**: The code iterates in a nested for loop over each cell. Cell `(i,j)`

in this table corresponds to the product `i*j`

. After each cell, we add a tabular character `'\t'`

as the `end`

argument of the `print()`

function. After each line, we print an empty line using the empty `print()`

function.

## Python Multiplication Table List Comprehension

You can create a full multiplication table where cell `(i,j)`

corresponds to the product `i*j`

by using a nested for loop, or better yet, a list comprehension statement as follows:

number = 10 for i in range(number): print(*[j*i for j in range(number)], sep='\t')

The output is the full multiplication table:

0 0 0 0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9 0 2 4 6 8 10 12 14 16 18 0 3 6 9 12 15 18 21 24 27 0 4 8 12 16 20 24 28 32 36 0 5 10 15 20 25 30 35 40 45 0 6 12 18 24 30 36 42 48 54 0 7 14 21 28 35 42 49 56 63 0 8 16 24 32 40 48 56 64 72 0 9 18 27 36 45 54 63 72 81

Cell `(i,j)`

in this table corresponds to the product `i*j`

.

**Explanation**: The code iterates in a for loop over each line. It then generates the multiplication results line-wise in the list comprehension expression `[j*i for j in range(number)]`

. This list is unpacked into the `print()`

function using the asterisk prefix `*`

. All values are separated using a tabular character `'\t'`

in the separator argument of the `print()`

function.

While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.

To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.

His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.