The Python Multiplication (*) Operator is part of the 4 basic Arithmetic Operators found in Python. The Multiplication Operator is used in Python to perform mathematical operations together with the Addition (+), Subtraction (-) and Division (/) operators.
There are also 3 more Advanced Arithmetic Operators known as Modulus (%), Exponentiation (**) and Floor Division (//), making for a total of 7 Arithmetic Operators in Python.
The Arithmetic Operators form part of the other Python Operators collection, which includes the Assignment Operators, Comparison Operators, Logical Operators, Identity Operators, Membership Operators and Bitwise Operators.
NOTE: This tutorial requires that Python is already installed, as well as an IDE (Integrated Development Environment), such as PyCharm. It’s also recommended that you have a basic understanding of How to Use Python if this is your first time learning Python.
With that said, let’s get started with this free Python Multiplication Operator tutorial.
How to use the Multiplication Operator in Python
Let’s use the Python Multiplication Arithmetic Operator (*), to type in a basic multiplication equation such as “5*3” in the Python IDE.
If you “run” this code, you will see that nothing happens in the output window.
5 * 3
This is because Python requires a statement in order for it to know that it needs to output this equation.
The most common Statement is the Print Statement “print()”, so let’s use that and run the script again:
print(5 * 3) # “5” and “3” are the “Operands”, while “*” is the “Operator”
#>> 15 # “15” is the “Output”
Python outputs a value of 15, because in math “5 x 3 = 15”, meaning that we just created some basic math in Python using the Multiplication Arithmetic Operator, which is used to find the product (aka multiplication) of two or more values.
In this example:
- “5” and “3” are the Operands
- “*” is the Operator
- “15” is the Output.
Multiplication Operator using Float values in Python
The above example is done using Whole Numbers, which in Python is referred to as an Integer (int) Data Type.
We can however also make use of Fractional or Decimal values, which in Python are referred to as a Floating Point (float) Data Type. Let’s now subtract 2.88 (float) from 3.75 (float) using the Multiplication Arithmetic Operator.
print(3.64 * 2.88) # float * float values
#>> 10.4832 # outputs float value
Python outputs a value of “10.4832”, because in math, “3.64 * 2.88 = 10.4832”. As this output value is a decimal, it remains a Float Value in Python.
Arithmetic Float values are sometimes subject to a floating point errors. This is not the fault of Python but how computer binary works in general.
To illustrate this, let’s take a look as this seemingly very basic example of where we now multiply 3.75 (float) from 2.88 (float) using the Multiplication Arithmetic Operator:
print(3.75 * 2.88) # float * float values
#>> 10.799999999999999 # outputs float value
You will quickly notice the problem here, as “3.75 * 2.88” should equal “10.8” and not “10.799999999999999”.
To solve this Floating Point recurring issue, we can make use of the round() Function or the decimal.Decimal module.
Using the Round() Function to limit Multiplication decimal value to 1
While technically out of the scope of this basic introduction to the Multiplication Operator, if you do want to round the output off to one decimal value, it is possible using the round() Function and setting the amount of decimal places, using “, 1” after the sum:
print(round(3.75 * 2.88, 1)) # round() Function with a decimal limit of 2
#>> 10.8 # outputs a rounded off float value
Now we get a much cleaner, one decimal output of “10.8”, thanks to the round() Function.
As this output value is a decimal, it remains a Float Value in Python, regardless of whether we use the round function or not.
Using the decimal.Decimal module to calculate precise Floating Point numbers
In order to avoid Floating Point values such as “3.75 * 2.88” to equal “10.799999999999999”, we can make use of Python’s decimal.Decimal module to accurately calculate it to be “10.8000” instead.
from decimal import Decimal
print(Decimal("3.75") * Decimal("2.88"))
#>> 10.8000
Multiplication Operator using Float and Int values in Python
We can then also mix both int and float values as we please in Python, when using the Multiplication Operator:
print(36.75 * 21) # float x int value
#>> 771.75 # outputs float value
Python outputs a value of “771.75”, which is again a Float Value.
When using the Multiplication Arithmetic Operator, to multiply a float value with an int value, a float value will always be the output result.
Multiplication Operator using Multiple Float and Int values in Python
Multiple values of both int and float values can be used with the Python Multiplication Operator, in any combination:
print(3.75 * 204 * 64.252 * 5 * 78) # float x int x float x int x int
#>> 19169584.2 # outputs float value
Here again we see the output of a float (decimal) value. This is will always be the result when using the Multiplication Operator within a combination of multiple float and int values, even if the output is technically a Whole Number:
print(3.75 * 204 * 64.2 * 5 * 78) # float x int x float x int x int
#>> 19154070.0 # outputs float value
Even though the mathematical multiplication of “3.75 * 204 * 64.2 * 5 * 78 = 19154070”, (making 19154070 is a whole number), Python sees it as “19154070.0”, which is a decimal value and therefore a Float.
Python Expressions
All the above examples of using the Multiplication Operator are better known as Python Expressions, which is a combination of Operators and Operands (Integer or Floating Point numbers), which are then interpreted to produce an output value.
The Multiplication Operator can also be combined with the other Arithmetic Operators found in Python, to form an Arithmetic Expression. When this happens, an Expression is evaluated as per the Precedence of its operators.
print(-5 + -3 * 7 + 4)
#>> -22
Here the Multiplication Operator is given presence before the Addition or Subtraction operators.
Conclusion
I hope you enjoyed this tutorial on the Multiplication Arithmetic Operator in Python. This should give you the starting knowledge you need to easily implement it into your next Python code or project.
Python Multiplication Operator Summary
Here is a summary of what we learnt about the Python Multiplication operator:
- How to use the Multiplication Operator with Integer numbers in Python.
- How to use the Multiplication Operator with Floating Point numbers in Python.
- When using Floating Point values with the Multiplication operator in Python, you will sometimes get recurring decimal values such as “3.75 * 2.88” = “10.799999999999999”, which can be solved using the round() Function or the decimal.Decimal module.
- How to use the Multiplication Operator with a mix of both Integer and Floating Point numbers in Python.
- When using the Multiplication Operator with an Operand such as an Integer or Floating Point value, you are creating an Expression in Python. The Multiplication Operator can also be combined with the other Python Arithmetic Operator in an Expression, which is then calculated based on Precedence.
Share the love
If you enjoyed this tutorial content and would like others to benefit from it as well, make sure to share it on your favorite Social Media platform, using the share buttons below.
Subscribe for News & Discount Offers
Subscribe right now to gain instant access to the latest news and tutorials while also being the first to know about, exclusive offers and discounts!