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:
- Addition:
+
- Subtraction:
-
- Multiplication:
*
- Division:
/
- Integer Division:
//
- Modulus (Remainder):
%
- Exponentiation:
**
Let’s explore each of these magical symbols!
7.2 Addition (+)
The +
operator adds two numbers together:
= 5 + 3
result print(result) # Output: 8
# You can also use variables
= 10
a = 7
b sum = a + b
print(sum) # Output: 17
7.3 Subtraction (-)
The -
operator subtracts the right number from the left:
= 10 - 4
result print(result) # Output: 6
= 15 - 23
difference print(difference) # Output: -8
7.4 Multiplication (*)
The *
operator multiplies two numbers:
= 6 * 7
result print(result) # Output: 42
# You can multiply floats too
= 4.99
price = 3
quantity = price * quantity
total print(total) # Output: 14.97
7.5 Division (/)
The /
operator divides the left number by the right. Note that this always returns a float:
= 20 / 5
result print(result) # Output: 4.0
= 10 / 3
result print(result) # Output: 3.3333333333333335
7.6 Floor Division (//)
The //
operator performs division and rounds down to the nearest integer:
= 20 // 6
result print(result) # Output: 3
= -20 // 6
result print(result) # Output: -4 (rounds towards negative infinity)
7.7 Modulus (%)
The %
operator returns the remainder after division:
= 17 % 5
result print(result) # Output: 2
# This is useful for checking if a number is even or odd
= 10 % 2 == 0
is_even print(is_even) # Output: True
7.8 Exponentiation (**)
The **
operator raises the left number to the power of the right number:
= 2 ** 3
result print(result) # Output: 8
= 9 ** 0.5
result 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):
- Parentheses
- Exponents
- Multiplication and Division (left to right)
- Addition and Subtraction (left to right)
Let’s see an example:
= 2 + 3 * 4 ** 2 - 6 / 2
result 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:
= 10
x += 5 # Equivalent to x = x + 5
x print(x) # Output: 15
= 20
y *= 3 # Equivalent to y = y * 3
y 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
= 5
dragon_scales = 3
phoenix_feathers = 2
unicorn_hair
# Brewing process
= dragon_scales * 2 + phoenix_feathers ** 2 + unicorn_hair * 3
potion_power print("Potion power:", potion_power)
# We found some extra ingredients!
+= 2
dragon_scales *= 2
phoenix_feathers
# Recalculate potion power
= dragon_scales * 2 + phoenix_feathers ** 2 + unicorn_hair * 3
potion_power print("New potion power:", potion_power)
# Check if the potion is extra powerful (power > 50)
= potion_power > 50
is_extra_powerful 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:
- Calculate the area of a rectangle with length 7 and width 5.
- 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?
- Calculate 2 to the power of 10 using the exponentiation operator.
- 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:
- Division by Zero: Trying to divide by zero will raise a
ZeroDivisionError
.
= 10 / 0 # ZeroDivisionError: division by zero result
- Integer Division Surprises: Remember that
//
always rounds down.
= 5 // 2 # Output: 2, not 2.5 result
- Floating Point Precision: Sometimes, floating-point arithmetic can give slightly unexpected results due to how computers represent decimals.
= 0.1 + 0.2
result print(result) # Output: 0.30000000000000004
- String and Number Confusion: Make sure you’re not trying to perform arithmetic on strings.
= "5" + 3 # TypeError: can only concatenate str (not "int") to str result
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:
- Python’s Official Documentation on Numeric Types
- Real Python’s Guide to Basic Python Math
- Khan Academy’s Arithmetic Operations (for a refresher on the math concepts)
Keep calculating, and may your Python programs always compute true!