Variables, Names, and Casting
What You'll Learn
How to create variables, choose good names, and convert values between different types (casting).
What Is a Variable?
A variable is a named container for a value. Think of it like a labelled box.
score = 100 # the box is named "score" and holds 100
name = "Alice" # the box is named "name" and holds "Alice"
You can change what's in the box at any time:
score = 100
print(score) # 100
score = 200
print(score) # 200
Creating Variables
Python creates variables automatically when you assign a value. No var, let, or int keyword needed:
age = 25
height = 1.75
username = "alice"
is_active = True
Multiple Assignment
Assign the same value to multiple variables:
x = y = z = 0
print(x, y, z) # 0 0 0
Assign different values in one line:
first, second, third = 10, 20, 30
print(first, second, third) # 10 20 30
Naming Rules
Python variable names must follow these rules:
| Rule | Example |
|---|---|
| Letters, numbers, underscores only | user_name, score1 |
| Cannot start with a number | ❌ 1score → ✅ score1 |
| Case-sensitive | Name and name are different variables |
| No reserved words | ❌ if, for, class, return |
Good names (descriptive):
user_age = 25
total_price = 49.99
is_logged_in = True
Bad names (avoid):
a = 25 # what is 'a'?
x1 = 49.99 # meaningless
t = True # unclear
Python Variable Naming Convention
Python uses snake_case for variables and functions:
first_name = "Alice" ✅
firstName = "Alice" # camelCase — works but not Pythonic
Checking a Variable's Type
Use type() to see what type a value is:
name = "Alice"
age = 25
height = 1.75
is_active = True
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
print(type(height)) # <class 'float'>
print(type(is_active)) # <class 'bool'>
Casting (Type Conversion)
Casting means converting a value from one type to another.
Convert to integer
x = int("42") # string "42" → integer 42
y = int(3.9) # float 3.9 → integer 3 (truncated, not rounded)
z = int(True) # True → 1
print(x, y, z) # 42 3 1
Convert to float
a = float("3.14") # string → float
b = float(10) # integer → float
print(a, b) # 3.14 10.0
Convert to string
age = 25
text = str(age) # integer → string
print("Age: " + text) # Age: 25 (can now concatenate)
Convert to boolean
print(bool(1)) # True
print(bool(0)) # False
print(bool("")) # False
print(bool("hi")) # True
A Common Beginner Pattern
When you get input from the user, it always comes as a string. You need to cast it:
user_input = "42" # pretend this came from input()
number = int(user_input) # convert to int to do math
result = number * 2
print(result) # 84
Common Mistakes
| Mistake | Example | Fix |
|---|---|---|
| Adding string and int | "Age: " + 25 | Cast: "Age: " + str(25) |
| Casting invalid string | int("hello") | ValueError — only numbers in strings |
| Forgetting case sensitivity | Name vs name | Use consistent names |
| Starting name with number | 1value = 5 | SyntaxError — start with letter |
Quick Reference
# Create
name = "Alice"
age = 25
height = 1.75
# Multiple
a, b, c = 1, 2, 3
# Check type
type(age) # <class 'int'>
# Cast to int
int("42") # 42
int(3.9) # 3
# Cast to float
float("3.14") # 3.14
float(10) # 10.0
# Cast to string
str(100) # "100"
# Cast to bool
bool(0) # False
bool("hello") # True
Practice Exercises
- Store your info: Create variables for your name, age, and city. Print them all with an f-string.
- Type check: Create 4 variables of different types and print
type()for each. - Casting: Write a program that takes
"100"as a string and prints"100" + 50 = 150by casting correctly. - Swap: Assign
a = 5andb = 10, then swap their values and print both.