Hey again, coder! π In Module 1 you wrote your first program. Now itβs time to make Python remember things β like your score in a game, your name, or whether youβre hungry (always true π). Weβll use variables and learn the four basic data types. Letβs dive in!
Think of a variable as a labelled box π¦. You put something inside and give the box a name.
Later, you can look inside the box by using its name. In Python, you create a variable with
the = sign (called assignment).
my_score),
and canβt start with a number.
Every value in Python has a type. Here are the four youβll use most:
| Type | Name | Example | What it's for |
|---|---|---|---|
int | Integer | 42 | Whole numbers |
float | Float | 3.14 | Decimal numbers |
str | String | "hello" | Text (in quotes) |
bool | Boolean | True / False | Yes/No values |
You can check any valueβs type using type():
Python can do math for you! Integers are whole numbers, floats have decimals. Try this in the REPL or a new file:
A string is text inside quotes (single ' or double ").
You can glue strings together with +, repeat them with *,
and even insert variables using f-strings.
f" and let you put variables inside {}.
A boolean can only be True or False (capital first letter!).
Youβll use them for decisions like βis the game over?β or βdo I have pizza?β
Sometimes you need to change a type. Use int(), float(), str(),
or bool(). This is called type casting.
int("hello") will crash β you can only convert numbers written as text!
Create a file called my_pet.py. Make variables for your dream pet: its name (string), its age (integer), how tall it is in metres (float), and whether it can fly (boolean). Then print a fun sentence using an f-string.
π¨ Get creative β make it a dragon, a robot, or a flying pizza!
Bonus: use type() to print the type of each variable.