Skip to content

Variables and types

Imagine you're writing a program that greets a user by name. You need somewhere to keep that name so you can use it. Or maybe you're tracking a score in a game — the number needs to live somewhere while the program runs.

That's what a variable does. It's a named place to store a piece of information.

Saving a value

You create a variable by writing a name, then =, then the value you want to store:

python
name = "Alice"

After this line runs, Python remembers that name holds "Alice". You can then use name anywhere in your code and Python will substitute the value in.

python
print(name)
# Alice

One thing that trips people up early: that = sign doesn't mean "is equal to" like in maths. It means store this value in this name. You're not asking a question — you're giving an instruction. Read it as "name gets Alice".

Picking a good name

You get to choose the name. The key is to make it describe what it holds — code is read far more than it's written, so clear names save you confusion later.

python
favourite_colour = "blue"
age = 25
city = "Melbourne"

A few rules Python enforces:

  • No spaces — use underscores instead: first_name not first name
  • Start with a letterscore2 is fine, 2score isn't
  • Case mattersName and name are two completely different variables

And one convention the Python community follows (not enforced, just expected):

  • Use lowercase with underscores: user_name, total_price, is_active

Four kinds of values

Python treats different kinds of information differently. Here are the four you'll use all the time.

Text

Any text value goes inside quote marks:

python
greeting = "Hello!"
country = "Australia"
message = "See you tomorrow"

The quotes are how Python tells the difference between text and code. Without them, Python would think Hello was the name of a variable, not the word hello. You can use single or double quotes — they behave identically, so pick one and be consistent.

This kind of value has a name in programming: a string. You'll hear that term a lot.

Numbers

Numbers don't get quotes:

python
age = 28
year = 2024
price = 4.99
discount = 0.15

There are two flavours. Integers are whole numbers — no decimal point. Floats are numbers with a decimal point. Python handles them slightly differently under the hood, but for now just know: if you need decimals, use a decimal point.

True or False

Some information is simply one of two states — on or off, yes or no:

python
is_logged_in = True
has_paid = False
is_available = True

These are called booleans and they're incredibly useful. A lot of decisions in code come down to "is this true or not?" Notice they're capitalised: True and False, not true and false.

Nothing

Sometimes you want to say "this variable exists, but there's no value in it yet":

python
result = None
winner = None

None is Python's way of representing the absence of a value. It's not zero, it's not empty text — it's genuinely nothing. You'll often use it as a placeholder before you have a real value to store.

Updating a variable

Variables aren't fixed. You can give them a new value any time:

python
score = 0
print(score)  # 0

score = 10
print(score)  # 10

score = score + 5
print(score)  # 15

Each time you use =, the old value is replaced. That last line is worth reading carefully: score = score + 5 means "take the current value of score, add 5 to it, and store that as the new score". It looks circular but it's perfectly valid — the right side is calculated first, then stored.

Putting variables to work

Variables become powerful when you use them together:

python
first_name = "Alice"
last_name = "Smith"
full_name = first_name + " " + last_name

print(full_name)
# Alice Smith
python
price = 12.00
quantity = 3
total = price * quantity

print(total)
# 36.0

This is the core idea: store values, name them clearly, then use those names to do something useful.

Quick reference

What you want to storeHow to write itWhat it's called
Text"hello"string
Whole number42integer
Decimal number3.14float
Yes / NoTrue / Falseboolean
Nothing yetNoneNoneType

Practice