2. Variables and Types¶
This is the same variable idea from Unit 1 (SET score TO 0) and Unit 2 (the set block) — now written as a plain assignment:
health = 3
name = "Alex"
has_key = False
Every value has a type. The ones you'll use most:
| Type | Example | Meaning |
|---|---|---|
str |
"Alex" |
text, in quotes |
int |
3 |
a whole number |
float |
3.5 |
a decimal number |
bool |
True / False |
yes or no |
You can check a value's type with type(...), and build text out of variables with an f-string:
room = "The Dark Forest"
item_count = 2
print(f"You are in {room} carrying {item_count} items.")
You are in The Dark Forest carrying 2 items.
Try it
Make three variables — a room name (str), an item count (int), and whether the player has a key (bool) — then print them in one sentence using an f-string.
Tip
Python figures out the type from the value you give it — you never have to declare it up front the way some languages require.