Lesson Objective
By the end of this lesson, you will be proficient in using arithmetic operators to perform calculations, understand operator precedence, and know how to use Python's built-in math functions. These skills enable you to build programs that perform numerical computations of any complexity.
What You Will Learn
- All arithmetic operators available in Python
- The difference between integer division and regular division
- How operator precedence affects calculations
- How to use parentheses to control order of operations
- Assignment operators for updating variables
- Built-in functions for common mathematical operations
Required Knowledge or Tools
This lesson builds on Lesson 03. You should understand variables and the difference between integers and floats. You need your Python environment set up and ready for writing and running programs.
Concept Explanation
Python provides several arithmetic operators for performing calculations. The basic operators are addition using the plus sign, subtraction using the minus sign, multiplication using the asterisk, and division using the forward slash. Division in Python 3 always returns a float, even when dividing two integers.
Python also provides special operators. The double forward slash performs integer division, which divides and rounds down to the nearest whole number. The percent sign is the modulo operator, which returns the remainder after division. The double asterisk raises a number to a power.
When an expression contains multiple operators, Python follows standard mathematical precedence. Exponentiation happens first, then multiplication and division, and finally addition and subtraction. Operators at the same level are evaluated left to right. You can use parentheses to override the default order and ensure operations happen in the sequence you intend.
Why This Lesson Matters
Numerical calculations are at the heart of countless programs, from simple calculators to complex scientific simulations. Understanding how Python handles numbers and operations ensures your calculations produce correct results. Mistakes in operator precedence can cause subtle bugs that are difficult to track down, making this knowledge essential for reliable programming.
Step-by-Step Guide
Step 1: Basic Arithmetic
Create a file called math_ops.py and practice the basic operators:
a = 10
b = 3
print("Addition:", a + b)
print("Subtraction:", a - b)
print("Multiplication:", a * b)
print("Division:", a / b)
print("Integer Division:", a // b)
print("Modulo:", a % b)
print("Exponent:", a ** b)
Step 2: Operator Precedence
Observe how precedence affects results:
result1 = 2 + 3 * 4
result2 = (2 + 3) * 4
print("2 + 3 * 4 =", result1)
print("(2 + 3) * 4 =", result2)
Step 3: Assignment Operators
Use compound assignment to update variables:
count = 10
count += 5
print("After += 5:", count)
count -= 3
print("After -= 3:", count)
count *= 2
print("After *= 2:", count)
Common Mistakes
- Confusing division with integer division, expecting whole numbers from regular division
- Forgetting operator precedence and getting unexpected results in complex expressions
- Using a single forward slash expecting integer results when you need the double slash
- Dividing by zero, which causes an error that crashes your program
- Misunderstanding modulo, which gives the remainder, not the quotient
Practical Example or Scenario
Create a program that calculates the total cost of items with tax:
item_price = 29.99
quantity = 3
tax_rate = 0.08
subtotal = item_price * quantity
tax = subtotal * tax_rate
total = subtotal + tax
print("Subtotal: $" + str(round(subtotal, 2)))
print("Tax: $" + str(round(tax, 2)))
print("Total: $" + str(round(total, 2)))
This example combines multiple concepts: variables, arithmetic operations, and the round function to format currency properly.
Lesson Summary
In this lesson, you mastered Python's arithmetic operators including addition, subtraction, multiplication, division, integer division, modulo, and exponentiation. You learned how operator precedence determines the order of operations and how to use parentheses for explicit control. You practiced compound assignment operators for updating variables efficiently. These numerical skills are essential for building programs that perform calculations. In the next lesson, you will explore strings and text manipulation techniques.