📝 Python

Math Operations in Python 🧮

0
Author
04e5cc8b-58ac-4bdc-bdee-661bbb
📅
Published
30.03.2026
⏱️
Reading time
4 min
👁️
Views
114
🌱
Level
Beginner

Python is a great calculator! Let’s cover all the arithmetic operators with examples.

➕ Basic operations

Addition (+)

print(5 + 3)      # 8
print(10 + 20)    # 30
print(-5 + 15)    # 10

# With variables
a = 10
b = 5
print(a + b)      # 15

Subtraction (-)

print(10 - 3)     # 7
print(5 - 15)     # -10
print(-5 - 3)     # -8

# Age calculation
birth_year = 2005
current_year = 2026
age = current_year - birth_year
print(f'Age: {age}')  # 21

Multiplication (*)

print(5 * 3)      # 15
print(10 * 10)    # 100
print(-5 * 3)     # -15

# Rectangle area
width = 5
height = 8
area = width * height
print(f'Area: {area}')  # 40

Division (/)

print(10 / 2)     # 5.0
print(15 / 4)     # 3.75
print(7 / 3)      # 2.3333...

# ⚠️ ALWAYS returns a float, even for exact divisions!
print(type(10 / 2))  # <class 'float'>

🎯 Special operations

Floor division (//)

Result WITHOUT the fractional part

print(10 // 3)    # 3 (not 3.333...)
print(15 // 4)    # 3 (not 3.75)
print(7 // 2)     # 3 (not 3.5)

# Example: How many boxes are needed?
items = 23
box_size = 5
boxes = items // box_size
print(f'Boxes needed: {boxes}')  # 4

Modulo (%)

The remainder after division

print(10 % 3)     # 1
print(15 % 4)     # 3
print(20 % 5)     # 0 (divides evenly)

# Example: Even or odd?
number = 17
if number % 2 == 0:
    print('Even')
else:
    print('Odd')  # This will print

Exponentiation (**)

print(2 ** 3)     # 8 (2³)
print(5 ** 2)     # 25 (5²)
print(10 ** 3)    # 1000 (10³)

# Square root
print(16 ** 0.5)  # 4.0 (√16)
print(9 ** 0.5)   # 3.0 (√9)

🔢 Order of operations

Python follows standard math rules:

  1. Parentheses ()
  2. Exponentiation **
  3. Multiplication/Division * / // %
  4. Addition/Subtraction + -
# Without parentheses
print(2 + 3 * 4)       # 14 (3*4=12 first, then +2)

# With parentheses
print((2 + 3) * 4)     # 20 (2+3=5 first, then *4)

# Complex example
result = 10 + 5 * 2 ** 3 - 4
# Order: 2**3=8, 5*8=40, 10+40=50, 50-4=46
print(result)  # 46

💪 Augmented assignment operators

Updating a variable

score = 10

# Long form
score = score + 5

# Short form (same thing!)
score += 5  # score = score + 5

All augmented forms

x = 10

x += 5   # x = x + 5  → x becomes 15
x -= 3   # x = x - 3  → x becomes 12
x *= 2   # x = x * 2  → x becomes 24
x /= 4   # x = x / 4  → x becomes 6.0
x //= 2  # x = x // 2 → x becomes 3.0
x **= 2  # x = x ** 2 → x becomes 9.0

🎮 Practical examples

Calculator

print('🧮 Calculator\n')

a = float(input('First number: '))
b = float(input('Second number: '))

print(f'\n{a} + {b} = {a + b}')
print(f'{a} - {b} = {a - b}')
print(f'{a} * {b} = {a * b}')
print(f'{a} / {b} = {a / b}')
print(f'{a} // {b} = {a // b}')
print(f'{a} % {b} = {a % b}')
print(f'{a} ** {b} = {a ** b}')

Temperature converter

celsius = float(input('Temperature in Celsius: '))

# Formula: F = C * 9/5 + 32
fahrenheit = celsius * 9/5 + 32

print(f'{celsius}°C = {fahrenheit}°F')

Discount calculator

price = float(input('Item price: '))
discount = int(input('Discount in %: '))

# Discount amount
discount_amount = price * discount / 100

# Final price
final_price = price - discount_amount

print(f'\nDiscount: ${discount_amount:.2f}')
print(f'Total: ${final_price:.2f}')

Taxi fare

distance = float(input('Distance (km): '))

# Rates
base_price = 3.00   # Base fare
price_per_km = 1.50  # Per km

total = base_price + distance * price_per_km

print(f'\nRide cost: ${total:.2f}')

⚡ Common mistakes

Mistake 1: Division by zero

# ❌ ZeroDivisionError!
print(10 / 0)

# ✅ Check before dividing
b = float(input('Divisor: '))
if b != 0:
    print(10 / b)
else:
    print('Cannot divide by zero!')

Mistake 2: True vs floor division

# Different results!
print(10 / 3)   # 3.3333... (float)
print(10 // 3)  # 3 (int)

# Use // when you need a whole number
items = 17
per_box = 5
boxes = items // per_box  # 3 (not 3.4)

Mistake 3: Negative modulo

# With negative numbers
print(10 % 3)    # 1
print(-10 % 3)   # 2 (not -1!)
print(10 % -3)   # -2 (not 1!)

# The sign of the result matches the sign of the divisor!

📋 Cheat sheet

# Basic operations
+    # Addition
-    # Subtraction
*    # Multiplication
/    # Division (always float)
//   # Floor division
%    # Modulo (remainder)
**   # Exponentiation

# Augmented assignment
+=   # Add and assign
-=   # Subtract and assign
*=   # Multiply and assign
/=   # Divide and assign
//=  # Floor divide and assign
**=  # Exponentiate and assign

# Precedence: () → ** → * / // % → + -

🎓 Summary

  • Python supports all arithmetic operations
  • / always returns a float; // returns an int
  • % is the remainder (great for even/odd checks!)
  • ** is exponentiation
  • Use parentheses () to control evaluation order
  • Augmented operators like += and -= keep your code concise

With these operators you can solve any math problem in Python! 🚀

Your reaction to the article

💬 Comments (0)

🔐 Sign in to leave a comment
🚪 Login
💭

No comments yet

Be the first to share your opinion about this article!

🔗 Similar

Similar articles

Continue learning with these materials

📝

Setting Up Your Environment: Python, pip, and VS …

Before writing code locally, you need to set up three tools: Python, pip, and VS...

📅 04.06.2026 👁️ 17
📝

The datetime Module: Working with Dates and Times

datetime is Python's standard module for working with dates and times. It's part of the...

📅 08.05.2026 👁️ 67
📝

.env Files and Environment Variables: Keeping Sec…

Imagine you wrote a program with an API key hardcoded in the source and pushed...

📅 08.05.2026 👁️ 76

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!