In childhood, we learned about addition, subtraction, multiplication, and other basic math operations. In Python, or any programming language, arithmetic operators are used to perform these basic mathematical calculations.
Table of Contents
1. Addition (+)
The addition operator (+
) is used to add two or more numbers or variables or operands.
Example:
# Simple Addition
print(5 + 3) # Output: 8
# Adding Variables
x = 10
y = 20
print(x + y) # Output: 30
# Multiple Value Addition
result = 5 + 10 + 15 + 20
print(result) # Output: 50
Python2. Subtraction (-)
The subtraction operator (-
) is used to subtract one number from another.
Example:
# Simple Subtraction
print(10 - 4) # Output: 6
# Subtracting Variables
x = 50
y = 20
print(x - y) # Output: 30
Pythonyou should notice one important thing here – x - y
gives 30
and not -30
which means we are subtracting from left value to right. This concept is called precedence where we understand how the operations being performed.
print(y - x) # Output: -30
# Multiple Value Subtraction
result = 100 - 20 - 10 - 5
print(result) # Output: 65
PythonHence the above code solve from left to right. First 100-20
solve which gives 80
then 80-10
solve which gives 70
then 70-5
solve which gives 65
.
3. Multiplication (*)
The multiplication operator (*
) is used to multiply two or more numbers or variables.
Example:
# Simple Multiplication
print(5 * 4) # Output: 20
# Multiplying Variables
x = 7
y = 6
print(x * y) # Output: 42
# Multiple Value Multiplication
result = 2 * 3 * 4
print(result) # Output: 24
Python4. Division (/)
The division operator (/
) is used to divide one number by another and always returns a float.
Example:
# Simple Division
print(10 / 2) # Output: 5.0
# Dividing Variables
x = 25
y = 5
print(x / y) # Output: 5.0
# Multiple Value Division
result = 20 / 5 / 2
print(result) # Output: 2.0
PythonHere, division is performed from left to right. For 20 / 5 / 2, first 20 / 5 is performed, which gives 4
then 4 / 2
is performed. Hence, precedence is from left to right.
5. Floor Division (//)
The floor division operator (//
) divides two numbers and returns the whole number.
Example:
# Simple Floor Division
print(10 // 3) # Output: 3
# Floor Division with Variables
x = 25
y = 4
print(x // y) # Output: 6
# Multiple Value Floor Division
result = 100 // 6 // 3
print(result) # Output: 5
PythonKeep this information in your mind, if operand is float, the result is a float but it gives floor of the result.
print(5.5 // 2) # Output: 2.0
Python6. Modulus (%)
The modulus operator (%
) returns the remainder when one number is divided by another.
Example:
# Simple Modulus
print(10 % 3) # Output: 1
# Modulus with Variables
x = 25
y = 4
print(x % y) # Output: 1
Python7. Exponentiation (**)
The exponentiation operator (**
) raises one number to the power of another.
Example:
# Simple Exponentiation
print(2 ** 3) # Output: 8
# Exponentiation with Variables
x = 5
y = 2
print(x ** y) # Output: 25
# Multiple Value Exponentiation
result = 2 ** 3 ** 2
print(result) # Output: 512
PythonALERT!, Exponentiation is performed from right to left. In the expression 2 ** 3 ** 2
, 3 ** 2
resolves first to 9
, then 2 ** 9
resolves to 512
.
Conclusion
Arithmetic operators are so simple, even every operators are simple, where students stuck is the order of solving operators i.e., Operators Associativity and precedence, like how this expression – 1-2**2**2//2*2/1+8
generates -7.0
answer.