Hướng dẫn dùng multiplication python python

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

Nội dung chính

  • Source Code
  • Method 1: Using For loop
  • Method 2: By using While Loop
  • How do you make a table in Python?
  • How do you print a table from 1 to 10 in Python?
  • How do you print a table in Python?

Nội dung chính

  • Source Code
  • Method 1: Using For loop
  • Method 2: By using While Loop
  • How do you make a table in Python?
  • How do you print a table from 1 to 10 in Python?
  • How do you print a table in Python?

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.

In Python, the user can write the program to display the multiplication table of any number. In this tutorial, we will discuss different methods for printing the multiplication table of any number using Python.

Method 1: Using For loop

In the following example, we will print the multiplication table of any number (from 1 to 10) by using the for loop.

Example:

Output:

Enter the number : 10
Enter the number of which the user wants to print the multiplication table:  13
The Multiplication Table of:  13
13 x 1 = 13
13 x 2 = 26
13 x 3 = 39
13 x 4 = 52
13 x 5 = 65
13 x 6 = 78
13 x 7 = 91
13 x 8 = 104
13 x 9 = 117
13 x 10 = 130

Explanation:

In the above program, we have taken an input integer number from the user. Then we have iterated for loop using the range (1, 11) function, which means greater than or equal to 1 and less than 11. In the first iteration, the loop will iterate and multiply by 1 to the given number. In the second iteration, 2 is multiplied by the given number, and so on.

In our case, we have printed the table of 10. You can provide the different numbers to test the program.

Method 2: By using While Loop

In this method, we will use the while loop for printing the multiplication table of any number specified by the user.

The following is the example for method 2:

Example:

Output:

Enter the number of which the user wants to print the multiplication table:  27
The Multiplication Table of:  27
27 x 10 = 27
27 x 10 = 54
27 x 10 = 81
27 x 10 = 108
27 x 10 = 135
27 x 10 = 162
27 x 10 = 189
27 x 10 = 216
27 x 10 = 243
27 x 10 = 270

Explanation:

The above code is the same as the previous program, but we have used the while loop. We declared a variable "count" and initialized it by 1. The while loop will iterate until the value of "count" is smaller than and equal to 10. Each time loop is iterated, the value of "count" will be incremented by 1. When the "count" became greater than 10, the loop will be terminated.

Conclusion

In this tutorial, we have discussed two different methods which can be used for printing the multiplication table of any number using 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

How do you make a table in Python?

Creating a table using python.

Establish connection with a database using the connect() method..

Create a cursor object by invoking the cursor() method on the above created connection object..

Now execute the CREATE TABLE statement using the execute() method of the Cursor class..

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

“python program to print multiplication table from 1 to 10” Code Answer's.

num = int(input('Which table you need write it here:')).

starting_range = int(input('write the number from where to start:')).

ending_range = int(input('write the end of the number you need:')).

print().

print('So This is the table of', num).

How do you print a table in Python?

How to Print Table in Python?.

Using format() function to print dict and lists..

Using tabulate() function to print dict and lists..

texttable..

beautifultable..

PrettyTable..