Python Arithmetic Operators

Python Arithmetic Operators perform the usual arithmetic operations like addition, subtraction, multiplication, division, etc. In python, arithmetic operators are in three forms. These are

  1. Unary Operators - These work on a single operand.
  2. Binary Operators - These work on two operands.
  3. Ternary Operators - These work on three operands. These are the conditional operators.

Python Unary Arithmetic Operators

Operators Operations Examples
Unary plus (+) It gives the value of the operand Let x = 15
Then +x means 15
Let x = -15
Then +x means -15
Unary Minus (-) It results in the negated value of the operand Let x = 15
Then -x means -15
Let x = -15
Then -x means 15

Binary Arithmetic Operators

Operators Operations Examples
Addition (+) Returns the sum of operands 1 + 2 = 3
5.0 + 3 = 8.0
2.5 + 3.5 = 6.0
"A" + "B" = "AB"
[5,6,7] + [8,9] = [5,6,7,8,9]
(1,2,3,4) + ('Bus','Truck') = (1,2,3,4,'Bus','Truck')
Subtraction (-) Subtracts the 2nd operand from the 1st operand 6 - 5 = 1
1.0 - 3 = -2.0
2.5 - 3.5 = -1.0
Division (/) Divides the 1st operand with 2nd operand and returns the quotient 10 / 4 = 2.5
10.0 / 5 = 2.0
1.0 / 0.3 = 3.3333333333333335
Modulus (%) Divides the 1st operand with 2nd operand and returns the remainder 5 % 2 = 1
10.0 % 4 = 2.0
12.0 % 7.0 = 5.0
Multiplication (*) Returns the product of the two operands 2 * 3 = 6
-2.2 * 2 = -4.4
6.2 * 4.5 = 27.900000000000002
"Hello"*2 = “HelloHello”
Exponent (**) Returns the result as 1st operand raised to the power of 2nd operand 5**2 = 25
2.5**3 = 15.625
10.5**3.2 = 1852.7027964066515
Truncated Division(//) Divides the 1st operand with 2nd operand and returns the whole part of quotient 10 // 4 = 2
10.25 // 4 = 2.0
-10 // 6.0 = -1.0

If you notice in the above examples, the result of an operation is implicitly type-casted to the type of operand, which is bigger in terms of byte size. For example

5.0 * 2 = 10.0(float) ( not 10(int) )