โž•โž–โœ–๏ธโž— 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 ๐Ÿ”ข

OperatorSymbolDescriptionExample
Addition+Adds two numbers3 + 2 = 5
Subtraction-Subtracts one from another5 - 2 = 3
Multiplication*Multiplies numbers4 * 3 = 12
Division/Returns float division10 / 4 = 2.5
Floor Division//Returns whole number division10 // 4 = 2
Modulus%Gives remainder10 % 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)  # 3

3. 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)  # 30

Note

๐Ÿง  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)   # 3

Note

๐Ÿ“Œ 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 โ†’ 3

7. 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! ๐Ÿ˜Š