1. Arithmetic Operators
Arithmetic operators are used to perform basic mathematical calculations such as addition, subtraction, multiplication, and division. These are some of the most commonly used operators in Python.
Types of Arithmetic Operators in Python:
- Addition (+): Adds two values.
Example: 5 + 3 → 8
- Subtraction (−): Subtracts right operand from
left.
Example: 5 - 2 → 3
- Multiplication (*): Multiplies two
values.
Example: 4 * 3 → 12
- Division (/): Divides left operand by right and returns a
float.
Example: 10 / 2 → 5.0
- Floor Division (//): Returns only the integer part of the
division.
Example: 10 // 3 → 3
- Modulus (%): Returns the remainder of
division.
Example: 10 % 3 → 1
- Exponentiation (**): Raises first value to the power of
second.
Example: 2 ** 3 → 8
Examples of Arithmetic Operators in Python
1. Addition (+)
a = 5
b = 3
print("Addition:", a + b)
Output:
Addition: 8
Explanation:
5 + 3 equals 8. The +
operator adds the two values.
2. Subtraction (-)
a = 5
b = 2
print("Subtraction:", a - b)
Output:
Subtraction: 3
Explanation:
5 - 2 equals 3. The -
operator subtracts the second number from the
first.
3. Multiplication (*)
a = 4
b = 3
print("Multiplication:", a * b)
Output:
Multiplication: 12
Explanation:
4 × 3 equals 12. The *
operator multiplies both
values.
4. Division (/)
a = 10
b = 2
print("Division:", a / b)
Output:
Division: 5.0
Explanation:
10 ÷ 2 equals 5.0. The /
operator returns the
result as a float.
5. Floor Division (//)
a = 10
b = 3
print("Floor Division:", a // b)
Output:
Floor Division: 3
Explanation:
10 ÷ 3 gives 3.33, but //
returns only the integer part →
3.
6. Modulus (%)
a = 10
b = 3
print("Modulus:", a % b)
Output:
Modulus: 1
Explanation:
10 divided by 3 leaves a remainder of 1. The %
operator gives the remainder.
7. Exponentiation (**)
a = 2
b = 3
print("Exponentiation:", a ** b)
Output:
Exponentiation: 8
Explanation:
2 raised to the power 3 → 2 × 2 × 2 = 8. The **
operator is used for powers.
Important Points to Remember:
- Arithmetic operations follow operator precedence rules. For example, multiplication is done before addition.
- Using
/
always gives a float result, even if both numbers are integers. - These operators can be used with numbers, variables, and even expressions.
- Parentheses
()
have the highest precedence. Operations inside them are always evaluated first.
Example: (2 + 3) * 4 → 20
- Python follows the **PEMDAS** rule for operator precedence:
- P: Parentheses
- E: Exponentiation
(**)
- MD: Multiplication
(*)
and Division(/ // %)
(left to right) - AS: Addition
(+)
and Subtraction(-)
(left to right)