Variables and Data Types
Store values in variables, pick good names, and tell integers, floats, strings and booleans apart so your programs handle data correctly.
Welcome back!
Step 1 of 13Learn
Giving values a name
Imagine a game that has to remember your score, your username and how many lives you have left. Programs remember things with variables. A variable is a name that refers to a value:
score = 0
username = "pixelfox"
lives = 3The = sign here does not mean "equals" like in math. It means assign: "make the name on the left refer to the value on the right." Read score = 0 as "score gets 0."
Once a variable exists, you can use its name anywhere you would use the value:
print(username, "has", lives, "lives")This prints pixelfox has 3 lives. Notice there are no quotes around username; with quotes you would print the word "username" instead of its value.
Variables can change. That's why they are called variables:
score = 0
score = score + 10
score = score + 5
print(score)Python evaluates the right side first, using the current value, then stores the result. So score goes 0, then 10, then 15. The shortcut score += 10 means exactly the same as score = score + 10.
Step 2 of 13Practice · Diagram
Watch one variable change
Each assignment replaces the old value. Tap a box to see what score holds after that line.
After which line does score first hold 15?
Tap the shape that answers the question, then press Check.
- score = 0
score holds 0.
- score = score + 10
Right side first: 0 + 10 = 10. Now score holds 10.
- score += 5
Shortcut for score = score + 5. Now 15.
- score = score * 2
15 × 2 = 30.
- print(score)
Shows 30. Only the latest value survives.
Step 3 of 13Practice · Work it out
What number does this program print?
``
coins = 20
coins = coins - 5
coins += 12
coins = coins * 2
print(coins)
``
Step 4 of 13Learn
Naming rules and good names
Python has a few hard rules for variable names:
- Only letters, digits and underscores (
_). No spaces or dashes. - A name can't start with a digit:
player2is fine,2playeris not. - Names are case sensitive:
Scoreandscoreare two different variables. - You can't use Python's reserved words, such as
if,for,classorTrue.
Breaking a rule gives a SyntaxError. Beyond the rules, there are good habits. Python programmers write names in snake_case: lowercase words joined by underscores, like high_score or time_left.
Good names explain themselves. Compare these two lines, which do the same thing:
x = y * 0.15
tip = bill * 0.15The second needs no comment. Single letters are fine for tiny throwaway values, like a loop counter, but anything you'll use more than once deserves a real name. Avoid names of built-in functions too: if you write print = 5, the print() function stops working for the rest of the program.
Step 5 of 13Compare
Allowed vs. helpful names
| Aspect | Works, but unclear | Clear name |
|---|---|---|
| Lives left | x | lives_left |
| Player name | n | player_name |
| Top score | ts2 | high_score |
| Is the game over? | flag | game_over |
Step 6 of 13Practice · Sort into groups
Sort these into valid or invalid Python variable names.
Tap an item, then tap its group (or drag it there).
Step 7 of 13Learn
The four basic data types
Every value in Python has a type, and the type decides what you can do with it.
| Type | Name | Examples | Used for |
|---|---|---|---|
int | integer | 7, 0, -40 | counting: lives, levels, followers |
float | floating-point number | 3.5, 98.6, 2.0 | measuring: prices, times, temperatures |
str | string | "Maya", "42", "" | text of any kind |
bool | boolean | True, False | yes/no facts: is the game over? |
A few details catch beginners out. 2.0 is a float, not an int, because it has a decimal point. "42" is a string, not a number, because it is in quotes: it is the characters 4 and 2. The empty string "" is still a string; it just contains no characters. And True and False need capital letters and no quotes.
You can ask Python for a value's type with type():
print(type(42)) # <class 'int'>
print(type("42")) # <class 'str'>
print(type(4.2)) # <class 'float'>Why care? Because "42" + "1" gives "421" while 42 + 1 gives 43. Same-looking data, completely different behavior.
Step 8 of 13Flashcards
The four basic types
Tap a card to flip it.
Step 9 of 13Practice · Match the pairs
Match each value to its data type.
Tap an answer, then tap the place where it belongs (or drag it there).
- 17
- 17.0
- "17"
- False
Step 10 of 13Practice · Choose one
What does print("5" + "5") show?
Step 11 of 13Note
Step 12 of 13Practice · Select all that apply
Which of these values are strings? Select all that apply.
Select all that apply.
Step 13 of 13Practice · What would you do?
What would you do?
Jordan writes a program to track a streak:
streak = 4
Streak = streak + 1
print(streak)It prints 4, but Jordan expected 5. What went wrong?
Lesson complete!