Pathwise

Intro to Python · Lesson 2 of 12 · 12 min

Variables: names for values

A variable is a name that points at a value so you can use it later. Learn how = stores a value, how to change it, and why changing one variable doesn't quietly update another.

ASSIGNMENT

price = 5 means: store 5 under the name price

The = sign in Python is assignment, not the equals of maths. Python first works out whatever is on the right, then stores the result under the name on the left. After that, writing price anywhere in your program means 5, until you store something else there.

After price = 5, the line print(price * 2) prints 10.

price = 5
qty = 3
total = price * qty
print(total)

Output

15

Three names, three values. The third line reads the values stored in price and qty, multiplies them, and stores 15 under total. Only the last line prints anything; assignment itself shows nothing on the screen.

Check yourself

What does the line qty = 3 do?

  1. Checks whether qty is equal to 3
  2. Stores 3 under the name qty
  3. Prints 3 on the screen
  4. Prints the text qty = 3
Show the answer

Stores 3 under the name qty

Right. A single = stores the value on the right under the name on the left. Nothing is printed and nothing is checked.

REASSIGNMENT

Storing a new value replaces the old one

A variable holds one value at a time. Assign to the same name again and the old value is gone. That's why score = score + 5 is normal code even though it looks wrong as maths: Python reads the old score, adds 5, and stores the result back under score. You'll also see the shorthand score += 5, which does the same thing.

score = 10, then score = score + 5: now score holds 15, and the 10 is gone.

Check yourself

Mina's program has score = 10 and then score = score + 5. What does Python do first on the second line?

  1. It works out score + 5 using the old value, 10
  2. It sets score to 0, then adds 5
  3. It stops, because score can't equal score + 5
  4. It stores the text score + 5
Show the answer

It works out score + 5 using the old value, 10

Right. The right side always goes first: 10 + 5 is 15, and only then is 15 stored back under score.

Check yourself

price = 5
qty = 3
total = price * qty
print(total)
price = 6
print(total)
  1. 15
    15
  2. 15
    18
  3. 18
    18
  4. 15
    6
Show the answer
15
15

Step through it

  1. price = 5 makes a box called price

    The code panel on the left highlights the line running now, price = 5. On the right, Python has made a box labelled price and put 5 in it. From now on, price means 5.

  2. qty = 3 gets a box of its own

    Now qty = 3 is highlighted and the first line dims, because it's done. A second box, qty, holding 3, appears beside price: every name gets its own box.

  3. total is worked out from 5 and 3

    For total = price * qty, arrows run from the price and qty boxes into 5 * 3: Python uses the values in the boxes right now. The result, 15, goes into a new box called total.

  4. price = 6 changes price, but not total

    The last line, price = 6, strikes out the 5 in the price box and writes 6. total still holds 15, and the dim note beside it, 15 != 6 * 3, says it is not 18. Changing price doesn't recalculate total; only running the total line again would.

Check yourself

After total = price * qty, changing price automatically updates total.

Show the answer

False

False. total was worked out once, from the values price and qty had at that moment, and it keeps that result. To get a new total, run total = price * qty again after changing price.

price = 5
qty = 3
total = price * qty
print(total)
price = 6
total = price * qty
print(total)

Output

15
18

This time the total line runs again after price changes, so the second print shows 18. Try it: delete line 6, run it, and watch the second print go back to 15.

NAMING RULES

Names that Python accepts, and names people can read

A name can use letters, digits and the underscore _, but it can't start with a digit and can't contain spaces. Case matters: Age and age are two different variables. Words Python uses itself, like for and if, can't be names. Python style is snake_case: lowercase words joined by underscores, like total_price. Pick names that say what the value means; t works, total_price explains itself.

second and total_price are fine. 2nd, my name and for all stop with a SyntaxError.

Check yourself

Sort these variable names: which ones would Python accept?

  • total_price
  • age2
  • _count
  • 2nd_place
  • my name
  • for
Show the answer

Valid name: total_price, age2, _count

Not allowed: 2nd_place, my name, for

print(type(5))
print(type(5.0))
print(7 / 2)

Output

<class 'int'>
<class 'float'>
3.5

Whole numbers are int (integer) and numbers with a decimal point are float. type() tells you which one a value is. Division gives a float, which is why 7 / 2 is 3.5 and 10 / 2 is 5.0. Lesson 4 meets two more types, text and True/False.

Check yourself

  1. price = 5
  2. qty = 3
  3. total = price * qty
  4. print(totl)
Show the answer

The bug is on line 4.

Found it. totl is a typo for total. Python has never had a value stored under totl, so it stops with a NameError: name 'totl' is not defined. Recent versions of Python even add: Did you mean: 'total'?

Lesson recap

  • A variable is a name for a value; price = 5 stores 5 under the name price.
  • = is assignment: the right side is worked out first, then stored under the name on the left.
  • Assigning again replaces the old value, so score = score + 5 adds 5 to the current score.
  • Variables don't update each other: after price = 6, total stays 15 until its line runs again.
  • Names use letters, digits and _, can't start with a digit, and are case-sensitive; using a name that holds nothing is a NameError.

Keep it, don't just read it

Pathwise brings each idea back just before you'd forget it, with a quick question. Free on Android and on the web, in English and Persian.

Cafe Bazaar Myket Open the web app

All lessons in this course

  1. Your first program
  2. Variables: names for values
  3. Strings: working with text
  4. Input and types
  5. Making decisions with if
  6. Lists: many values in one place
  7. Loops: doing it again
  8. Functions: name a job, reuse it
  9. Dictionaries: look it up by name
  10. Reading error messages
  11. Saving and reading files
  12. Final project: an expense tracker