In this tutorial, we will learn how to multiply variables with numbers in the Python programming language.
Multiplication is one of the basic arithmetic operations that we regularly use in our day-to-day tasks. Python, like other programming languages, allows you to perform these calculations with its built-in function.
Step 1: Basic Multiplication
Firstly, we should understand how to carry out the multiplication operation in Python. This operation can be performed using the asterisk (*) symbol between two numbers. Let’s see the syntax below:
1 2 3 4 |
x = 5 y = 2 mul = x * y print(mul) |
In the output, you should see the result:
10
Step 2: Multiplying Variable with a Number
Suppose you have a variable and you want to multiply that with a number. You can do this in exactly the same way as we did in the previous step.
1 2 3 |
x = 3 mul = x * 5 print(mul) |
The output will give us:
15
Step 3: Multiplying Variables
Python also allows you to multiply two different variables. Look at the syntax below to do that:
1 2 3 4 |
x = 4 y = 5 mul = x * y print(mul) |
In the output, you will get:
20
Full code:
Now, let’s see the complete code that we discussed in the above steps.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
x = 5 y = 2 mul = x * y print(mul) x = 3 mul = x * 5 print(mul) x = 4 y = 5 mul = x * y print(mul) |
Conclusion
From this tutorial, you learned how easy it is to multiply variables and numbers in Python.
We started by understanding the basic multiplication operation in Python, then we learned how to multiply a variable with a number and finally, we saw the way to multiply two different variables.
I hope you found it insightful and that it added value to your Python coding skills.