
Python
Number operations are the foundation of any calculation in Python. Beyond simple addition and subtraction, you'll frequently use multiplication, division, and special operations like modulo (remainder) and floor division. Understanding how these operations work together, including operator precedence, ensures your calculations are always correct.
Python provides six main arithmetic operations that you can perform on numbers. Each operation has a specific purpose and behaves slightly differently, especially when mixing integers and floats. Knowing all of them lets you express calculations naturally and efficiently.
# All arithmetic operations
x = 20
y = 6
add = x + y # Addition: 26
subtract = x - y # Subtraction: 14
multiply = x * y # Multiplication: 120
divide = x / y # Division: 3.333... (always float)
floor_div = x // y # Floor division: 3 (rounds down)
modulo = x % y # Modulo (remainder): 2
print(f"Add: {add}")
print(f"Subtract: {subtract}")
print(f"Multiply: {multiply}")
print(f"Divide: {divide}")
print(f"Floor Div: {floor_div}")
print(f"Modulo: {modulo}")Modulo (%) gives you the remainder after division, which is useful for checking divisibility, cycling through values, and extracting digits. Floor division (//) divides and rounds down to the nearest whole number, useful when you need a whole number result but don't want to explicitly round.
# Modulo examples
print(10 % 3) # 1 (remainder of 10 รท 3)
print(15 % 5) # 0 (15 is divisible by 5)
print(7 % 2) # 1 (7 is odd)
# Floor division examples
print(10 / 3) # 3.333... (regular division)
print(10 // 3) # 3 (rounded down)
print(-10 // 3) # -4 (rounds down, not toward zero)
# Practical: Check if even/odd
number = 15
if number % 2 == 0:
print(f"{number} is even")
else:
print(f"{number} is odd")The power operator (**) raises a number to a power, useful for exponential calculations. Python also supports scientific notation for very large or very small numbers, making it easy to work with values like millions or millionths without typing many zeros.
# Power operations
square = 5 ** 2 # 5 squared: 25
cube = 3 ** 3 # 3 cubed: 27
power = 2 ** 10 # 2 to power 10: 1024
square_root = 16 ** 0.5 # Square root: 4.0
# Scientific notation
million = 1e6 # 1,000,000
thousand = 1e3 # 1,000
millionth = 1e-6 # 0.000001
tiny = 1.5e-10 # 0.00000000015
print(f"2^10 = {power}")
print(f"Million = {million}")
print(f"Millionth = {millionth}")When you combine multiple operations in one expression, Python evaluates them in a specific order (PEMDAS). Exponents are evaluated before multiplication/division, which happen before addition/subtraction. Understanding this prevents mistakes, and you can always use parentheses to make the order explicit.
# Without parentheses - follows PEMDAS
result1 = 2 + 3 * 4 # Multiply first: 2 + 12 = 14
result2 = 10 / 2 + 3 # Divide first: 5 + 3 = 8
result3 = 2 ** 3 * 4 # Power first: 8 * 4 = 32
# With parentheses - changes order
result4 = (2 + 3) * 4 # Addition first: 5 * 4 = 20
result5 = (10 / 2) + 3 # Division first: 5 + 3 = 8 (same)
result6 = 2 ** (3 * 4) # Multiplication first: 2^12 = 4096
print(f"2 + 3 * 4 = {result1}")
print(f"(2 + 3) * 4 = {result4}")You can use the results of one calculation as input to another, building complex expressions from simple operations. This is especially useful when working with variables, where you often need to combine multiple calculations.
# Compound calculations
price = 99.99
quantity = 3
discount_percent = 10
subtotal = price * quantity
discount_amount = subtotal * (discount_percent / 100)
final_price = subtotal - discount_amount
print(f"Price: ${price}")
print(f"Quantity: {quantity}")
print(f"Subtotal: ${subtotal}")
print(f"Discount: ${discount_amount:.2f}")
print(f"Final: ${final_price:.2f}")principal = 1000 # Initial amount
rate = 0.05 # 5% interest per year
years = 2
interest = principal * rate * years
total = principal + interest
print(f"Initial: ${principal}")
print(f"Interest: ${interest}")
print(f"Total: ${total}")celsius = 25
fahrenheit = (celsius * 9/5) + 32
print(f"{celsius}ยฐC = {fahrenheit}ยฐF")scores = [85, 90, 78, 92, 88]
total = sum(scores)
average = total / len(scores)
print(f"Average score: {average:.1f}")| Operation | Symbol | Example | Result |
|---|---|---|---|
| Addition | + | 10 + 3 | 13 |
| Subtraction | - | 10 - 3 | 7 |
| Multiplication | * | 10 * 3 | 30 |
| Division | / | 10 / 3 | 3.333... |
| Floor Division | // | 10 // 3 | 3 |
| Modulo | % | 10 % 3 | 1 |
| Power | ** | 10 ** 2 | 100 |
Numbers are just one data type. Let's explore strings โ text data that's equally important in programming!
Next: Strings โ Creating and Using Text โ
Ready to practice? Try challenges or take the quiz
Resources
Ojasa Mirai
Master AI-powered development skills through structured learning, real projects, and verified credentials. Whether you're upskilling your team or launching your career, we deliver the skills companies actually need.
Learn Deep โข Build Real โข Verify Skills โข Launch Forward