Loading...
Loading...
Store and work with different kinds of data
A variable is a named container that holds a value in your computer's memory. Think of it like a labeled box: the label is the variable name, and whatever you put inside is the value.
When you write name = "Alice", Python carves out a spot in memory, labels it name, and stores "Alice" inside. Later, whenever you use name in your code, Python retrieves whatever's in that box.
name = "Alice"
age = 25
print(name, age)| Part | What it's called | What it does |
|---|---|---|
| `name` | **Variable name** | You choose this — make it descriptive! |
| `=` | **Assignment operator** | Takes the value on the right and stores it in the name on the left |
| `"Alice"` | **String value** | Text data (must be in quotes) |
| `25` | **Integer value** | Whole number (no quotes needed) |
When Python runs the code above:
The output is:
Alice 25Without variables, every value in your code would be hard-coded — fixed and unchangeable. That works for a one-line script, but fails for any real program.
Real-world examples:
Variables let your code adapt — ask the user for their name, store it in a variable, and use it throughout the program.
Python automatically knows what *kind* of data a variable holds:
| Type | Name | Examples |
|---|---|---|
| `int` | Whole numbers | `42`, `-5`, `0`, `1000` |
| `float` | Decimal numbers | `3.14`, `-0.5`, `2.0` |
| `str` | Text (strings) | `"hello"`, `"Python"`, `"42"` |
| `bool` | True/False | `True`, `False` |
| ❌ Wrong | Why | ✅ Right |
|---|---|---|
| `name = Alice` (no quotes) | Python looks for a variable called `Alice` — NameError! | `name = "Alice"` |
| `my name = "Alice"` (space) | Python sees 3 separate tokens — syntax error | `my_name = "Alice"` |
| `age = "25"` (quotes on number) | It's a string now — you can't do math with it | `age = 25` |
| `Name = "Alice"` then `print(name)` | Python is case-sensitive — `Name` and `name` are different! | Be consistent with lowercase |
Create a variable called favoriteColor and set it to your favorite color (as a string). Then create a variable called year and set it to the current year (as a number). Print both variables.
favoriteColor = "Blue" year = 2026 print(favoriteColor, year)