Skip to content
PythonExamples

LESSON 3 / 5 · 7 MIN

Variables

LET'S MAKE IT CLICK

Name your values

A variable is a name that refers to a value. Names let you reuse values and explain what your program is working with.

WATCH THE IDEA
Value10

A number to work with.

Namescore → 10

Give that value a meaningful label.

Readprint(score) → 10

Use the name to retrieve the current value.

A variable is a name that refers to a value.

Move through one idea at a time. Watch, experiment, and check your understanding along the way.

Give a value a name

WATCH THE IDEA
Evaluate10

Read the right-hand side.

Bindscore → 10

Attach the name to the value.

Read10

print(score) displays that value.

Assignment evaluates the value before binding the name.

In score = 10, Python evaluates the value on the right, then binds the name score to it. Reading score later gives you the value it currently refers to.

See the Python code
score = 10
print(score)
Keep in mind

A useful beginner picture is a label attached to a value. Python variables are names referring to objects, rather than fixed typed boxes.

Change what the name refers to

WATCH THE IDEA
Beforescore → 10

Read the current score.

Calculate10 + 5 → 15

Evaluate the right-hand side.

Afterscore → 15

Rebind score to the new value.

The old value is used in the calculation before the binding changes.

Assigning again changes the binding. In score = score + 5, the right-hand side uses the current score, calculates a new number, then assigns that result back to score.

Assignment is not a mathematical equality test. A single = assigns; == compares two values.

YOUR TURN TO EXPLORE

Watch a score change, line by line.

Choose a starting score and an increase. Step through the assignments and watch the current binding update.

example.pyPython
    CURRENT VALUESReady

    No assignments yet.

    OUTPUT

    No output yet.

    Predict the result, then take the first step.

    QUICK CHECK 1 / 2

    After score = 10 followed by score = score + 5, what is score?
    You can try again.

    Choose names that explain

    WATCH THE IDEA
    Unclearx = 5

    What does x represent?

    Descriptivelesson_count = 5

    The name tells you what the value means.

    Validletters + underscores

    Digits are allowed after the first character.

    Names should explain their purpose. total_score is valid; 2scores is not.

    Use names such as total_price or user_name instead of mysterious letters when they clarify the purpose. Python names can contain letters, digits, and underscores, but cannot start with a digit or be a keyword such as for.

    Names are case-sensitive: score and Score are separate names. Use lowercase words separated by underscores for ordinary variables.

    See the Python code
    user_name = "Asha"
    lesson_count = 5

    Names are not copies

    WATCH THE IDEA
    First namea → [1, 2]

    a refers to a list object.

    Second nameb = a

    b refers to that same list, without copying it.

    Shared objecta, b → [1, 2]

    There are two names and one list.

    Giving an object another name is different from creating a copy.

    For immutable numbers, reassignment gives a name a different value. With mutable objects such as lists, several names can refer to the same object; changing that object is different from rebinding a name.

    We will explore shared references later. For now, distinguish creating a value, assigning a name, and reading that name.

    QUICK CHECK 2 / 2

    Which is a valid Python variable name?
    You can try again.

    TAKE IT WITH YOU

    What you have learned

    • Assignment evaluates the right-hand side before updating the name.
    • Reassignment changes the value a name refers to.
    • Descriptive, case-sensitive names make code easier to read.

    Answer both quick checks correctly to mark this lesson complete. You can continue at any time.