1. The Basics

This page has the building blocks you’ll use in almost every program.


Printing to the screen

print() shows something on the screen. Put what you want to show inside the brackets.

print("Hello, William!")

Output:

Hello, William!

Text goes inside quote marks "like this". Numbers don’t need quotes:

print(42)
print("The creature has 42 energy")

Comments — notes for humans

A comment starts with a #. The computer ignores everything after it. Comments are notes to remind you (or someone else) what your code does.

# This line is a comment. The computer skips it.
print("This line runs")  # you can also add a comment at the end of a line

Write comments to explain the tricky bits. You don’t need one on every line.


Variables — labelled boxes

A variable is a box with a label. You put a value in it, and you can use the label later to get the value back.

creature_name = "Spike"
creature_energy = 100

print(creature_name)
print(creature_energy)

Output:

Spike
100

The = sign means “put the value on the right into the box on the left”. It does not mean “equals” like in maths.

Naming your variables

  • Use lowercase letters.
  • Join words with an underscore _. This is called snake_case.
  • Use names that mean something, so your code is easy to read.
# Good names — you can tell what they are 👍
player_score = 0
food_amount = 25
is_hungry = True

# Bad names — what do these even mean? 👎
x = 0
thing = 25
q = True

Changing a variable

You can put a new value in the box whenever you like.

creature_energy = 100
print(creature_energy)   # shows 100

creature_energy = 80     # the box now holds 80 instead
print(creature_energy)   # shows 80

You can even use the old value to make the new one:

creature_energy = 100
creature_energy = creature_energy - 20   # take 20 away
print(creature_energy)   # shows 80

There’s a shorter way to write that last bit:

creature_energy = 100
creature_energy -= 20    # same as: creature_energy = creature_energy - 20
print(creature_energy)   # shows 80

+= adds, -= takes away, *= multiplies.


Constants — values that don’t change

Sometimes you have a value that should never change while the program runs — like the biggest energy a creature can ever have. We call this a constant.

Python doesn’t force a constant to stay the same, but there’s a rule we all follow: write its name in CAPITAL LETTERS. That’s a signal to everyone: “don’t change this!”

MAX_ENERGY = 100
WORLD_WIDTH = 800
STARTING_FOOD = 25

print(MAX_ENERGY)

Variable or constant?

  • If the value changes as the program runs (like a creature’s energy going up and down) → it’s a variable, use snake_case.
  • If the value stays the same the whole time (like the biggest energy allowed) → it’s a constant, use CAPITALS.

Kinds of values

Values come in different types. You’ll mostly use these four:

creature_count = 5          # a whole number (called an "int")
creature_speed = 2.5        # a number with a decimal point (called a "float")
creature_name = "Spike"     # text (called a "string")
is_alive = True             # True or False (called a "boolean")

f-strings — putting values inside text

Often you want to print a message with a variable inside it. An f-string lets you do that. Put an f just before the opening quote, then wrap any variable in curly brackets { }.

creature_name = "Spike"
creature_energy = 80

print(f"{creature_name} has {creature_energy} energy left.")

Output:

Spike has 80 energy left.

You can even do maths inside the curly brackets:

food_eaten = 3
energy_per_food = 10

print(f"Eating gave the creature {food_eaten * energy_per_food} energy!")

Output:

Eating gave the creature 30 energy!

f-strings are the easiest way to build nice messages. You’ll use them all the time.


Asking the person a question

input() shows a question and waits for the person to type an answer. Whatever they type comes back as text.

player_name = input("What is your name? ")
print(f"Hello, {player_name}!")

⚠️ Important: input() always gives back text, even if the person types a number. If you need a real number to do maths with, wrap it in int() (for whole numbers) or float() (for decimals):

answer = input("How many creatures? ")
creature_count = int(answer)          # turn the text "5" into the number 5

print(f"You will have {creature_count + 1} creatures after one is born.")

✅ Quick recap

  • print() shows things on screen.
  • # starts a comment (the computer ignores it).
  • A variable is a labelled box: creature_energy = 100.
  • A constant never changes — write it in CAPITALS.
  • An f-string puts variables inside text: f"{name} has {energy}".
  • input() asks a question; use int() to turn the answer into a number.