Chapter 4: Python Operators Exercises

Hands-on exercises to practice working with Python operators

Chapter 4 Concepts

This chapter covers the different types of operators in Python programming:

Arithmetic Comparison Logical Assignment Identity Membership Bitwise
Exercise 1

Arithmetic Operations

Create a program that demonstrates various arithmetic operations.

# Define two variables
a = 15
b = 4

# Perform these operations:
# 1. Addition
# 2. Subtraction
# 3. Multiplication
# 4. Division
# 5. Floor division
# 6. Modulus
# 7. Exponentiation
  • Use + for addition
  • Use - for subtraction
  • Use * for multiplication
  • Use / for division
  • Use // for floor division
  • Use % for modulus
  • Use ** for exponentiation
  • Print all results with descriptive messages
  • Exercise 2

    Comparison Operators

    Practice using comparison operators to evaluate conditions.

    # Define two variables
    x = 10
    y = 5

    # Compare using these operators:
    # 1. Equal to (==)
    # 2. Not equal to (!=)
    # 3. Greater than (>)
    # 4. Less than (<)
    # 5. Greater than or equal to (>=)
    # 6. Less than or equal to (<=)
  • Use == to check equality
  • Use != to check inequality
  • Use > to check if greater
  • Use < to check if less
  • Use >= to check if greater or equal
  • Use <= to check if less or equal
  • Print all results with descriptive messages
  • Exercise 3

    Logical Operators

    Practice using logical operators to combine conditions.

    # Define some variables
    age = 25
    has_license = True
    has_car = False

    # Create logical expressions:
    # 1. Can drive if age >= 18 AND has_license
    # 2. Can rent a car if age >= 21 AND has_license
    # 3. Needs transportation if NOT has_car
    # 4. Is young adult if age >= 18 AND age <= 25
    # 5. Is eligible for discount if student OR senior
  • Use and to combine conditions
  • Use or to provide alternatives
  • Use not to negate conditions
  • Create complex expressions with parentheses
  • Print all results with descriptive messages
  • Exercise 4

    Assignment Operators

    Practice using assignment operators to modify variables.

    # Start with these variables
    total = 10
    product = 5
    bits = 0b1010

    # Use assignment operators:
    # 1. Add 5 to total using +=
    # 2. Multiply product by 3 using *=
    # 3. Subtract 2 from total using -=
    # 4. Divide product by 2 using /=
    # 5. Perform bitwise OR on bits using |=
  • Use += to add and assign
  • Use -= to subtract and assign
  • Use *= to multiply and assign
  • Use /= to divide and assign
  • Use //= for floor division assignment
  • Use %= for modulus assignment
  • Print variable values after each operation
  • Exercise 5

    Identity Operators

    Practice using identity operators to compare object identity.

    # Create some variables
    a = [1, 2, 3]
    b = [1, 2, 3]
    c = a

    # Use identity operators:
    # 1. Check if a is b
    # 2. Check if a is c
    # 3. Check if a is not b
    # 4. Check if b is not c
    # 5. Compare with None using is operator
  • Use is to check object identity
  • Use is not to check if objects are different
  • Compare lists with same content but different objects
  • Compare variables that reference the same object
  • Check if a variable is None
  • Print all results with explanations
  • Hint

    Identity operators (is, is not) compare whether two objects are the same, not whether they have the same value.

    Exercise 6

    Membership Operators

    Practice using membership operators to check for element presence.

    # Create some collections
    fruits = ["apple", "banana", "cherry"]
    message = "Hello, World!"
    person = {"name": "John", "age": 30, "city": "New York"}

    # Use membership operators:
    # 1. Check if "banana" is in fruits
    # 2. Check if "orange" is not in fruits
    # 3. Check if "World" is in message
    # 4. Check if "name" is in person keys
    # 5. Check if "John" is in person values
  • Use in to check for presence in a sequence
  • Use not in to check for absence from a sequence
  • Check for substrings in a string
  • Check for keys in a dictionary
  • Check for values in a dictionary
  • Print all results with explanations
  • Exercise 7

    Bitwise Operators

    Practice using bitwise operators to manipulate binary values.

    # Define some values
    a = 0b1100 # 12 in decimal
    b = 0b1010 # 10 in decimal

    # Use bitwise operators:
    # 1. Bitwise AND (&)
    # 2. Bitwise OR (|)
    # 3. Bitwise XOR (^)
    # 4. Bitwise NOT (~)
    # 5. Left shift (<<)
    # 6. Right shift (>>)