23. Mathematical Operators#

We do have a big set of mathematical operators in Python. letโ€™s have a look ๐Ÿ˜Ž

Letโ€™s start with Addition, which is the only one I am good at ๐Ÿ˜œ JD!

Operator

Operator Symbol

Addition

+

Subtraction

-

Multiplication

*

Division

/

Floor Division

//

Modulus

%

Exponentiation/Power

**

Hurray! I am recapping my primary school ๐Ÿ‘ฆ:P

Success Kid

23.1. Addition#

Addition operator + is used to add two or more bool, int, float, complex, list, tuple, str etc..

๐Ÿ™„ If you are wondering does addition operator + does a lot of stuff mentioned above? Yep, it does, isnโ€™t that cool ๐Ÿ˜Ž? Thatโ€™s the flexibility provided by our beloved Python ๐Ÿ.

Letโ€™s go through each example ๐Ÿ˜Š

# Let's create a few attributes of different datatypes.

# Integers.
integer_a: int = 2
integer_b: int = 6

# Floats.
float_a: float = 1.4
float_b: float = 5.2

# complex.
complex_a: complex = 2 + 9j
complex_b: complex = 9 - 1j

# list.
list_a: list = [1, 2, 3]
list_b: list = [4, 5, 6]

# tuple.
tuple_a: tuple = (1, 2, 3)
tuple_b: tuple = (4, 5, 6)

# str.
str_a: str = "Hey "
str_b: str = "Pythonist ๐Ÿ"

Wohooo! We successfully created many attributes of different datatypes. Letโ€™s start using the Addition + operator on them

23.1.1. Integers#

print(integer_a + integer_b)
8

23.1.2. Floats#

print(float_a + float_b)
6.6

23.1.3. Complex#

print(complex_a + complex_b)
(11+8j)

Thereโ€™s no restriction on the operands type here, we can even add integers with floats, integers with complex, float with complex

# Integer with float.
int_with_float = integer_a + float_a
print(int_with_float)

# Integer with complex.
int_with_complex = integer_a + complex_a
print(int_with_complex)

# Float with complex
float_with_complex = float_a + complex_a
print(float_with_complex)
3.4
(4+9j)
(3.4+9j)

Letโ€™s check the datatype of the above results int_with_float, int_with_complex, โ€˜float_with_complex

print(f"{type(int_with_float)=}")
print(f"{type(int_with_complex)=}")
print(f"{type(float_with_complex)=}")
type(int_with_float)=<class 'float'>
type(int_with_complex)=<class 'complex'>
type(float_with_complex)=<class 'complex'>

23.1.3.1. ๐Ÿ’ก Note#

In the above print function we have used = in the f-strings, which prints the output as <expression>=<expression_result>.

In simple terms using = in the f-strings make the above print function equivalent to:

print(f"type(int_with_float)={type(int_with_float)}")
type(int_with_float)=<class 'float'>

๐Ÿ’ก We see that the results datatype is changed in the above cases with respect to addition of integer with complex and float with complex. Thanks to Python again, it does take care of result datatype dynamically.

The final result would be of datatype equal to the highest precedence of the operands shown as below.

complex > float > int

complex numbers are superset of float, float are superset to integers.

23.1.4. lists#

print(list_a + list_b)
[1, 2, 3, 4, 5, 6]

Addition operator on two lists generate a new list appending the objects present in the second list to first list.

23.1.5. Tuples#

print(tuple_a + tuple_b)
(1, 2, 3, 4, 5, 6)

23.1.6. Strings#

print(str_a + str_b)
Hey Pythonist ๐Ÿ

Note

We do have a string method called join for efficient concatenation of strings. We would go through it in the coming chapters related to strings ๐Ÿ˜ฌ.

23.2. Subtraction#

23.2.1. Integers#

print(integer_a - integer_b)
-4

23.2.2. Floats#

print(float_a - float_b)
-3.8000000000000003

We might wonder seeing the above result -3.8000000000000003 instead of -3.8 and doubt ourselves whether we forgot how to do subtraction ๐Ÿ™„. No worries, We are still good at math (basic math atleast ๐Ÿ˜…).

The extra 3 at the end of the result is due to Floating Point Arithmetic Issues and Limitations

23.2.3. Complex#

print(complex_a - complex_b)
(-7+10j)

In the similar way subtraction can be done with a mix of integers, floats and complex values ๐Ÿ‘.

Note

Subtraction operation doesnโ€™t work with lists, tuples and dictionary. These datastructures have built-in methods associated to remove objects from them.

23.3. Multiplication#

print(integer_a * integer_b)
print(float_a * float_b)
print(complex_a * complex_b)
12
7.279999999999999
(27+79j)

Apart from the above numerical multiplication, we can even use multiplication for strings, list

23.3.1. Strings#

print(str_a * 5)
Hey Hey Hey Hey Hey 

Note

But multiplication of strings can be only done with a integer. Non-Integer multiplication of strings lead to TypeError ๐Ÿ™ˆ.

print(str_a * 5.5)  # Raises TypeError.
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[16], line 1
----> 1 print(str_a * 5.5)  # Raises TypeError.

TypeError: can't multiply sequence by non-int of type 'float'

23.3.2. List/Tuple#

# list
print(list_a * 2)

# tuple
print(tuple_a * 3)
[1, 2, 3, 1, 2, 3]
(1, 2, 3, 1, 2, 3, 1, 2, 3)

Multiplication of integer on list or tuples works similar, appending the same list/tuple objects in the same order equal to integer times multiplied.

23.4. Division#

Division operator only works for numerical attributes like integer, float and complex.

print(integer_a / integer_b)
print(float_a / float_b)
print(complex_a / complex_b)
0.3333333333333333
0.2692307692307692
(0.10975609756097561+1.0121951219512195j)

23.5. Floor division#

In the division section we have seen the results in fractions. Floor division operator // works similar, just that it neglects the fractional part. In Math terms, the result of Floor division is always the quotient.

print(integer_a // integer_b)
print(float_a // float_b)
print(
    5 // 2
)  # When divided result should be 2.5, but for floor division the result would be just 2.
0
0.0
2

Note

Pythonโ€™s default behaviour doesnโ€™t support Floor divison on complex numbers.

23.6. Modulus#

As we have already seen that Floor division results in Quotient of the a division, the modulus operator returns the remainder of the division. The Modulus operator is denoted by the symbol %.

print(integer_a % integer_b)
print(float_a % float_b)
2
1.4

23.7. Exponentiation/Power#

The Exponentiation/Power operator is denoted as **. Power operator on two operands x**y is said as the value of x raised to the power y.

print(integer_a**integer_b)
print(float_a**float_b)
print(complex_a**complex_b)
64
5.752621307139592
(-1611671639.8022103-929304789.2091311j)

Wohooo! Thatโ€™s too much of knowledge we acquired ๐Ÿ˜ฌ. The only way we can master anything is through build and bombard, feel free to test out various scenarios ๐Ÿ˜Š.