2. Data Types and Operators
2.1 Understanding variables
Highlights & notes
Select lesson text to highlight it, or tap Add note.
Bookmarks
Bookmark a lesson to see it here.
A variable is a lunch box with a name sticker. You put something in, and use the name to get it back.
- The name sticker goes on the left.
- The thing inside goes on the right of =.
- Put something new in and the old thing is gone.
Thumb rule: name = value. The = means "put into", not "equals".
Watch it run · 1
box = "apple"
print(box)apple
Watch it run · 2
box = "mango"
print(box)mango
A variable is a labelled box that stores a value. rows_loaded = 10 puts 10 in a box labelled rows_loaded. Later you use the label to get the value back.
Python works out the right side of = first, then stores the result under the name on the left. That’s why rows_loaded = rows_loaded + 3 works: it takes the old value 10, adds 3, and stores 13.
- Use clear names:
file_name,rows_loaded,order_total - Use lowercase words joined by
_(called snake_case) - Names are case-sensitive:
Rowsandrowsare different - A name can’t start with a number or contain spaces or hyphens
Read count = count + 1 right to left
- Take the current value of
count - Add 1
- Store the result back in
count
How it fits together
- rows_loaded = 10
- rows_loaded + 3
- rows_loaded → 13
Use = to assign a value to a name. It means "store this", not "is equal to".
Example 1
Which one is a valid variable name?
Example 2
After these lines run, what is count? count = 10 count = count + 5
Step through the code
Python
rows_loaded = 10rows_loaded = rows_loaded + 3print(rows_loaded)
Output
13
Practice in Python ✓ Solved
Start rows_loaded at 20, add 5, and print the updated value.
Expected output
25
Reference solution
rows_loaded = 20
rows_loaded += 5
print(rows_loaded)
Question 1 · Easy
rows_loaded = 10, then rows_loaded = 12. What does print(rows_loaded) show?
Question 2 · Medium
a = 4, then a = a * 2, then a = a + 1. What is a?
Question 3 · Hard
x = 5, y = x, then x = 10. What does print(y) show?
Key takeaways
Variables name values, and reassignment can update a running count.
Save your progress
Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.
Start free Log in