Notes
Categories

Arithmetic Operators in Python [ English ]

< Prev Next >

1. Introduction

Arithmetic operators in Python are used to perform mathematical calculations such as addition, subtraction, multiplication, and division. These operators work with numeric data types like integers (int) and floating-point numbers (float).

2. Types of Arithmetic Operators

Operator Name Description Example
+ Addition Adds two values 5 + 3 = 8
- Subtraction Subtracts one value from another 5 - 3 = 2
* Multiplication Multiplies two values 5 * 3 = 15
/ Division Divides and returns float 5 / 2 = 2.5
// Floor Division Divides and returns integer part 5 // 2 = 2
% Modulus Returns remainder 5 % 2 = 1
** Exponent Raises power 2 ** 3 = 8

3. Detailed Explanation of Each Operator


3.1 Addition (+)

Adds two numbers.

a = 10
b = 5
print(a + b)

Output

15

3.2 Subtraction (-)

Subtracts one number from another.

print(10 - 5)

Output

5

3.3 Multiplication (*)

Multiplies two numbers.

print(4 * 3)

Output

12

3.4 Division (/)

Performs division and always returns a float value.

print(5 / 2)

Output

2.5

3.5 Floor Division (//)

Returns the integer part (quotient) of division.

print(5 // 2)

Output

2

3.6 Modulus (%)

Returns the remainder of division.

print(5 % 2)

Output

1

3.7 Exponent (**)

Raises one number to the power of another.

print(2 ** 3)

Output

8

4. Arithmetic Operators with Different Data Types

a = 5       # int
b = 2.5     # float

print(a + b)

Output

7.5

Explanation

5. Special Behavior of Operators

5.1 Division Always Returns Float

print(6 / 3)

Output:

2.0

5.2 Modulus with Negative Numbers

print(-5 % 2)

Output:

1

5.3 Exponent with Negative Power

print(2 ** -2)

Output:

0.25

6. Operator Precedence in Arithmetic

Order of execution:

  1. ** (Exponent)
  2. *, /, //, %
  3. +, -

Example

result = 2 + 3 * 4
print(result)

Output

14

7. Practical Example

a = 10
b = 3

print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Floor Division:", a // b)
print("Modulus:", a % b)
print("Exponent:", a ** b)

8. Summary

< Prev Next >