Python Multiplication Operator

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!


The * operator on integer operands yields another integer—the mathematical product of both operands:

>>> 2 * 2
>>> 2 * 3
>>> 2 * 4
>>> 16 * 16

If at least one of the operands is a float value, the result is also a float—float is infectious!

>>> 2.0 * 44
>>> 4 * 2.2
>>> 2.2 * -2.2

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: self and 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

# 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 x and 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
>>> ((x * y) * z)

Arithmetic Operators

Arithmetic operators are syntactical shortcuts to perform basic mathematical operations on numbers.

+AdditionCalculating the sum of the two operands3 + 4 == 7
SubtractionSubtracting the second operand from the first operand4 - 3 == 1
*MultiplicationMultiplying the first with the second operand3 * 4 == 12
/DivisionDividing the first by the second operand3 / 4 == 0.75
%ModuloCalculating the remainder when dividing the first by the second operand7 % 4 == 3
//Integer Division, Floor DivisionDividing the first operand by the second operand and rounding the result down to the next integer8 // 3 == 2
**ExponentRaising the first operand to the power of the second operand2 ** 3 == 8