Comparison Operators, Logical Operators, and… LISTS

STEP 3 — Comparison Operators, Logical Operators, and… LISTS

Alright, young coder — you’ve mastered if, elif, and else. Your game can now make basic decisions.

  • “Is the player alive?”
  • “Do they have enough coins?”
  • “Should I panic?”

But now we level up.

This step gives your game actual reasoning skills — like the ability to check multiple things at
once, compare values, and even look inside lists.

Let’s break it down.


1. Comparison Operators

These operators let your game compare two values:

  • > greater than
  • < less than
  • >= greater or equal
  • <= less or equal
  • == equal to
  • != not equal to

Example:

health = 50

if health <= 20:
    print("Critical health!")
Click Run to execute.

Your game is basically asking:
“Is your health low enough that I should start worrying?”


2. Logical Operators (a.k.a. “Two Conditions Walk Into a Game…”)

These let your game check multiple conditions at once:

  • and → both must be True
  • or → at least one must be True
  • not → reverses True / False
health = 40
has_shield = True

if health < 50 and has_shield:
    print("You can survive the next hit.")
Click Run to execute.

Your game is now thinking like a strategist.


3. Lists — Your Game’s Inventory System

Yes, lists belong in Unit 3.
Why?
Because conditions + lists = inventory checks, enemy lists, quest items, and all the good stuff you love building.

A list is a collection of items.

inventory = ["apple", "banana", "cherry", "date"]

if "banana" in inventory:
    print("You eat the banana and gain energy.")
Click Run to execute.

Checking if something is in a list:

if "banana" in inventory:
    print("You eat the banana and gain energy.")
    

This is how games decide:
  • “Do you have the key?”
  • “Do you have enough ingredients?”
  • “Is this enemy on the map?”

Lists make your game world feel real.


Practice (Your Turn!)

Mini-Challenge

You have this inventory:

inventory = ["potion", "sword", "map"]

Write a condition:

  • If "potion" is in the inventory → print "You heal yourself."
  • Otherwise → print "You have no potions left."
Write code and Run.