Arithmetic Operators in Python
Arithmetic operators in Python are used to perform mathematical operations between numerical values. These operators work with both integers and floating-point numbers.
List of Arithmetic Operators
Operator | Symbol | Description |
---|---|---|
Addition | + | Adds two operands |
Subtraction | - | Subtracts the right operand from the left |
Multiplication | * | Multiplies two operands |
Division | / | Divides left operand by right (float result) |
Floor Division | // | Divides and returns the integer quotient |
Modulus | % | Returns remainder after division |
Exponentiation | ** | Raises the first operand to the power of the second |
1. Addition (+
)
Adds two numbers.
addition.py
a = 10
b = 5
print("Sum:", a + b)
output.txt
Sum: 15
Example 2: Adding Floats
addition_float.py
x = 3.5
y = 4.5
print("Result:", x + y)
output.txt
Result: 8.0
Example 3: Adding Mixed Types
addition_mixed.py
num = 7
decimal = 2.3
print("Total:", num + decimal)
output.txt
Total: 9.3
2. Subtraction (-
)
Subtracts the right operand from the left.
subtraction.py
a = 15
b = 8
print("Difference:", a - b)
output.txt
Difference: 7
Example 2: Subtracting Floats
subtraction_float.py
x = 10.0
y = 3.75
print("Result:", x - y)
output.txt
Result: 6.25
Example 3: Subtracting with Negative Numbers
subtraction_negative.py
m = -5
n = 12
print("Result:", m - n)
output.txt
Result: -17
3. Multiplication (*
)
Multiplies two operands.
multiplication.py
a = 6
b = 4
print("Product:", a * b)
output.txt
Product: 24
Example 2: Multiplying Floats
multiplication_float.py
x = 2.5
y = 4.0
print("Result:", x * y)
output.txt
Result: 10.0
Example 3: Multiplying with Zero
multiplication_zero.py
num = 7
print("Product:", num * 0)
output.txt
Product: 0
4. Division (/
)
Divides left operand by right. Result is always a float.
division.py
a = 20
b = 4
print("Quotient:", a / b)
output.txt
Quotient: 5.0
Example 2: Dividing Floats
division_float.py
x = 7.5
y = 2.5
print("Result:", x / y)
output.txt
Result: 3.0
Example 3: Division with Result < 1
division_small.py
num = 3
den = 10
print("Result:", num / den)
output.txt
Result: 0.3
5. Floor Division (//
)
Divides and returns the integer part (quotient).
floor_division.py
a = 15
b = 4
print("Floor Division:", a // b)
output.txt
Floor Division: 3
Example 2: With Negative Numbers
floor_division_negative.py
x = -10
y = 3
print("Result:", x // y)
output.txt
Result: -4
Example 3: With Floats
floor_division_float.py
num = 7.5
den = 2
print("Result:", num // den)
output.txt
Result: 3.0
6. Modulus (%
)
Returns remainder after division.
modulus.py
a = 17
b = 5
print("Remainder:", a % b)
output.txt
Remainder: 2
Example 2: Even or Odd Check
modulus_evenodd.py
num = 8
print("Is Even?", num % 2 == 0)
output.txt
Is Even? True
Example 3: Negative Modulus
modulus_negative.py
x = -13
y = 4
print("Result:", x % y)
output.txt
Result: 3
7. Exponentiation (**
)
Raises first operand to the power of the second.
exponentiation.py
a = 2
b = 3
print("Power:", a ** b)
output.txt
Power: 8
Example 2: Square Root
exponentiation_sqrt.py
num = 16
print("Square Root:", num ** 0.5)
output.txt
Square Root: 4.0
Example 3: Negative Exponents
exponentiation_negative.py
base = 2
exp = -2
print("Result:", base ** exp)
output.txt
Result: 0.25
Summary
- Python supports a full set of arithmetic operators.
- Operators work on integers, floats, and mixed types.
- Division
/
always returns a float, while floor division//
returns an integer part. - Exponentiation
**
is useful for power operations and square roots. - Use these operators for mathematical and data-processing tasks.