Python course Β· Module 1: Python Basics

Operators - The Explorer's Tools

4 min read Β·
In this lesson5

Welcome back! Darwin here. Every explorer in the jungle needs tools - a machete to clear the trail, a compass to find direction, and a scale to measure discoveries. In Python, our tools are operators. They are symbols that perform actions on data: they count, compare, and combine information.

Arithmetic Operators - Counting in the Jungle

These are tools for mathematical calculations. Imagine counting animals, measuring distances, and dividing supplies among team members.

# Count animals from two camps
camp_a_animals = 12
camp_b_animals = 8

total = camp_a_animals + camp_b_animals      # addition -> 20
difference = camp_a_animals - camp_b_animals  # subtraction -> 4

Python knows all the basic operations:

distance = 30   # kilometers to cover
days = 4        # expedition days

print(distance * days)   # multiplication -> 120
print(distance / days)   # division -> 7.5 (always float!)
print(distance // days)  # floor division -> 7
print(distance % days)   # remainder (modulo) -> 2
print(distance ** 2)     # exponentiation -> 900

Remember: regular division / always returns a float (a number with a decimal point), even when it divides evenly. Floor division // truncates the decimal part, and modulo % gives the remainder. The remainder is super useful - for example, number % 2 tells you whether something is even (result 0) or odd (result 1).

Comparison Operators - Is It the Bigger Creature?

Sometimes an explorer must compare two values: is an elephant heavier than a lion? Did the temperature cross a safe threshold? Comparison operators always return a logical value: True or False.

elephant_weight = 5000   # kg
lion_weight = 190        # kg

print(elephant_weight > lion_weight)   # greater than -> True
print(elephant_weight < lion_weight)   # less than -> False
print(elephant_weight == lion_weight)  # equal? -> False
print(elephant_weight != lion_weight)  # different? -> True

temperature = 35
print(temperature >= 30)  # greater than or equal -> True
print(temperature <= 25)  # less than or equal -> False

Watch out for the trap! A single equals sign = is assignment (giving a value to a variable), while a double == is comparison (checking whether two values are the same). This is a common beginner explorer mistake.

species = "Python"      # = assigns the value
print(species == "Python")  # == compares -> True

Logical Operators - Combining Conditions

In the jungle we rarely make decisions based on a single fact. "We'll set off if it's warm and dry." "We'll take shelter if it's raining or there's a storm." To combine conditions we use logical operators: and, or, not.

is_warm = True
is_dry = False

# and -> True only when BOTH conditions are True
print(is_warm and is_dry)   # False (because it's not dry)

# or -> True when AT LEAST ONE condition is True
print(is_warm or is_dry)    # True (because it's warm)

# not -> reverses the value
print(not is_dry)           # True (negation of False)

We can combine comparisons with logic to build complex research conditions:

temperature = 28
humidity = 60

# Are conditions perfect for an expedition?
good_weather = temperature > 20 and humidity < 80
print(good_weather)  # True

Practice

Try out operators in the editor. Calculate expedition statistics and compare data from two camps:

Code for this lesson: main.py
# Operators - expedition statistics

camp_a_animals = 12
camp_b_animals = 8

# TODO: calculate the total animals from both camps
total = camp_a_animals + camp_b_animals
print(f"Total animals: {total}")

# TODO: calculate the difference (camp A - camp B)
difference = camp_a_animals - camp_b_animals
print(f"Difference: {difference}")

# TODO: compare which camp has more animals (use >)
a_has_more = camp_a_animals > camp_b_animals
print(f"Does camp A have more? {a_has_more}")

# TODO: check whether the total is even (modulo % 2 == 0)
is_even = total % 2 == 0
print(f"Is the total even? {is_even}")

Summary

In this lesson you learned the explorer's tools:

  • Arithmetic operators: +, -, *, /, //, %, **
  • Comparison operators: >, <, >=, <=, ==, !=
  • Logical operators: and, or, not
  • The difference between = (assignment) and == (comparison)

In the next lesson Darwin will show you how to make decisions in code based on these comparisons!

Docs entries used in this lesson

Spotted a mistake in this lesson?

Check yourself

Answer the questions from this lesson. Pick an answer to see right away whether it is correct.

  1. 1. What is the result of the expression 17 % 5?

  2. 2. What is the result of the expression 2 ** 3?

  3. 3. Which operator is used to COMPARE two values?

Hands-on tasks in the game

  • Horizontal ordering

    Arrange the expression checking two conditions:

Useful articles