◀ Stage Select World 1 · Stage 1-02

Variables & Names

Give a value a name and it stops being a mystery

A program that only prints fixed text is a very expensive way to write a note. The moment you store a value under a name, you can reuse it, change it, and calculate with it. This stage covers what assignment actually does, why one mental model of it makes everything easier, and how to choose names that save the next reader from guessing.

Ready?

Step-by-step lessons

Storing Something and Getting It Back

Four short lessons: what an assignment does, the mental model that makes the rest obvious, how values change over time, and how to name things so the code explains itself.

1

Assignment: Putting a Value Under a Name

A variable is a name that points at a value. You make one by writing the name, a single =, and the value:

player_name = "Rookie"
level = 1
xp = 0

Nothing is printed. Assignment is silent — it stores something and moves on. From that point the name works anywhere the value would: print(level) shows 1.

Read = as "gets", never as "equals"

x = 5 is not a claim that x and 5 are the same. It is an instruction: x gets 5. Python works out the right-hand side first, then attaches the name on the left to the result. Hold on to that reading — it is what makes the line score = score + 10 sensible instead of mathematically impossible.

Fragile

Repeating the value

print(4.25) in six places. Change the price and you have six edits and one you will miss.

Sturdy

Naming it once

unit_price = 4.25, then use the name everywhere. One edit changes all six.

Quick check

What does the line total = 3 * 4 leave stored under total?

2

A Variable Is a Label, Not a Box

Most beginner material describes a variable as a box you put a value into. It is an easy picture and it will mislead you within a week. Python actually works the other way round: the value exists, and the name is a label stuck to it.

One value, many labels

a = b does not copy anything. Both names now point at the same value.

Reassigning moves a label

It never disturbs the value, and never disturbs any other name pointing at it.

Unlabelled values disappear

When the last name pointing at a value moves away, Python quietly reclaims it.

The payoff shows up immediately in a swap. Because Python builds the whole right-hand side before handing anything out, two names can trade values in one line, with no temporary variable:

one, two = two, one

The bigger payoff comes in World 2, when lists arrive. Two names pointing at the same list means changing it through one name changes what the other one sees — behaviour that is baffling under the box model and obvious under this one.

Quick check

After a = 5 then b = a then a = 9, what is b?

3

Values That Change Over Time

Assign to a name that already exists and it simply points somewhere new. That is how counters count and totals total:

score = 0
score = score + 10   # right-hand side first: 0 + 10, so score gets 10
score = score + 15   # 10 + 15, so score gets 25

That middle line is the one people stare at. It is not saying "score equals score plus ten" as a fact. It is saying: take the current score, add ten, and put the answer back under the same name.

Because this shape is so common, Python has a shorthand for it, called augmented assignment:

score += 10   # exactly the same as score = score + 10
lives -= 1
price *= 2

The name has to exist first

score += 10 reads the current value before writing the new one, so a score that has never been assigned gives you a NameError. Start your counters at zero explicitly. That one line also tells the reader what the starting point is meant to be.

Quick check

lives = 3, then lives -= 1 twice. What is lives?

4

Naming: The Rules, and the Conventions

The rules are what Python enforces. Letters, digits and underscores only; never starting with a digit; no spaces; and not one of Python's own reserved words like if, for or class. Case matters: score and Score are two different variables.

The conventions are what other Python programmers expect. Lowercase words joined by underscores — items_in_cart — which the community calls snake_case. Nothing breaks if you ignore it, but your code stops looking like Python.

Costs the reader

x = 250

Two hundred and fifty what? The reader has to scan the rest of the file to find out.

Pays the reader

item_price = 250

The line explains itself, and any line using it explains itself too.

The test that actually matters

A good name is one that lets someone read a line in the middle of your program and follow it without scrolling up. if days_until_renewal < 7: passes that test. if d < 7: does not, and the person who pays for it is usually you, some weeks later, with no memory of what d stood for.

Names are documentation you cannot forget to update.

Quick check

Which of these is a valid Python variable name that also follows the usual convention?

Type it yourself

Code Lab

Four exercises on storing values under names. Run as often as you like — a failed check costs nothing and tells you exactly what it expected.

0 of 4 cleared

Real Python runs right here in your browser — nothing to install, nothing sent to a server. The interpreter downloads once the first time you press Run, then stays cached.

01

Give a value a name

To do

A variable is a name pointing at a value. You create one by writing the name, an =, and the value:

player_name = "Rookie"

Read = as "gets", not as "equals". It is an instruction — put this value under this name — not a statement of fact.

Your task: create player_name holding the text Rookie, and level holding the number 1. Then print them on one line so the output reads exactly Rookie is on level 1.

your_code.py
Python
Hint

The text needs quotes, the number does not. For the output, print("...", "is on level", ...) — commas add the spaces for you.

Output

      
    02

    Change what a name points at

    To do

    A variable is not fixed. Assign to it again and the name simply points at the new value; the old one is gone.

    lives = 3
    lives = 2 # the name now points at 2

    This line trips up almost everyone at first: score = score + 10. It looks like a contradiction, but remember = means "gets". Python works out the right-hand side first (the old score plus ten), then puts that answer under the name score.

    Your task: start score at 0, print it, then add 10 and print it, then add 15 and print it. Three lines of output: 0, 10, 25.

    your_code.py
    Python
    Hint

    score = score + 10 puts the new total back under the same name. Python has a shorthand for it too: score += 10.

    Output
    
          
      03

      Swap two values

      To do

      Two players sit at positions one and two. They change seats. In most languages that needs a third, temporary variable — Python can do it in a single line:

      a, b = b, a

      Python builds the right-hand side first, out of the current values, and only then hands them back out to the names on the left. That is why nothing gets overwritten halfway.

      Your task: swap the values in one and two without changing the two lines that create them, then print them in that order.

      your_code.py
      Python
      Hint

      One line: one, two = two, one — then the existing print does the rest.

      Output
      
            
        04

        Names that explain themselves

        To do

        Python does not care what you call things. Everyone who reads your code afterwards does — and that includes you.

        The rules: letters, digits and underscores only; no starting with a digit; no spaces. The convention: lowercase words joined by underscores, which Python programmers call snake_case.

        Your task: this code works but reads like a puzzle. Rewrite it using the names item_price, item_count and total_cost, keeping the same numbers and the same result. Print the total on its own line.

        your_code.py
        Python
        Hint

        Same maths, better names: item_price = 250, item_count = 3, total_cost = item_price * item_count.

        Output
        
              
          Boss round

          Assignment: The Character Sheet

          One graded task. Pass every check and the stage is cleared and the title is yours — one more stage towards the Python Practitioner certificate.

          This one is graded. You can see 2 of the checks up front, and 3 stay hidden until you run. The hidden ones test the same job from angles you have not been shown, so code that genuinely solves the problem clears this and code shaped around the visible examples does not. Pass them all and the stage is yours.

          Build a character sheet

          To do

          A new player joins the dojo. Store their details in well-named variables, work out one value from the others, and print the sheet.

          Set up these variables, with exactly these names and values:

          • name — the text Kenji
          • belt — the text White
          • stages_cleared — the number 3
          • xp_per_stage — the number 200
          • total_xpcalculated from the two numbers above, not typed in as 600

          Then print exactly four lines:

          Name: Kenji
          Belt: White
          Cleared: 3
          XP: 600

          One space after each colon. Use commas inside print() and the spacing takes care of itself.

          your_code.py
          Python
          Hint

          total_xp = stages_cleared * xp_per_stage — let Python do the arithmetic, then print("XP:", total_xp).

          Output
          
                
            Lock it in

            Guess the Term

            Read the clues and name the concept. The fewer clues you need, the more points you score.

            Round 1 Score 0
            Keep it handy

            Variables Quick Reference

            The whole stage on one screen.

            Assignment

            =

            name = value

            Right-hand side first, then the name points at the result.

            +=

            score += 10

            Shorthand for score = score + 10. The name must already exist.

            ,

            a, b = b, a

            Swap in one line, no temporary needed.

            The Model

            Label, not box

            The name points at a value; it does not contain one.

            b = a copies nothing

            Two labels, one value.

            Reassigning moves a label

            Other names pointing at the old value are unaffected.

            Naming Rules

            Allowed

            Letters, digits, underscores. Must not start with a digit.

            Not allowed

            Spaces, punctuation, and Python's reserved words.

            Aa

            Case-sensitive

            score and Score are different names.

            Naming Style

            snake_case

            Lowercase words joined by underscores.

            Say what it means

            days_until_renewal, not d.

            UPPER_CASE

            Signals a value meant never to change.

            Notification