Skip to content
BENIBIX Learn

Variables and Assignment

Level 2 · Values and variables

How a variable keeps a value under a name, and how the = sign puts it there. When a constant is better, and why hard-coding hurts.

Course contentsLesson 5 of 18

After this lesson you can

  • Create a variable and change its value
  • Read the = sign the way Python does
  • Replace a hard-coded value with a constant

11 mincovers 4 glossary entries

Worth reading first: Statements and Expressions

How a program remembers a value

A variable is a name that holds a value. You write score = 0, and from then on the name score stands for 0. Later you can write score = 10, and the old value is replaced. A program can hold thousands of variables, each with its own name and value.

The step that puts a value into a variable is called assignment. In Python it uses one = sign. Python always works out the right side first, then stores the result under the name on the left. So score = score + 10 means take the old score, add 10, and store the answer back.

That line looks wrong to anyone who knows maths, where score can never equal score plus 10. In code, = does not mean equal. It means put this value in that box. Checking whether two values are equal uses two signs, ==, which comes later in the course.

Names follow a few rules. A name can hold letters, numbers and the _ sign, but it cannot start with a number. It cannot be a keyword. Case counts, so Score and score are two different names. Python style is to write names in lower case, with _ between words, like total_price.

Some values should never change while the program runs, such as a tax rate or the number of days in a week. A constant is a name for one of these. Python has no lock for them. Instead, people write the name in capitals, like TAX_RATE = 0.2, as a sign to leave it alone.

The opposite habit is hard-coding: typing a value straight into the sums, wherever it is needed. A price list might hold 0.2 in 15 different places. When the rate changes, someone has to find all 15, and one always gets missed.

A common mistake is using a name before giving it a value. Python reads from the top down, so print(total) above total = 5 fails with a NameError. The fix is to assign first, then use.

python
TAX_RATE = 0.2          # a constant, in capitals
price = 50              # assignment
price = price + 10      # the right side first: 50 + 10
tax = price * TAX_RATE
print(price, tax)
# 60 12.0
The right side is worked out first, then stored under the name on the left.
A variable is a named box whose value can be swapped. A constant is a box you agree never to change.

Worked example

Lucy's shop changes its tax rate

Lucy's shop script had the rate 0.2 typed into 9 lines. When the rate went to 0.15, she changed 8 of them and missed one.

One page showed the old price for a week. She then put TAX_RATE = 0.15 at the top and used the name on all 9 lines. The next change will take 1 edit.

The words for this lesson4

Check yourself

What does count = count + 1 do in Python?

Finished this lesson?

Marking it complete also marks its 4 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.