DEV Community

Rubyfield press
Rubyfield press

Posted on AI-assisted

Python Variables Explained for Complete Beginners (With Pictures in Your Head)

Why variables trip people up

Variables are the first idea in programming where you have to picture something you can't see. Most tutorials show x = 5, say "x is now 5" and move on. Then the beginner sees x = x + 1 and thinks the computer has broken maths.

This post explains variables the way we teach them in Python Made Simple: an everyday picture first, then the real Python terms, then code you can run. You need Python installed and any editor. We use the free Thonny editor because it is made for beginners.

Picture a labelled box

When you move house, you write a label on every box: Kitchen, Books, Winter coats. Weeks later you don't open every box to find a cookery book. You look for the one labelled Books.

A variable works the same way. It has two halves:

  • a name: the label on the outside, such as customer_name
  • a value: the information it holds, such as "Priya Shah"

Why bother? Imagine greeting a customer by name in ten places in a program. Without a variable you type the name ten times. With one, you type it once, and changing it in one place changes it everywhere.

Your first variable

Type this into a new file and run it:

customer_name = "Priya Shah"
print(customer_name)
Enter fullscreen mode Exit fullscreen mode

Output:

Priya Shah
Enter fullscreen mode Exit fullscreen mode

The first line is an assignment statement. The = sign is the assignment operator, and Python always carries it out in two steps:

  1. Work out the value on the right.
  2. Attach the name on the left to that value.

The most important thing to unlearn from school maths: in Python, a single = does not mean "equals". It is an instruction. Read the line aloud as "customer_name is set to Priya Shah".

The label or the text?

Quotation marks change everything:

customer_name = "Priya Shah"
print("customer_name")
print(customer_name)
Enter fullscreen mode Exit fullscreen mode

Output:

customer_name
Priya Shah
Enter fullscreen mode Exit fullscreen mode

Rule of thumb: quotation marks mean these exact characters. No quotation marks means the value attached to this name.

Several variables together

shop_name = "Corner Bakery"
customer_name = "Priya Shah"
number_of_items = 3

print("Welcome to", shop_name)
print("Customer:", customer_name)
print("Items in basket:", number_of_items)
Enter fullscreen mode Exit fullscreen mode

Output:

Welcome to Corner Bakery
Customer: Priya Shah
Items in basket: 3
Enter fullscreen mode Exit fullscreen mode

Text goes in quotation marks; the number 3 doesn't. The comma inside print() separates the items, and Python adds one space between them for you.

Changing a value

It's called a variable because its value can vary. Assign a new value to the same name and the old one is replaced:

favourite_drink = "tea"
print("My favourite drink is", favourite_drink)

favourite_drink = "coffee"
print("My favourite drink is now", favourite_drink)
Enter fullscreen mode Exit fullscreen mode

Output:

My favourite drink is tea
My favourite drink is now coffee
Enter fullscreen mode Exit fullscreen mode

The line that looks impossible

Now the one that confuses everyone:

number_of_items = 3
number_of_items = number_of_items + 2
print("Items in basket:", number_of_items)
Enter fullscreen mode Exit fullscreen mode

Output:

Items in basket: 5
Enter fullscreen mode Exit fullscreen mode

In maths, "number = number + 2" can never be true. In Python it makes perfect sense, because = is the same two-step instruction as before:

  1. Work out the right-hand side: number_of_items + 2 is 3 + 2, which is 5.
  2. Attach the name on the left to the result: number_of_items now refers to 5.

This is how every program keeps a running total: a score going up, a basket filling, a balance changing.

Python also has a shortcut for it. score += 5 means exactly the same as score = score + 5, and there are matching versions for subtracting (-=), multiplying (*=) and dividing (/=).

Naming your variables

Python is strict about names. A variable name:

  1. can contain letters, digits (0–9) and the underscore _
  2. cannot start with a digit: item_2 is fine, 2nd_item is not
  3. cannot contain spaces or symbols such as -, ! or ?
  4. cannot be a keyword Python already uses, such as if, for, class, True or None
  5. is case-sensitive: total_price and Total_price are different names

A name can follow every rule and still be unhelpful:

Valid but unclear Clear and meaningful
x number_of_items
tp total_price
thing delivery_address

Python's official style guide, PEP 8, recommends lower-case words joined by underscores, a style called snake_case. Pick names that say what the value means; future you will thank you.

Three mistakes everyone makes (and the fixes)

Error messages are clues, not criticism. Read the last line first; it usually tells you the most.

1. A typo in the name

customer_name = "Priya Shah"
print(custmer_name)
Enter fullscreen mode Exit fullscreen mode
NameError: name 'custmer_name' is not defined. Did you mean: 'customer_name'?
Enter fullscreen mode Exit fullscreen mode

Python matches names letter for letter, capitals included, so custmer_name is a brand-new name it has never seen. Fix: take Python's suggestion and correct the spelling.

2. Using a variable before creating it

print(delivery_address)
delivery_address = "12 Station Road"
Enter fullscreen mode Exit fullscreen mode
NameError: name 'delivery_address' is not defined
Enter fullscreen mode Exit fullscreen mode

Python runs top to bottom, so at line 1 the name doesn't exist yet. Fix: assign first, use second.

3. A space in the name

customer name = "Priya Shah"
Enter fullscreen mode Exit fullscreen mode
SyntaxError: invalid syntax
Enter fullscreen mode Exit fullscreen mode

Python sees two separate words. Hyphens (first-name) and leading digits (2nd_place) fail too. Fix: customer_name.

Try it yourself

Predict the answer before you run this. Start with 20 coins, spend 7, then double what's left:

coins = 20
coins -= 7
coins *= 2
print(coins)
Enter fullscreen mode Exit fullscreen mode

Write your prediction in the comments, then run it. (The answer is at the very end of this post.)

Bonus challenge: create two variables, morning_drink = "coffee" and evening_drink = "hot chocolate", then write code that swaps them so the morning drink is hot chocolate and the evening drink is coffee. It's trickier than it looks. Share your solution below.

What's next

You now know what a variable is, how = really works, how to change a value and how to name things well. That's the foundation of every program you'll ever write.

Next in this series: why 3 + 4 gives 7 but "3" + "4" gives "34", which is all about data types.

This post is adapted from Chapter 5 of Python Made Simple: A Visual, Hands-On Guide to Programming for Complete Beginners, a full-colour book where every idea gets a plain-English explanation and a labelled diagram on the facing page. It goes from your very first program to building a calculator, a quiz game and an expense tracker. Every example was tested on Python 3.15 and 3.14.

👉 Read the free sample on Leanpub

Answer to the coins puzzle: 26. Starting at 20, spending 7 leaves 13, and doubling 13 gives 26.

Top comments (0)