Python Lesson 4: Variables
A variable is a labeled box that stores data. You give it a name, put something in it, and use that name later to access the value.
Creating Variables
name = "Alice"
age = 25
height = 5.6
is_student = True
print(name) # Alice
print(age) # 25
print(height) # 5.6
Naming Rules
- Use letters, numbers, underscores: user_name, age2
- Start with a letter or underscore: _data, score
- Cannot start with a number: 2name is an ERROR
- No spaces: use first_name not “first name”
Python Style: snake_case
# Good Python style (snake_case)
first_name = "Bob"
total_score = 100
user_email = "bob@example.com"
# Works but not Pythonic (camelCase)
firstName = "Bob" # avoid this in Python
Changing Variable Values
score = 0
print(score) # 0
score = 10
print(score) # 10
score = score + 5
print(score) # 15
Multiple Assignment
# Same value to multiple variables
x = y = z = 0
# Different values in one line
name, age, city = "Alice", 30, "London"
print(name, age, city) # Alice 30 London
🏋️ Practice Task
Create variables for: name (your name), age (your age), city (your city), hobby (something you like). Print them in one sentence: “My name is [name] and I am [age] years old from [city].”
💡 Hint: Use print(“My name is”, name, “and I am”, age, “years old from”, city)