How to format a multiplication table in python

Gnibbler's approach is quite elegant. I went for the approach of constructing a list of list of integers first, using the range function and taking advantage of the step argument.

for n = 12

import pprint
n = 12
m = list[list[range[1*i,[n+1]*i, i]] for i in range[1,n+1]]
pprint.pprint[m]
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
 [2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24],
 [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36],
 [4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48],
 [5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60],
 [6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72],
 [7, 14, 21, 28, 35, 42, 49, 56, 63, 70, 77, 84],
 [8, 16, 24, 32, 40, 48, 56, 64, 72, 80, 88, 96],
 [9, 18, 27, 36, 45, 54, 63, 72, 81, 90, 99, 108],
 [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120],
 [11, 22, 33, 44, 55, 66, 77, 88, 99, 110, 121, 132],
 [12, 24, 36, 48, 60, 72, 84, 96, 108, 120, 132, 144]]

Now that we have a list of list of integers that is in the form that we want, we should convert them into strings that are right justified with a width of one larger than the largest integer in the list of lists [the last integer], using the default argument of ' ' for the fillchar.

max_width = len[str[m[-1][-1]]] + 1
for i in m:
    i = [str[j].rjust[max_width] for j in i]
    print[''.join[i]]

   1   2   3   4   5   6   7   8   9  10  11  12
   2   4   6   8  10  12  14  16  18  20  22  24
   3   6   9  12  15  18  21  24  27  30  33  36
   4   8  12  16  20  24  28  32  36  40  44  48
   5  10  15  20  25  30  35  40  45  50  55  60
   6  12  18  24  30  36  42  48  54  60  66  72
   7  14  21  28  35  42  49  56  63  70  77  84
   8  16  24  32  40  48  56  64  72  80  88  96
   9  18  27  36  45  54  63  72  81  90  99 108
  10  20  30  40  50  60  70  80  90 100 110 120
  11  22  33  44  55  66  77  88  99 110 121 132
  12  24  36  48  60  72  84  96 108 120 132 144

and demonstrate the elasticity of the spacing with a different size, e.g. n = 9

n=9
m = list[list[range[1*i,[n+1]*i, i]] for i in range[1,n+1]]
for i in m:
    i = [str[j].rjust[len[str[m[-1][-1]]]+1] for j in i]
    print[''.join[i]]

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

In the program below, we have used the for loop to display the multiplication table of 12.

Source Code

# Multiplication table [from 1 to 10] in Python

num = 12

# To take input from the user
# num = int[input["Display multiplication table of? "]]

# Iterate 10 times from i = 1 to 10
for i in range[1, 11]:
   print[num, 'x', i, '=', num*i]

Output

12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120

Here, we have used the for loop along with the range[] function to iterate 10 times. The arguments inside the range[] function are [1, 11]. Meaning, greater than or equal to 1 and less than 11.

We have displayed the multiplication table of variable num [which is 12 in our case]. You can change the value of num in the above program to test for other values.

Goodness Chidinma Abarugo

Overview

In Python, we can create a multiplication table for any number by combining the input[] and range[] functions with a loop statement.

The input[] function

The input[] function is used to accept input from the user. If the required data type is not explicitly defined, any value provided by the user at the prompt is stored in memory as a string.

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 allows us to create a series of numbers automatically. We iterate over the series while executing some lines of code until we arrive at the last 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:

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.

Code using a for loop

We’ll use the following code to generate a multiplication table using the for loop.

ourNum = int[input["Enter the number you want to generate a multiplication table for, then hit the `enter` key: "]]
ourRange = range[1,6]
for x in ourRange:
    result = ourNum * x
    print[ourNum," * ",x," = ",result]

Multiplication table using the for loop

Explanation

Let’s explain our code:

  • In 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[].

  • In line 2, we define ourRange which includes numbers from 1 to 5.

  • Next, in line 3, we initiate our for loop and define x as a variable to hold the items stored in the ourRange variable.

  • In 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.

  • In 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.

Output

Enter the number you want to generate a multiplication table for, then hit the enter key:7 
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35

Note: We require an integer from the user during code execution. Therefore, we’ll input 7 as the number we want to create a multiplication table for. In our output, the loop terminates after multiplying by 5 instead of 6. This is because the last item in a range is not used during code execution.

Code using a while loop

We’ll use the code below to generate a multiplication table using a while loop.

ourNum = int[input["Enter the number you want to generate a multiplication table for, then hit the `enter` key: "]]
p = 1
while p < 6:
    result = ourNum * p
    print[ourNum, " * ", p," = ",result] 
    p = p + 1

Multiplication table using the while loop

Explanation

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

  • In line 1, we request an integer from the user.

  • In line 2, we set p as the variable that holds our counter, and set its initial value to 1.

  • In line 3, we state that the code block within our while loop will only execute as long as p is less than 6.

  • In line 4, we get the product of the number we input and the current number in the series.

  • In line 5, we display a row of our multiplication table.

  • In line 6, we increase our counter by 1.

Output

Enter the number you want to generate a multiplication table for, then hit the enter key:7 
7 * 1 = 7
7 * 2 = 14
7 * 3 = 21
7 * 4 = 28
7 * 5 = 35

Note: Our integer here is 7. We start our counter p from 1 and continue to increase it by 1 until we get a number that is equal to, or greater than 6.

CONTRIBUTOR

Goodness Chidinma Abarugo

How do you make a multiplication table in Python?

Code using a for loop.
ourNum = int[input["Enter the number you want to generate a multiplication table for, then hit the `enter` key: "]].
ourRange = range[1,6].
for x in ourRange:.
result = ourNum * x..
print[ourNum," * ",x," = ",result].

How do you make a multiplication table from 1 to 10 in Python?

Method 1: Using For loop.
number = int[input ["Enter the number of which the user wants to print the multiplication table: "]].
# We are using "for loop" to iterate the multiplication 10 times..
print ["The Multiplication Table of: ", number].
for count in range[1, 11]:.
print [number, 'x', count, '=', number * count].

How do you print a table pattern in Python?

Steps to Print Pattern in Python Accept the number of rows from a user using the input[] function to decide the size of a pattern. Next, write an outer loop to Iterate the number of rows using a for loop and range[] function. Next, write the inner loop or nested loop to handle the number of columns.

Chủ Đề