Names Point at Values
A value you cannot refer to again is not much use. If a program works out a total, it needs a way to hold on to that total and use it on the next line. A variable is a name you give to a value so you can use it later. You make one with a single equals sign: price = 12 gives the name price to the value 12.
The best picture of what that line does is a luggage tag. The value 12 is the suitcase. The name price is a tag tied to it. You can read the tag to find the suitcase, move the tag to another suitcase, or tie a second tag to the same one. That picture, a label rather than a box, is how Python really works, and one surprise in Chapter 6 makes sense only with it.
Assignment
The single equals sign is called assignment, and it does not mean "is equal to". It means: work out the value on the right, then tie the name on the left to it. The right side always goes first. After price = 12, any line that mentions price gets 12.
Changing a Value
A name can be tied to a new value at any time. The line price = price + 1 looks impossible as mathematics, but it is an instruction, not an equation. Read it right to left: take the value price points at now, add one, and tie the name price to the result. In the session below, printing price before and after shows 12 and then 13.
>>> price = 12 >>> print(price) 12 >>> price = price + 1 >>> print(price) 13
Good Names
Names follow a few firm rules. They may contain letters, digits and underscores; they may not start with a digit; and they may not contain spaces. Break a rule and Python stops before running anything. In the session below, a name starting with the digit 2 produces a SyntaxError about an invalid decimal literal: Python saw a digit and expected a number, not a name.
>>> 2nd_price = 5
File "<python-input-4>", line 1
2nd_price = 5
^
SyntaxError: invalid decimal literal
Beyond the rules there is a convention that almost all Python code follows. Names are written in lower case with underscores between words, such as monthly_total or first_name. The style is called snake_case. And a name should say what the value means. The two versions below do exactly the same thing. Only one of them can be understood a week later.
a = 48.5 b = 4 c = a / b bill_total = 48.5 people = 4 share_each = bill_total / people
Short names do not make a program faster, and long names do not make it slower. Names cost nothing to run and everything to read.
Labels, Not Boxes
Because a name is a label, two names can point at the same value. After a = 12 and b = a, both names are tied to the same 12. With numbers and text that never causes a problem, because those values cannot be changed in place. With lists, which arrive in Chapter 4, it causes one of the classic surprises, and Chapter 6 comes back to it with this exact picture.
- "The equals sign means equals." In Python it means "tie this name to this value". Asking whether two things are equal uses two equals signs, which Chapter 3 introduces.
- "
x = x + 1can never be true." It is not a statement of fact but an instruction: compute the right side using the current value, then move the name to the result. - "Short names are quicker to run." The computer does not care how long a name is. People reading the code care a great deal.
- Good names are the cheapest documentation a program will ever have: they explain the code at the exact place it is read.
- The label picture makes lists and dictionaries predictable later, including the one behaviour that surprises almost everyone.
Knowledge Check
What does the line price = 12 do?
- It checks whether price is currently equal to the number 12
- It ties the name price to the value 12 for later use
- It creates a box that can hold only the number 12
- It prints the number 12 in the terminal straight away
After price = 12 and then price = price + 1, what does price point at?
- 12, because a name cannot be changed once it is set
- An error, because a name cannot appear on both sides
- The text 121, because the numbers are joined
- 13, the old value of price with one added to it
Which of these is a valid Python name?
monthly_total, letters with an underscore2nd_price, which begins with a digitmonthly total, two words with a spacetotal-price, joined with a hyphen
Why is bill_total a better name than a?
- Longer names make the program run more quickly
- Python only accepts names of more than one letter
- It says what the value means to a person reading it
- Short names are reserved for Python's own use
You got correct