7  Arithmetic Operators: The Magic of Mathematical Operations

Now that you’ve mastered the art of data types, it’s time to learn how to perform magical calculations with Python’s arithmetic operators. These powerful symbols will allow you to add, subtract, multiply, divide, and more!

7.1 The Basic Arithmetic Spells

Python provides several basic arithmetic operators that work just like the math you’re familiar with:

  1. Addition: +
  2. Subtraction: -
  3. Multiplication: *
  4. Division: /
  5. Integer Division: //
  6. Modulus (Remainder): %
  7. Exponentiation: **

Let’s explore each of these magical symbols!

7.2 Addition (+)

The + operator adds two numbers together:

result = 5 + 3
print(result)  # Output: 8

# You can also use variables
a = 10
b = 7
sum = a + b
print(sum)  # Output: 17

7.3 Subtraction (-)

The - operator subtracts the right number from the left:

result = 10 - 4
print(result)  # Output: 6

difference = 15 - 23
print(difference)  # Output: -8

7.4 Multiplication (*)

The * operator multiplies two numbers:

result = 6 * 7
print(result)  # Output: 42

# You can multiply floats too
price = 4.99
quantity = 3
total = price * quantity
print(total)  # Output: 14.97

7.5 Division (/)

The / operator divides the left number by the right. Note that this always returns a float:

result = 20 / 5
print(result)  # Output: 4.0

result = 10 / 3
print(result)  # Output: 3.3333333333333335

7.6 Floor Division (//)

The // operator performs division and rounds down to the nearest integer:

result = 20 // 6
print(result)  # Output: 3

result = -20 // 6
print(result)  # Output: -4 (rounds towards negative infinity)

7.7 Modulus (%)

The % operator returns the remainder after division:

result = 17 % 5
print(result)  # Output: 2

# This is useful for checking if a number is even or odd
is_even = 10 % 2 == 0
print(is_even)  # Output: True

7.8 Exponentiation (**)

The ** operator raises the left number to the power of the right number:

result = 2 ** 3
print(result)  # Output: 8

result = 9 ** 0.5
print(result)  # Output: 3.0 (square root of 9)

7.9 Order of Operations

Just like in regular math, Python follows the order of operations (PEMDAS):

  1. Parentheses
  2. Exponents
  3. Multiplication and Division (left to right)
  4. Addition and Subtraction (left to right)

Let’s see an example:

result = 2 + 3 * 4 ** 2 - 6 / 2
print(result)  # Output: 47.0

# Let's break it down:
# 1. 4 ** 2 = 16
# 2. 3 * 16 = 48
# 3. 6 / 2 = 3.0
# 4. 2 + 48 - 3.0 = 47.0

7.10 Combining Arithmetic with Assignment

Python provides a shorthand way to perform an operation and assign the result back to the variable:

x = 10
x += 5  # Equivalent to x = x + 5
print(x)  # Output: 15

y = 20
y *= 3  # Equivalent to y = y * 3
print(y)  # Output: 60

This works with all arithmetic operators: +=, -=, *=, /=, //=, %=, **=

7.11 Practical Magic: A Potion Brewing Calculator

Let’s use our new arithmetic skills to create a potion brewing calculator:

# Initial ingredients
dragon_scales = 5
phoenix_feathers = 3
unicorn_hair = 2

# Brewing process
potion_power = dragon_scales * 2 + phoenix_feathers ** 2 + unicorn_hair * 3
print("Potion power:", potion_power)

# We found some extra ingredients!
dragon_scales += 2
phoenix_feathers *= 2

# Recalculate potion power
potion_power = dragon_scales * 2 + phoenix_feathers ** 2 + unicorn_hair * 3
print("New potion power:", potion_power)

# Check if the potion is extra powerful (power > 50)
is_extra_powerful = potion_power > 50
print("Is the potion extra powerful?", is_extra_powerful)

7.12 Practice Your Arithmetic Magic

Now it’s your turn to practice these arithmetic spells:

  1. Calculate the area of a rectangle with length 7 and width 5.
  2. You have 47 gold coins and want to divide them equally among 5 friends. How many coins does each friend get, and how many are left over?
  3. Calculate 2 to the power of 10 using the exponentiation operator.
  4. Create a simple temperature converter that converts Celsius to Fahrenheit using the formula: F = C * 9/5 + 32

7.13 Common Bugs to Watch Out For

As you experiment with arithmetic operations, be wary of these common pitfalls:

  1. Division by Zero: Trying to divide by zero will raise a ZeroDivisionError.
result = 10 / 0  # ZeroDivisionError: division by zero
  1. Integer Division Surprises: Remember that // always rounds down.
result = 5 // 2  # Output: 2, not 2.5
  1. Floating Point Precision: Sometimes, floating-point arithmetic can give slightly unexpected results due to how computers represent decimals.
result = 0.1 + 0.2
print(result)  # Output: 0.30000000000000004
  1. String and Number Confusion: Make sure you’re not trying to perform arithmetic on strings.
result = "5" + 3  # TypeError: can only concatenate str (not "int") to str

7.14 Conclusion

You’ve now mastered the basic arithmetic operators in Python. With these tools at your disposal, you can perform a wide range of calculations, from simple addition to complex formulas.

Remember, practice makes perfect. The more you use these operators, the more natural they’ll become. Soon, you’ll be slinging arithmetic spells like a true Python wizard!

7.15 Further Resources

To deepen your understanding of Python arithmetic, check out these magical scrolls:

  1. Python’s Official Documentation on Numeric Types
  2. Real Python’s Guide to Basic Python Math
  3. Khan Academy’s Arithmetic Operations (for a refresher on the math concepts)

Keep calculating, and may your Python programs always compute true!