In the program below, we have used the for loop to display the multiplication table of 12. Show Nội dung chính
Nội dung chính
Source Code
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 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 loopIn 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 LoopIn 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. ConclusionIn 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
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
and demonstrate the elasticity of the spacing with a different size, e.g.
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.. |