🏷️ Python Variables and Types: Your Magic Boxes
The Story of Magic Boxes
Imagine you have a room full of magic boxes. Each box has a name tag on it, and you can put different things inside. Some boxes hold numbers, some hold words, some hold true/false answers, and some are empty on purpose.
That’s exactly what variables are in Python!
📦 Variables and Assignment
What is a Variable?
A variable is like a labeled box where you store stuff.
age = 10
name = "Emma"
Here:
ageis the box name (label)=means “put inside”10is what goes in the box
The Magic of =
The = sign doesn’t mean “equals” in Python. It means “put this value into this box”.
score = 100
Think: “Put 100 into the box called score.”
Changing What’s Inside
You can change what’s in a box anytime:
score = 100
score = 200
Now score holds 200. The old 100 is gone!
🔄 Multiple Assignment
One Line, Many Boxes
Python lets you fill many boxes at once:
x, y, z = 1, 2, 3
This creates three boxes:
xgets1ygets2zgets3
Same Value, Many Boxes
Want to put the same thing in multiple boxes?
a = b = c = 0
All three boxes (a, b, c) now hold 0.
Swapping Made Easy
Want to swap two boxes? Python makes it magical:
x, y = 10, 20
x, y = y, x
Now x is 20 and y is 10. No extra box needed!
🔢 Numeric Types
Python has different kinds of number boxes.
Integers (int) – Whole Numbers
Numbers without dots:
count = 42
temperature = -5
big_number = 1000000
Floats (float) – Decimal Numbers
Numbers with dots:
price = 19.99
pi = 3.14159
tiny = 0.001
Quick Tip
whole = 5 # int
decimal = 5.0 # float
They look similar but Python treats them differently!
âś… Boolean Type
True or False – That’s It!
Boolean is the simplest type. Only two possible values:
is_sunny = True
is_raining = False
Where Do We Use Them?
Booleans help make decisions:
has_ticket = True
age = 12
can_enter = age >= 10
can_enter becomes True because 12 is greater than 10.
Boolean Values
| Value | Meaning |
|---|---|
True |
Yes, correct, on |
False |
No, wrong, off |
Capital T and F are important!
🕳️ None Type
The Empty Box
Sometimes you need a box but have nothing to put in it yet:
result = None
None means “nothing here” or “empty on purpose.”
Why Use None?
winner = None
# Later in your game...
winner = "Emma"
It’s like a placeholder. “I’ll fill this box later.”
None is NOT Zero
score = 0 # Has a value: zero
result = None # Has NO value
Zero is something. None is nothing.
🔍 Type Checking
What’s in My Box?
Use type() to peek at what kind of thing is inside:
age = 10
print(type(age))
Output: <class 'int'>
Examples of Type Checking
type(42) # <class 'int'>
type(3.14) # <class 'float'>
type("hello") # <class 'str'>
type(True) # <class 'bool'>
type(None) # <class 'NoneType'>
The isinstance() Function
A fancier way to check types:
age = 10
isinstance(age, int) # True
isinstance(age, float) # False
🔄 Type Conversion
Changing Box Types
Sometimes you need to transform what’s inside.
String to Integer
text = "25"
number = int(text)
Now number is 25 (a real number, not text).
Integer to Float
whole = 5
decimal = float(whole)
decimal becomes 5.0
Number to String
age = 10
text = str(age)
text becomes "10" (now it’s text).
Float to Integer
price = 9.99
whole = int(price)
whole becomes 9 (the decimal part is chopped off!).
Conversion Table
| From | To | Function | Example |
|---|---|---|---|
| str | int | int() |
int("5") → 5 |
| str | float | float() |
float("3.14") → 3.14 |
| int | str | str() |
str(10) → "10" |
| int | float | float() |
float(5) → 5.0 |
| float | int | int() |
int(9.8) → 9 |
| any | bool | bool() |
bool(1) → True |
⚠️ Watch Out!
Not all conversions work:
int("hello") # ERROR!
You can’t turn “hello” into a number!
🎯 Quick Summary
graph LR A[Variables] --> B[Assignment =] A --> C[Multiple Assignment] D[Types] --> E[int - Whole Numbers] D --> F[float - Decimals] D --> G[bool - True/False] D --> H[None - Empty] I[Operations] --> J[type - Check Type] I --> K[Convert - Change Type]
🌟 Key Takeaways
- Variables are named boxes that store values
=means “put this value in this box”- Multiple assignment lets you set many variables in one line
- int = whole numbers, float = decimals
- bool = only
TrueorFalse - None = empty on purpose
type()tells you what kind of value you have- Convert using
int(),float(),str(),bool()
🎮 Try It Yourself!
# Create your own magic boxes!
my_name = "Your Name"
my_age = 10
my_height = 4.5
loves_python = True
secret = None
# Check their types!
print(type(my_name))
print(type(my_age))
print(type(my_height))
print(type(loves_python))
print(type(secret))
You’re now a Python variable wizard! 🧙‍♂️