Multiplication Table in Python
Explore the application of Python loops like for loop and while loop through this article, focusing on creating multiplication tables in Python. Gain insights into various methods for creating multiplication tables, enhancing your proficiency with loops in Python.
Print Multiplication Table in Python
In the module below, we shall dive deep into the source code to create a multiplication table in python. We can modify the source code and implement the multiplication table for any number. We will be discussing multiple methods for doing the same using python.
The examples we shall be looking forward are:
Multiplication Table in Python Using For loop
Let us understand by the below example how we can create multiplication tables in python using them for a loop.
Code:
Output:
Explanation: Here we are making the use of for loop, helping us print the multiplication table of 4. We first ask the user to input the number for which we want the table to be printed, and then we iterate it ten times by range() function in for loop. The arguments we have provided inside the range() function are from (1, 11), which means that greater than or equal to 1 and less than 11 will be 10. We also have given the num as user input, but we can display it as any number if we don't want to ask the user for it.
Multiplication Table in Python Using While Loop
Let us understand by the below example how we can create multiplication tables in python using the while loop.
Code:
Output:
Explanation: Here we have used the while loop to print the multiplication table of 3 ( in our case). We know that while loop works until the condition specified by us holds true, which in our case is up till 10. So the while loop will work for ten times, and hence the multiplication table till ten times will get printed.
What Function is Used for Multiplication in Python?
We can create a function multiply using def to obtain the product of two numbers.
Additionally, in Python, you can multiply two equal lists using the zip() function, and the results will be appended into a new list. The elements of one list are multiplied by the corresponding elements in the other list:
Python provides the math.prod function for multiplying all the elements of a list. Here's an example:
Conclusion
- We used concepts of for loop and while loop to print the multiplication table.
- You need to be well versed with For loop, While loop, Input, Output and Import in python.