Open workspace
Python 1.3Code walkthrough8 min5 parts

Variables, Keywords and Identifiers

Bind names to objects, and name variables by Python's rules.

Loading your progress…Next: 1.4 Data Types

In this lesson5 parts

  1. 01What a variable is: assignment, names and values
  2. 02Names and objects: the namespace, id, two names for one object, and rebinding
  3. 03Multiple assignment, chained assignment, and swapping two variables
  4. 04Identifier rules, SyntaxError, the thirty five keywords and PEP 8 naming
  5. 05Recap and your challenge

Key terms

The words this lesson introduces, each in one line. The module’s glossary collects them all.

variable · assignment
a name given to a value · the instruction that gives it
object · namespace · bind
a value in memory with its type · the table of names · add a name with a reference to an object
identifier · keyword
the name of a variable · a word Python reserves (35 in 3.12)

Quiz 5 questions

Answer each one in your head, or on paper, before you check it. Getting one wrong here is how the lesson sticks.

  1. 01

    After total = 24.75 and previous_total = total, what is total is previous_total?

  2. 02

    Then total = 26.25. What is previous_total?

  3. 03

    Is item_2 a valid identifier? Is 2nd_item?

  4. 04

    What does x = true (small t) give?

  5. 05

    After a, b = 1, 2 and a, b = b, a, what are a and b?

Practice

Problems to solve in your own notebook. Each states the problem, not the steps: working out the steps is the exercise. Level A applies the lesson, B combines it with earlier ones, C stretches it.

The self-checking notebook for this lesson is Notebook 2 · Variables and Data Types.

Common mistakes

What you will see when it goes wrong, why it happens, and the fix.

Where it’s used

Where this lesson’s ideas turn up in real work.