bonusscore1score = 102bonus = score3score = score + 54print(score, bonus)print(Score, bonus)int10int15✓ total_score✓ player2✗ 2player✗ my score✗ forNameError1010 + 515 10
Code card: score.py; the highlighted line is the one runningName tags: the names your program madeObjects: values in memory, with their type (int)Arrows: which object each name refers to right nowA variable is a name that refers to a value. The arrow from a name tag to an object in memory is the variable. Running score.py: line 1 makes the object 10 and ties score to it; line 2 ties bonus to the same object; line 3 works out score + 5 first, makes 15, and the score arrow swings over while bonus stays on 10; line 4 prints 15 10. In the last step line 4 says Score with a capital S: no such name exists, so Python stops with a NameError.

Make a variable

  1. A variable is a name that refers to a value. Line 1, score = 10, makes one. Python works out the right side of = first: 10, a whole number, is stored in memory as an object.

Share and move

Good names

Try it yourself

;; step 1/8 · line 1: score = 10
score = 10

# right side first: 10 is an int object in memory
>>> type(10)
<class 'int'>

Python calls every value an object. A variable does not hold its value like a box; it is a name tied to an object, and = changes which object a name is tied to. Whole numbers (int) and strings never change once made, so score + 5 always makes a new object. Two names can share any object, but sharing only shows when an object can change, such as a list.