โโโ๏ธโ Python Arithmetic Operators โ Performing Mathematical Operations
Introduction ๐
Arithmetic operators allow you to perform mathematical calculations in Python. These are the most commonly used operators when working with numbers, formulas, and expressions.
Note
๐ก Python supports integer, float, and even complex number arithmetic.
1. List of Arithmetic Operators ๐ข
| Operator | Symbol | Description | Example |
|---|---|---|---|
| Addition | + | Adds two numbers | 3 + 2 = 5 |
| Subtraction | - | Subtracts one from another | 5 - 2 = 3 |
| Multiplication | * | Multiplies numbers | 4 * 3 = 12 |
| Division | / | Returns float division | 10 / 4 = 2.5 |
| Floor Division | // | Returns whole number division | 10 // 4 = 2 |
| Modulus | % | Gives remainder | 10 % 3 = 1 |
| Exponent | ** | Power (xโฟ) | 2 ** 3 = 8 |
2. Basic Arithmetic Examples ๐งฎ
basic_arithmetic.py
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.333...
print(a % b) # 1
print(a ** b) # 1000
print(a // b) # 33. Order of Operations (PEMDAS/BODMAS) ๐
Python follows mathematical precedence rules:
- P โ Parentheses
- E โ Exponent (**)
- M/D โ Multiplication & Division
- A/S โ Addition & Subtraction
precedence.py
result = 10 + 5 * 2
print(result) # 20
result = (10 + 5) * 2
print(result) # 30Note
๐ง Parentheses change the default order โ use them to avoid confusion.
4. Working With Floats & Integers ๐
float_int.py
x = 5
y = 2.0
print(x + y) # 7.0
print(x / y) # 2.5 (always float)
print(x // y) # 2.0 (floor division keeps float)Division always returns a float, even if divisible perfectly.
5. Modulus Operator (%) โ Getting the Remainder ๐งฉ
modulus.py
print(10 % 3) # 1
print(15 % 5) # 0
print(7 % 4) # 3Note
๐ Useful in even/odd checking, circular loops, and indexing.
6. Exponent Operator (**) โ Power Operation โก
exponent.py
print(2 ** 3) # 8
print(5 ** 2) # 25
print(9 ** 0.5) # square root of 9 โ 37. Floor Division (//) โ Remove Decimals ๐ฝ
floor_div.py
print(10 // 3) # 3
print(15 // 2) # 7
print(-10 // 3) # -4 (floors down)Note
Floor division always rounds downward.
8. Using Arithmetic Operators With Variables ๐ฏ
variables_arithmetic.py
x = int(input("Enter a number: "))
y = int(input("Enter another number: "))
print("Sum =", x + y)
print("Difference =", x - y)
print("Product =", x * y)
print("Quotient =", x / y)9. Real-World Example ๐
real_world_example.py
price = float(input("Enter price: "))
qty = int(input("Enter quantity: "))
total = price * qty
print(f"Total amount: Rs.{total:.2f}")Conclusion ๐
>>โArithmetic operators are the foundation of all calculations โ master them and math becomes effortless in Python.โ โจ
You now know all arithmetic operators and how to use them. Want the next topic? Try Comparison Operators, Logical Operators, Assignment Operators, Expressions, or Conditional Statements. Just ask! ๐