Nishaglobal Education logo

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.

Lesson 2 of 1420 minQuiz: 4 questions

🎯 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
  1. What you'll learn and why it matters
  2. Your first variable
  3. Changing a value
  4. Variables can hold numbers too
  5. Naming rules and good names
  6. Practice
  7. Common mistakes
  8. Recap
  9. Quiz
  10. Mark lesson complete

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:

Python
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:

Python
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 print two things separated by a comma. Python prints both, with a space between them.
After learner_name = "Asha"learner_name"Asha"After learner_name = "Ravi"learner_name"Ravi""Asha" is forgotten
A variable is like a name tag. learner_name points to the value "Asha"; after learner_name = "Ravi", the same tag points to the new value.

Changing a value

A variable can be given a new value. Python always uses the most recent one.

Python
learner_name = "Asha"
print(learner_name)
learner_name = "Ravi"
print(learner_name)

Output

Asha
Ravi

Walk through it line by line, like Python does:

LineWhat happenslearner_name is now
1Create the variable"Asha"
2Print it"Asha" (prints Asha)
3Give it a new value; the old one is forgotten"Ravi"
4Print 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:

Python
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

RuleAllowedNot allowed
Use letters, digits and underscorescity, score_2my-city (hyphen), my city (space)
Do not start with a digitplayer11player
Upper and lower case are differentname and Name are two different variables-
Do not use Python's own wordsclass_nameclass, 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.

  1. Set friend to a name in quotes and city to a city in quotes.
  2. Use print("Happy birthday,", friend) and print("Greetings from", city).
  3. Give city a new value and print the second line again.
  4. Before you run it, predict exactly what will be printed. Then run it and compare.
Show the solution
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
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

A small typo in the name
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. 1. After these lines, what does `print(colour)` show? colour = "red" colour = "blue"

  2. 2. What is the value of `total` after `total = 10` and then `total = total + 5`?

  3. 3. Which is a valid Python variable name?

  4. 4. `print("city")` shows `city`, but you wanted the value of the variable. What should you write?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.