We use cookies to enhance your experience on the site
CodeWorlds

Operators - The Explorer's Tools

Welcome back, @name! 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.

1# Count animals from two camps
2camp_a_animals = 12
3camp_b_animals = 8
4
5total = camp_a_animals + camp_b_animals      # addition -> 20
6difference = camp_a_animals - camp_b_animals  # subtraction -> 4

Python knows all the basic operations:

1distance = 30   # kilometers to cover
2days = 4        # expedition days
3
4print(distance * days)   # multiplication -> 120
5print(distance / days)   # division -> 7.5 (always float!)
6print(distance // days)  # floor division -> 7
7print(distance % days)   # remainder (modulo) -> 2
8print(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`.

1elephant_weight = 5000   # kg
2lion_weight = 190        # kg
3
4print(elephant_weight > lion_weight)   # greater than -> True
5print(elephant_weight < lion_weight)   # less than -> False
6print(elephant_weight == lion_weight)  # equal? -> False
7print(elephant_weight != lion_weight)  # different? -> True
8
9temperature = 35
10print(temperature >= 30)  # greater than or equal -> True
11print(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.

1species = "Python"      # = assigns the value
2print(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`.

1is_warm = True
2is_dry = False
3
4# and -> True only when BOTH conditions are True
5print(is_warm and is_dry)   # False (because it's not dry)
6
7# or -> True when AT LEAST ONE condition is True
8print(is_warm or is_dry)    # True (because it's warm)
9
10# not -> reverses the value
11print(not is_dry)           # True (negation of False)

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

1temperature = 28
2humidity = 60
3
4# Are conditions perfect for an expedition?
5good_weather = temperature > 20 and humidity < 80
6print(good_weather)  # True

Practice

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

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! 🧭🌴

Go to CodeWorlds