Arithmetic operators in Python


Introduction

In this blog, you will learn about arithmetic operators in python. Operators are used for doing operations on variables and values. It can manipulate operands and returns a new result.  

For example;

>>> num1 = 10
>>> num2 = 20
>>> total = num1 + num2
>>> print(total)
30

In the above example, num1 and num2 are variable holding integer value 10 and 20 respectively. The operator used is an addition arithmetic operator to add two values and the result is 30.

So, basically, you will learn seven different types of arithmetic operators used in python. And it is particularly used for mathematical computations. Here are the seven different types of operators;

1. Addition (+)

Adds values on either side of the operators.

Example:

# Addition operation on variables
>>> num1 = 10
>>> num2 = 20
>>> total = num1 + num2
>>> print(total)
30
# Addition operation on values
>>> print(50 + 2)
52
2. subtraction (-)

Subtracts the right-hand operand from the left-hand operand.

Example:

# Subtraction on variables
>>> num1 = 78
>>> num2 = 34
>>> sub = num1 - num2
>>> print(sub)
44
# Subtraction on values
>>> print(67 - 23)
44
3. Division (/)

Divides left-hand operand by right-hand operand. It always returns a floating value.

Example:

# Division on variables
>>> a = 4
>>> b = 2
>>> div = a / b
>>> print(div)
2.0
# Division on values
>>> print(5/2)
2.5


4. Floor division (//)

The division of operands where the result is the quotient in which the digits after the decimal point are neglected or removed. The floor division returns an integer value.

Example:

# Floor division on variables
>>> a = 4
>>> b = 2
>>> fdiv = a // b
>>> print(fdiv)
2
# Floor division on values
>>> print(5//2)
2

But, if any or all operands are in floating-point, the floor value will be also in float number.

Example:

>>> a = 4.0
>>> b = 2
>>> print(a//b)
2.0
5. Multiplication (*)

Multiplies values on either side of the operator.

Example:

>>> a = 4
>>> b = 2
>>> mul = a * b
>>> print(mul)
8
6. Exponents (**)

Performs exponential (power) calculation. To represent power you have to use two * signs.

Example:

>>> a = 4
>>> b = 2
>>> exp = a ** b  # 4 to the power of 2
>>> print(exp)
16


7. Modulus (%)

Divides left-hand operand by right-hand operand and returns the remainder. Use percentage (%) for modulus.

Example:

>>> a = 5
>>> b = 2
>>> mod = a % b
>>> print(mod)
1

Sample example on arithmetic operators: Simple calculator program
# simple calculator to demonstrate arithmetic operators in python
num1 = float(input("Enter num1:"))
num2 = float(input("Enter num2:"))
add = num1 + num2  # Addition 
sub = num1 - num2  # Subtraction
mul = num1 * num2  # Multiplication
div = num1 / num2  # division
floor_div = num1 // num2  # Floor Division
power = num1 ** num2  # Power Operation
mod = num1 % num2  # Modulus
print("*****************")
print(f"Addition of {num1} and {num2}: {add}")
print(f"Subtraction of {num1} and {num2}: {sub}")
print(f"Multiplication of {num1} and {num2}: {mul}")
print(f"Division of {num1} and {num2}: {div}")
print(f"Floor Division of {num1} and {num2}: {floor_div}")
print(f"Power operation of {num1} and {num2}: {power}")
print(f"modulus of {num1} and {num2}: {mod}")

Output:

Enter num1:13
Enter num2:7
*****************
Addition of 13.0 and 7.0: 20.0
Subtraction of 13.0 and 7.0: 6.0
Multiplication of 13.0 and 7.0: 91.0
Division of 13.0 and 7.0: 1.8571428571428572
Floor Division of 13.0 and 7.0: 1.0
Power operation of 13.0 and 7.0: 62748517.0
modulus of 13.0 and 7.0: 6.0
Summary

Congratulations! now you are familiar with all seven arithmetic operators; addition, subtraction, division, multiplication, floor division, exponents, and modulus.



Python language supports the following operators: