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.
Output:
Text goes inside quote marks "like this". Numbers don’t need quotes:
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.
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.
Output:
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.
Changing a variable
You can put a new value in the box whenever you like.
You can even use the old value to make the new one:
There’s a shorter way to write that last bit:
+= 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!”
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:
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 { }.
Output:
You can even do maths inside the curly brackets:
Output:
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.
⚠️ 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):
✅ 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; useint()to turn the answer into a number.