Variables: giving names to values
Learn what a Python variable is, how to create one, change its value and use it in print(), with simple naming rules and exercises.
🎯 By the end of this lesson you will be able to:
- Create a variable and use it in your program
- Change a variable's value and predict what the program prints
- Choose clear variable names that follow Python's rules
Outline
What you'll learn and why it matters
Programs need to remember things: a learner's name, a price, a score. A variable is a name you use to refer to a value, so you can use it again and again. Almost every line of code you will ever write uses variables, so this idea is worth getting completely clear.
Your first variable
Look at this line:
learner_name = "Asha"learner_name is the name you use to refer to the value "Asha". The = sign means “make this name refer to this value”. It is not the “equals” you know from maths; read it as “learner_name is set to Asha”.
Now you can use the name instead of typing the value again:
learner_name = "Asha"
print(learner_name)
print("Welcome,", learner_name)Output
Asha Welcome, Asha
- Line 1 creates the variable. Nothing is shown yet.
- Line 2 prints the value the name refers to. Notice there are no quotation marks around
learner_name: we want the value, not the word “learner_name”. - Line 3 gives
printtwo things separated by a comma. Python prints both, with a space between them.
Changing a value
A variable can be given a new value. Python always uses the most recent one.
learner_name = "Asha"
print(learner_name)
learner_name = "Ravi"
print(learner_name)Output
Asha Ravi
Walk through it line by line, like Python does:
| Line | What happens | learner_name is now |
|---|---|---|
| 1 | Create the variable | "Asha" |
| 2 | Print it | "Asha" (prints Asha) |
| 3 | Give it a new value; the old one is forgotten | "Ravi" |
| 4 | Print it again | "Ravi" (prints Ravi) |
Variables can hold numbers too
A value does not have to be text. Numbers are written without quotation marks, and you can calculate with them:
lessons_done = 2
lessons_total = 15
print("Done:", lessons_done)
lessons_done = lessons_done + 1
print("Done:", lessons_done, "of", lessons_total)Output
Done: 2 Done: 3 of 15
Line 4 looks strange at first: lessons_done = lessons_done + 1. Python works out the right-hand side first (2 + 1 is 3), then makes lessons_done refer to that answer. This is how you count things in a program.
Naming rules and good names
| Rule | Allowed | Not allowed |
|---|---|---|
| Use letters, digits and underscores | city, score_2 | my-city (hyphen), my city (space) |
| Do not start with a digit | player1 | 1player |
| Upper and lower case are different | name and Name are two different variables | - |
| Do not use Python's own words | class_name | class, print (would hide the print function) |
Good names say what the value means. price is better than p; total_price is better than tp. Python programmers write names in lowercase with underscores between words, a style called snake_case.
Practice
Guided exercise
A greeting card
Create two variables, friend and city. Print a message that uses both. Then change city and print the message again.
- Set
friendto a name in quotes andcityto a city in quotes. - Use
print("Happy birthday,", friend)andprint("Greetings from", city). - Give
citya new value and print the second line again. - Before you run it, predict exactly what will be printed. Then run it and compare.
Show the solution
friend = "Meera"
city = "Kochi"
print("Happy birthday,", friend)
print("Greetings from", city)
city = "Chennai"
print("Greetings from", city)Output
Happy birthday, Meera Greetings from Kochi Greetings from Chennai
How to get there: The first two prints use the first values. After city = "Chennai", the name city refers to the new value, so the last line prints Chennai. friend did not change.
Your turn
Count your steps
Create a variable steps set to 4500. Add 2500 to it using steps = steps + 2500, then print Steps today: followed by the value.
Show the solution
steps = 4500
steps = steps + 2500
print("Steps today:", steps)Output
Steps today: 7000
How to get there: Python calculates 4500 + 2500 first, then stores the answer back in steps. Numbers have no quotation marks, so Python can add them.
Common mistakes
learner_name = "Asha"
print(learnr_name)Python stops with this error
NameError: name 'learnr_name' is not defined. Did you mean: 'learner_name'?
Misspelling a variable name
You see: NameError: name '...' is not defined
Fix: Check the spelling and capitals match exactly where you created it. Python suggests the closest name it knows.
Using a variable before creating it
You see: NameError on the line that uses it
Fix: Python runs top to bottom, so create the variable on an earlier line.
Putting quotes around the variable name
You see: It prints the word learner_name instead of Asha
Fix: Remove the quotes: print(learner_name).
Writing the value on the left: "Asha" = learner_name
You see: SyntaxError: cannot assign to literal
Fix: The name always goes on the left of =, the value on the right.
Recap
- A variable is a name that refers to a value:
learner_name = "Asha". =means “make this name refer to this value”. Python works out the right-hand side first.- Giving a variable a new value replaces the old one; Python always uses the latest value.
- Use clear snake_case names made of letters, digits and underscores, not starting with a digit.
Key terms
- Variable
- A name that refers to a value so you can use it later.
- Assignment
- Giving a variable a value with =.
- Tracing
- Going through code line by line and writing down what each variable holds.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. After these lines, what does `print(colour)` show? colour = "red" colour = "blue"
2. What is the value of `total` after `total = 10` and then `total = total + 5`?
3. Which is a valid Python variable name?
4. `print("city")` shows `city`, but you wanted the value of the variable. What should you write?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.