The following code is used to print the multiplication table of a given number:
tableNum = int(input("Enter the input number"))
tableSize = range(1,8)
for x in tableSize:
result = tableNum * x
print(tableNum," * ",x," = ",result)
Key takeaways:
The
input()
function gathers user input as a string, which can be converted into integers for calculations.The
range()
function generates a sequence of numbers, useful for iteration in loops likefor
andwhile
.A
for
loop is ideal for iterating over a specific range of numbers, whereas awhile
loop requires a condition and counter for controlling iterations.Both loops can be used to generate multiplication tables, with the choice of loop depending on the structure of the problem.
Learning to create a multiplication table in Python is an excellent starting point for mastering loops and automation. With Python, you can quickly generate tables for any number and streamline your calculations.
To create a multiplication table for any number, we can combine two methods: the input()
and range()
functions with a loop statement.
input()
functionThe input()
function in Python is used to take input from the user. By default, the value entered by the user is stored as a string, unless a specific data type is defined.
input(prompt)
prompt
: A string enclosed in single or double quotes. Its presence makes our code more interactive. It is an optional parameter.range()
functionThe range()
function generates a sequence of numbers automatically. We can loop through this sequence, running specific code for each number until we reach the final number in the series.
range(start, stop, step)
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 thestart
andstep
parameters are taken as0
and1
, respectively.
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.
# for loop
for x in series:
Do something
# while loop
while condition is met:
Do something
We’ll create our multiplication table based on the flowchart below:
Our flowchart above translates into the following algorithm:
range
or a condition. The range
will be used in the for
loop, and the condition will be used in the while
loop.for
loop to generate a multiplication tableLet’s explore the following code to generate a multiplication table using the for
loop.
tableNum = int(input("Enter a number to generate its multiplication table: "))tableSize = range(1,8)for x in tableSize:result = tableNum * xprint(tableNum," * ",x," = ",result)
Enter the input below
Let’s explain our code:
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()
.
Line 2: We define tableSize
which includes numbers from 1 to 8.
Line 3: We initiate our for
loop and define x
as a variable to hold the items stored in the tableSize
variable.
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.
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.
while
loop to generate a multiplication tableLet’s explore the following code to generate a multiplication table using a while
loop.
tableNum = int(input("Enter a number to generate its multiplication table: "))p = 1while p < 8:result = tableNum * pprint(tableNum, " * ", p," = ",result)p = p + 1
Enter the input below
Unlike the for
loop, our while
loop needs a counter to make its iteration.
Line 1: We request an integer from the user.
Line 2: We set p
as the variable that holds our counter, and set its initial value to 1
.
Line 3: We state that the code block within our while
loop will only execute as long as p
is less than 8
.
Line 4: We get the product of the number we input and the current number in the series.
Line 5: We display a row of our multiplication table.
Line 6: We increase our counter by 1
.
Become a Python developer with our comprehensive learning path!
Ready to kickstart your career as a Python Developer? Our “Become a Python Developer” path is designed to take you from your first line of code to landing your first job.
This comprehensive journey offers essential knowledge, hands-on practice, interview preparation, and a mock interview, ensuring you gain practical, real-world coding skills. With our AI mentor by your side, you’ll overcome challenges with personalized support, building the confidence needed to excel in the tech industry.
Haven’t found what you were looking for? Contact Us