RizTech Academy logo
RizTech Academy
Variables and Data TypesLesson 1 of 515 min

Variables and naming things well

A program that can only work with values you typed out by hand is not much of a program. Variables are how you hold on to a value now and use it later, and they are the first thing that makes code feel like code.

What a variable actually is

age = 25

Read that as "the name age now refers to the value 25". The = is not the equals sign from mathematics — it is not claiming the two sides are equal. It is an instruction: make this name point at this value.

Once a name exists, you use it wherever you would have used the value:

age = 25
print(age)
print(age + 5)
25
30

A lot of tutorials describe a variable as a box that holds a value. That picture is comforting and it will mislead you later, when two names refer to the same list and changing one appears to change the other. The accurate picture is a label stuck onto a value. Several labels can be stuck on the same thing.

We come back to this properly in the lists lesson. For now, "a name pointing at a value" is the idea to carry forward.

Names can be re-pointed

score = 10
print(score)

score = 20
print(score)
10
20

Nothing was overwritten in place. The name score simply stopped pointing at 10 and started pointing at 20.

This is also legal, and it is worth staring at for a second:

count = 5
count = count + 1
print(count)
6

As mathematics, count = count + 1 is nonsense. As an instruction it is perfectly clear: work out count + 1, which is 6, then point count at that. The right-hand side is always worked out first.

This is so common that there is a shorthand:

count = 5
count += 1   # same as count = count + 1
count -= 2   # same as count = count - 2
count *= 3   # same as count = count * 3
print(count)
12

The rules

Python enforces only a few:

  • Names may contain letters, digits and underscores.
  • They may not start with a digit. total2 is fine, 2total is not.
  • They are case-sensitive. age, Age and AGE are three different names.
  • They may not be one of Python's own keywords: if, for, class, import, return, True, and about thirty more.

Break the last rule and the message is unhelpfully terse:

class = "Physics"
SyntaxError: invalid syntax

Python cannot tell you "class is a keyword" because by the time it reads class it is already expecting a class definition. When a SyntaxError appears on a line that looks obviously fine, check whether you have used a reserved word. class, type, id, list, sum and input are the ones that catch people — the last four are not keywords but are names Python already uses, and reusing them causes strange failures later rather than an immediate error.

Naming things well

This is the part that actually matters, and it is not fussiness.

You will read code far more often than you write it — your own code, three weeks later, with no memory of what you were thinking. Good names are the difference between understanding it immediately and reconstructing it from scratch.

Compare:

d = 86400
n = 5
t = d * n

with:

seconds_per_day = 86400
number_of_days = 5
total_seconds = seconds_per_day * number_of_days

The second is longer and requires no explanation. The first needs a comment, and that comment will eventually be wrong.

The convention in Python is snake_case: lowercase words joined by underscores. Not totalSeconds (that is JavaScript and Java), not TotalSeconds (that is a class name in Python). Follow it — consistency across a codebase is worth more than your personal preference.

Values that never change are written in capitals:

MAX_RETRIES = 3
TAX_RATE = 0.18

Python will not stop you reassigning these. The capitals are a message to other people — and to you — that you did not intend to.

A few practical rules that hold up:

  • Say what it is, not what type it is. user_email beats email_string.
  • Avoid single letters except for a loop counter i or a coordinate x. n in the middle of a forty-line function is a small act of cruelty.
  • Do not abbreviate to save typing. Your editor autocompletes; your reader does not have autocomplete for your brain. usr_cnt costs more than it saves.
  • Booleans read as a question. is_active, has_permission, was_found. Then if is_active: reads as English.
  • Longer is fine when it earns it. days_until_expiry is a better name than days, and nobody was ever confused by it.

Several names at once

Python lets you assign more than one name in a single line:

x, y = 10, 20
print(x, y)
10 20

Used sparingly this is fine. Used for five unrelated values on one line it is just a way of writing dense code, so keep it for things that genuinely belong together — like a pair of coordinates.

The genuinely useful case is swapping:

a = 1
b = 2
a, b = b, a
print(a, b)
2 1

In most languages that swap needs a third temporary variable. Here the right-hand side is worked out completely before anything is assigned, so it just works.

Using a name before it exists

print(total)
NameError: name 'total' is not defined

This is the error from the first module, and now you can read it exactly: you used a name Python has never been told about. Two causes, near enough always:

  1. A typo. You assigned total_price and asked for total_prce.
  2. Order. You used the name on a line that runs before the line that assigns it. Python reads top to bottom and does not look ahead.

Practice

  1. Create variables for your name, your age and your city, then print a sentence using all three.

  2. Write a small currency converter: a variable for an amount in rupees, one for an exchange rate, and one for the result. Print all three with labels.

  3. Take this and rename everything so it can be understood without the comment:

    # price after 18% tax
    p = 1200
    r = 0.18
    f = p + (p * r)
    
  4. Start with total = 100. Using only +=, -= and *=, get it to exactly 250 in three steps.

  5. Cause a NameError on purpose, then fix it. Then cause one by using a name one line too early, and notice the message is identical — the error tells you what is wrong, never why.

Next: numbers, and a well-known trap that catches everybody once.

Stuck on this lesson?

Being stuck is part of it — but being stuck alone for three days is not. Our internship programme pairs this curriculum with code review and one-to-one help from working developers, and it is free.

About the internship