# Python Multiplication Operator

5/5 - (1 vote)

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!

## Examples

The `*` 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!

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

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 `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
100
>>> ((x * y) * z)
100```

## Arithmetic Operators

Arithmetic operators are syntactical shortcuts to perform basic mathematical operations on numbers.
{"ft_lang_label":"__label__en","ft_lang_prob":0.806786,"math_prob":0.95942044,"size":4630,"snap":"2023-40-2023-50","text_gpt3_token_len":1162,"char_repetition_ratio":0.14029399,"word_repetition_ratio":0.02457956,"special_character_ratio":0.27343413,"punctuation_ratio":0.12589073,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99351454,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-26T13:27:24Z\",\"WARC-Record-ID\":\"<urn:uuid:21df9caf-41c8-4a71-954b-c9fb2cde4b58>\",\"Content-Length\":\"104057\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:9294ceb5-82cb-4eb1-8f79-81d8a9cff842>\",\"WARC-Concurrent-To\":\"<urn:uuid:4d957d57-401c-46ae-9f4d-a62c31df07d7>\",\"WARC-IP-Address\":\"194.1.147.89\",\"WARC-Target-URI\":\"https://blog.finxter.com/python-multiplication-operator/\",\"WARC-Payload-Digest\":\"sha1:MA76KN4SJ6UVGWPHJPUCP4DDKRAWSBJQ\",\"WARC-Block-Digest\":\"sha1:LSWRJOPX2IO5P6I7YWSMSV3CKSZHAGT6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233510208.72_warc_CC-MAIN-20230926111439-20230926141439-00237.warc.gz\"}"}