๐Ÿ“ Python

Math Operations in Python ๐Ÿงฎ

P
Author
PyLand Team
๐Ÿ“…
Published
30.03.2026
โฑ๏ธ
Reading time
4 min
๐Ÿ‘๏ธ
Views
416
๐ŸŒฑ
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

๐Ÿ“

Logical Operators in Python: and, or, not

One condition is fine. But what if you need to check multiple conditions at once?

๐Ÿ“… 30.03.2026 ๐Ÿ‘๏ธ 386
๐Ÿ“

if __name__ == "__main__": the Python entry point

The if name == "main" guard defines a Python program's entry point: it distinguishes direct...

๐Ÿ“… 14.08.2026 ๐Ÿ‘๏ธ 44
๐Ÿ“

strip() and lower(): Preparing User Text ๐Ÿงน

A user may enter the correct word with extra spaces or different letter case. Python...

๐Ÿ“… 11.08.2026 ๐Ÿ‘๏ธ 50
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Python from Scratch: Build 7 Practical Projects Open course curriculum