In Python, we can create a multiplication table for any number by combining the input()
and range()
functions with a loop statement.
input()
functionThe 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.
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 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.
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
loopWe’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 * xprint(ourNum," * ",x," = ",result)
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.
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 by5
instead of6
. This is because the last item in a range is not used during code execution.
while
loopWe’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 = 1while p < 6:result = ourNum * pprint(ourNum, " * ", p," = ",result)p = p + 1
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
.
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 counterp
from1
and continue to increase it by1
until we get a number that is equal to, or greater than6
.