Arithmetic Operators
Arithmetic operators in Python are used to perform mathematical calculations.
They work with numeric data types such as
int, float, and complex.These operators are fundamental and used in almost every Python program.
What Are Arithmetic Operators?
Arithmetic operators perform basic math operations between two operands.
python
a = 10
b = 3
Types of Arithmetic Operators
| Operator | Name | Description |
|---|---|---|
+ | Addition | Adds two values |
- | Subtraction | Subtracts second value from first |
* | Multiplication | Multiplies two values |
/ | Division | Divides two values |
% | Modulus | Returns remainder |
** | Exponentiation | Power operation |
// | Floor Division | Returns quotient without decimals |
Addition (+)
Adds two numbers.
python
x = 10
y = 20
print(x + y)
python
print(5 + 2.5)
python
print(-10 + 30)
Subtraction (-)
Subtracts one number from another.
python
x = 20
y = 5
print(x - y)
python
print(5 - 10)
python
print(10.5 - 2.5)
Multiplication (*)
Multiplies two numbers.
python
x = 4
y = 3
print(x * y)
python
print(2.5 * 4)
python
print(-3 * 6)
Division (/)
Always returns a float value, even if the division is exact.
python
print(10 / 2)
python
print(9 / 2)
python
print(5 / 2.0)
Modulus (%)
Returns the remainder of a division.
python
print(10 % 3)
python
print(9 % 2)
python
print(15 % 4)
Exponentiation (**)
Raises a number to the power of another.
python
print(2 ** 3)
python
print(5 ** 2)
python
print(9 ** 0.5)
Floor Division (//)
Returns the integer part of the division result.
python
print(10 // 3)
python
print(9 // 2)
python
print(10.5 // 2)
Important
Floor division rounds down to the nearest whole number.
Arithmetic Operators with Variables
python
a = 15
b = 4
print(a + b)
print(a - b)
print(a * b)
print(a / b)
print(a % b)
Arithmetic Operators with Negative Numbers
python
print(-10 + 5)
python
print(-10 // 3)
python
print(-10 % 3)
Operator Precedence in Arithmetic
Python follows BODMAS/PEMDAS rule.
python
print(10 + 5 * 2)
python
print((10 + 5) * 2)
python
print(100 / 10 * 2)
Tricky Arithmetic Examples (Interview)
python
print(5 // 2)
python
print(-5 // 2)
python
print(5 % 2)
python
print(-5 % 2)
Common Mistakes
Expecting Integer Result from /
python
print(10 / 5) # Output: 2.0
Correct Way
python
print(10 // 5) # Output: 2
Summary
- Arithmetic operators perform mathematical operations
/always returns float//performs floor division%returns remainder**is used for power- Operator precedence affects results
Practice
- Perform all arithmetic operations on two numbers
- Find square root using exponentiation
- Predict output of negative floor division
- Solve expressions using operator precedence