Python’s multiplication operator
* multiplies two objects. The semantics of the multiplication depends on the operands’ data types. For example, multiplying two integers performs arithmetic multiplication whereas multiplying a list with an integer performs list concatenation. The specific return value of the multiplication operator is defined in a data types’
__mul__() magic method.
Have a look at the following examples!
* operator on integer operands yields another integer—the mathematical product of both operands:
>>> 2 * 2 4 >>> 2 * 3 6 >>> 2 * 4 8 >>> 16 * 16 256
If at least one of the operands is a float value, the result is also a float—float is infectious!
>>> 2.0 * 44 88.0 >>> 4 * 2.2 8.8 >>> 2.2 * -2.2 -4.840000000000001
We can also multiply a string with an integer that performs string concatenation, concatenating the same string n times assuming n is the second integer operand:
>>> 'boom! ' * 10 'boom! boom! boom! boom! boom! boom! boom! boom! boom! boom! '
In fact, you can multiply other sequence types with integers as well such as lists. The result is the same sequence appended to itself n times. The original lists remain unchanged.
>>> [1, 2] * 3 [1, 2, 1, 2, 1, 2] >>> ('Alice', 'Bob') * 5 ('Alice', 'Bob', 'Alice', 'Bob', 'Alice', 'Bob', 'Alice', 'Bob', 'Alice', 'Bob')
What if two operands have an incompatible data type—unlike floats and integers?
>>> 'hello' * 'hi' Traceback (most recent call last): File "<pyshell#11>", line 1, in <module> 'hello' * 'hi' TypeError: can't multiply sequence by non-int of type 'str'
The result of incompatible addition is a
TypeError. You can fix it by using only compatible data types for the operation.
Can you use the multiplication operator on custom objects? Yes!
Python Addition Magic Method
To use the multiplication operator on custom objects, define the
__mul__() “dunder” or magic method that takes two arguments:
other and returns the result of
self * other. The method can use the attributes from the two operand instances.
In the following code, you define the multiplication magic method on a custom data type called
Network that quantifies a value of a given network. If you combine two networks, the value multiplies!
class Network: def __init__(self, value): self.value = value def __mul__(self, other): return Network(self.value * other.value) # Create two networks: my_network = Network(2) your_network = Network(4) # Multiply two custom Network instances: our_network = my_network * your_network print(our_network.value) # 8
Consequently, the new Network created by multiplying your network with my network has value 2*4 = 8.
Python Multiplication Operator Chaining
You can chain together two multiplication operators. For example, the expression
x * y * z would first multiply
y and then multiply z with the result. Thus, it is semantically identical to
((x * y) * z).
Here’s a minimal example:
>>> x = 10 >>> y = 5 >>> z = 2 >>> x * y * z 100 >>> ((x * y) * z) 100
Arithmetic operators are syntactical shortcuts to perform basic mathematical operations on numbers.
|+||Addition||Calculating the sum of the two operands|
|--||Subtraction||Subtracting the second operand from the first operand|
|*||Multiplication||Multiplying the first with the second operand|
|/||Division||Dividing the first by the second operand|
|%||Modulo||Calculating the remainder when dividing the first by the second operand|
|//||Integer Division, Floor Division||Dividing the first operand by the second operand and rounding the result down to the next integer|
|**||Exponent||Raising the first operand to the power of the second operand|
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.