Ojasa Mirai

Ojasa Mirai

Python

Loading...

Learning Level

๐ŸŸข Beginner๐Ÿ”ต Advanced
What are Variables?Numbers โ€” Integers and FloatsNumber OperationsStrings โ€” Creating and Using TextString FormattingBooleans and NoneType ConversionGetting User InputBest Practices
Python/Variables Data Types/Number Operations

โž• Number Operations โ€” Mastering Calculations

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.


๐Ÿงฎ All Arithmetic Operations Summary

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 and Floor Division in Detail

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")

๐Ÿ’ช Power Operations and Scientific Notation

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}")

๐Ÿ”ค Combining Operations and Operator Precedence

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}")

๐Ÿ”— Combining Operations with Variables

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}")

๐Ÿ’ก Real-World Calculations

Calculate Interest

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}")

Temperature Conversion

celsius = 25
fahrenheit = (celsius * 9/5) + 32

print(f"{celsius}ยฐC = {fahrenheit}ยฐF")

Calculate Average

scores = [85, 90, 78, 92, 88]
total = sum(scores)
average = total / len(scores)

print(f"Average score: {average:.1f}")

โœ… Key Takeaways

OperationSymbolExampleResult
Addition+10 + 313
Subtraction-10 - 37
Multiplication*10 * 330
Division/10 / 33.333...
Floor Division//10 // 33
Modulo%10 % 31
Power**10 ** 2100

๐Ÿ”— What's Next?

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

Python Docs

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

Courses

PythonFastapiReactJSCloud

ยฉ 2026 Ojasa Mirai. All rights reserved.

TwitterGitHubLinkedIn