Lesson 3 of 30 · Getting Started
Variables
Learn how Python variables store information. Create, change and name variables correctly, with runnable examples and the mistakes beginners make most.
- 6 min read
- Beginner
- Reviewed
- What a variable is and why every program needs them
- How to create a variable and change its value
- The rules and good habits for naming variables
What is a variable?
A variable is a name that holds a piece of information. Think of a labelled box: the label is the variable's name, and whatever you put inside is its value.
You create a variable with the equals sign.
name = "Ada" age = 36 print(name) print(age)
Read name = "Ada" as "name is set to Ada". From then on, whenever Python sees name, it uses the value inside.
Why variables are useful
Without variables you would have to type the same value again and again. With a variable, you write it once and reuse it.
name = "Ada"
print("Hello,", name)
print(name, "is learning Python.")
print("Well done,", name)Change "Ada" to your own name on the first line and run it again. All three lines update, even though you changed only one.
Variables can change
The word variable means "able to vary". You can give a variable a new value at any time. The old value is replaced.
score = 10
print("Score:", score)
score = 25
print("Score:", score)
score = score + 5
print("Score:", score)Look closely at score = score + 5. Python works out the right side first (25 + 5 is 30), then stores the result back in score.
Naming your variables
Python has a few rules for names.
| Rule | Allowed | Not allowed |
|---|---|---|
| Use letters, numbers and underscores | user_name, score2 | user-name, user name |
| Do not start with a number | player1 | 1player |
| Do not use Python's own words | my_class | class, print |
Names are also case-sensitive: age, Age and AGE are three different variables.
Beyond the rules, good programmers follow two habits:
- Use meaningful names.
priceis better thanp.total_priceis better still. - Use lowercase with underscores between words. This style is called snake_case, and it is the standard in Python.
first_name = "Grace" favourite_language = "Python" years_of_experience = 3 print(first_name, "has used", favourite_language, "for", years_of_experience, "years.")
Common mistakes
Fix the two bugs in this program. One is a missing variable, the other a spelling mistake.
city = "Nairobi"
print("I live in", citty)
print("My country is", country)Create two variables, name and age, then use them to print Ada is 36 years old.
# 1. Create a variable called name with the value "Ada" # 2. Create a variable called age with the value 36 # 3. Print them so the output is exactly: Ada is 36 years old
What is printed by this code? x = 5 then x = x + 2 then print(x)
Summary
- A variable is a name that stores a value.
- Create one with
=, for exampleage = 36. - A variable can be given a new value at any time.
- Names use letters, numbers and underscores, and cannot start with a number.
- Choose clear, lowercase names such as
total_price.
Next: the different kinds of values Python can store.
Finished reading? Tick it off to track your progress.