Teacher's Guide

Chapter 3: Basic Operators

Teaching Objectives

By the end of this chapter, students should:

  • Understand and use arithmetic operators (+, -, *, /, //, %, **)
  • Understand and use comparison operators (==, !=, >, <, >=, <=)
  • Understand and use logical operators (and, or, not)
  • Apply operators to solve basic problems
  • Understand operator precedence

Preparation

Before teaching this chapter, ensure:

  • Students are comfortable with variables and basic data types
  • You have prepared visual aids to explain operator precedence
  • You have tested all code examples to make sure they work as expected

Lesson Overview

1. Introduction to Operators (10 minutes)

Start by explaining what operators are:

  • Operators are symbols that tell the computer to perform specific operations
  • They work on values (operands) to produce a result
  • Different types of operators perform different kinds of operations

Analogy: Operators are like the buttons on a calculator - they tell the computer what calculation to perform.

2. Arithmetic Operators (20 minutes)

Introduce the basic arithmetic operators:

# Addition
sum_result = 5 + 3
print("5 + 3 =", sum_result)  # Outputs: 5 + 3 = 8

# Subtraction
difference = 10 - 4
print("10 - 4 =", difference)  # Outputs: 10 - 4 = 6

# Multiplication
product = 6 * 4
print("6 * 4 =", product)  # Outputs: 6 * 4 = 24

# Division (returns floating-point result)
division_result = 12 / 5
print("12 / 5 =", division_result)  # Outputs: 12 / 5 = 2.4

# Floor Division (returns integer result, discards remainder)
floor_division = 12 // 5
print("12 // 5 =", floor_division)  # Outputs: 12 // 5 = 2

# Modulus (returns the remainder after division)
remainder = 12 % 5
print("12 % 5 =", remainder)  # Outputs: 12 % 5 = 2

# Exponentiation (raises to the power)
power = 2 ** 3
print("2 ** 3 =", power)  # Outputs: 2 ** 3 = 8

Teaching approach:

  • Demonstrate each operator with multiple examples
  • Explain when to use each operator (particularly // vs / and when % is useful)
  • Show how operators work with variables as well as literal values

Real-world examples:

  • Division (/) for calculating averages: average_score = total_score / number_of_tests
  • Modulus (%) for determining odd/even numbers: is_even = number % 2 == 0
  • Floor division (//) for grouping items: number_of_full_boxes = total_items // items_per_box

3. Comparison Operators (15 minutes)

Explain how comparison operators work:

# Equal to
print("5 == 5:", 5 == 5)  # Outputs: 5 == 5: True
print("5 == 6:", 5 == 6)  # Outputs: 5 == 6: False

# Not equal to
print("5 != 6:", 5 != 6)  # Outputs: 5 != 6: True

# Greater than
print("7 > 3:", 7 > 3)    # Outputs: 7 > 3: True

# Less than
print("4 < 8:", 4 < 8)    # Outputs: 4 < 8: True

# Greater than or equal to
print("5 >= 5:", 5 >= 5)  # Outputs: 5 >= 5: True

# Less than or equal to
print("6 <= 5:", 6 <= 5)  # Outputs: 6 <= 5: False

Teaching points:

  • Comparison operators always return a boolean result (True or False)
  • These operators form the basis for decision-making in programs
  • Show examples with different data types, including strings

Common mistakes to highlight:

  • Confusing assignment (=) with equality comparison (==)
  • Attempting to compare incompatible types (may cause errors or unexpected results)

4. Logical Operators (15 minutes)

Introduce logical operators that work with boolean values:

# Logical AND - returns True if both operands are True
print("True and True:", True and True)    # Outputs: True and True: True
print("True and False:", True and False)  # Outputs: True and False: False

# Logical OR - returns True if at least one operand is True
print("True or False:", True or False)    # Outputs: True or False: True
print("False or False:", False or False)  # Outputs: False or False: False

# Logical NOT - returns the opposite boolean value
print("not True:", not True)              # Outputs: not True: False
print("not False:", not False)            # Outputs: not False: True

Truth tables: Use visual truth tables to explain how logical operators work:

ANDTrueFalse
TrueTrueFalse
FalseFalseFalse
ORTrueFalse
TrueTrueTrue
FalseTrueFalse

NOT |
--- | ---- True | False False | True

Combining logical operators:

age = 15
has_permission = True

# Using multiple logical operators
can_play = age >= 13 and has_permission
print("Can play:", can_play)  # Outputs: Can play: True

# With parentheses for clarity
is_eligible = (age > 10) and (age < 18)
print("Is eligible:", is_eligible)  # Outputs: Is eligible: True

5. Operator Precedence (15 minutes)

Explain how Python determines the order of operations:

# Without parentheses - multiplication happens before addition
result1 = 2 + 3 * 4
print("2 + 3 * 4 =", result1)  # Outputs: 2 + 3 * 4 = 14

# With parentheses - addition happens first
result2 = (2 + 3) * 4
print("(2 + 3) * 4 =", result2)  # Outputs: (2 + 3) * 4 = 20

# Complex expression
result3 = 10 - 2 ** 3 // 2
print("10 - 2 ** 3 // 2 =", result3)  # Outputs: 10 - 2 ** 3 // 2 = 6

Precedence rules (from highest to lowest):

  1. Parentheses ()
  2. Exponentiation **
  3. Unary operators +x, -x, not x
  4. Multiplication, division, floor division, modulus *, /, //, %
  5. Addition, subtraction +, -
  6. Comparison operators ==, !=, >, <, >=, <=
  7. Logical AND and
  8. Logical OR or

Teaching tip: Encourage students to use parentheses to make their code more readable, even when not strictly necessary.

6. Guided Practice (20 minutes)

Have students work through these exercises:

  1. Basic calculator:

    # Calculator app that demonstrates arithmetic operators
    
    num1 = int(input("Enter first number: "))
    num2 = int(input("Enter second number: "))
    
    print("Addition:", num1 + num2)
    print("Subtraction:", num1 - num2)
    print("Multiplication:", num1 * num2)
    print("Division:", num1 / num2)
    print("Floor division:", num1 // num2)
    print("Remainder:", num1 % num2)
    print("Exponentiation:", num1 ** num2)
    
  2. Age verification:

    # Program to check if someone can watch a PG-13 movie
    
    age = int(input("Enter your age: "))
    with_adult = input("Are you with an adult? (yes/no): ") == "yes"
    
    can_watch = age >= 13 or (age >= 8 and with_adult)
    
    if can_watch:
        print("You can watch the movie!")
    else:
        print("Sorry, you cannot watch this movie.")
    

7. Problem-Solving Activities (15 minutes)

Present students with scenarios that require using operators:

  1. Grade calculator:

    # Calculate a final grade based on test scores
    
    test1 = 85
    test2 = 92
    test3 = 78
    
    # Calculate average (sum divided by number of tests)
    average = (test1 + test2 + test3) / 3
    
    # Check if the student passed (passing grade is 70 or above)
    passed = average >= 70
    
    print("Average score:", average)
    print("Passed:", passed)
    
    # Determine letter grade
    if average >= 90:
        print("Letter grade: A")
    elif average >= 80:
        print("Letter grade: B")
    elif average >= 70:
        print("Letter grade: C")
    elif average >= 60:
        print("Letter grade: D")
    else:
        print("Letter grade: F")
    
  2. Even/odd checker:

    # Check if a number is even or odd using modulus
    
    number = int(input("Enter a number: "))
    
    is_even = number % 2 == 0
    
    if is_even:
        print(number, "is an even number")
    else:
        print(number, "is an odd number")
    

8. Review and Discussion (10 minutes)

  • Review the key concepts covered
  • Ask students to explain in their own words:
    • What are the different types of operators?
    • How do logical operators work?
    • Why is operator precedence important?

Common Challenges and Solutions

  • Order of operations confusion: Use parentheses to make the intended order clear.
  • Misunderstanding logical operators: Provide plenty of examples with truth tables.
  • Confusing = and ==: Emphasize that = is for assignment and == is for comparison.
  • Division returning unexpected results: Remind students about the difference between / and //.

Extension Activities

For students who finish early:

  • Challenge them to create a BMI calculator using arithmetic operators
  • Have them build a simple quiz program using logical operators to check answers
  • Ask them to create a program that determines if a year is a leap year

Assessment

Look for these indicators of understanding:

  • Students can use operators correctly in their code
  • They can predict the result of expressions with different operators
  • They understand and can apply operator precedence rules

Resources

Chapter 3: Basic Operators | Teacher's Guide