Variables and Naming
Learn how to store and name data in Python
What You'll Learn
- What variables are
- How to create variables
- Rules for naming variables
- Good naming habits
What is a Variable?
A variable is like a box that holds information. You give the box a name so you can use it later.
Simple example:
age = 25Here, age is the box name, and 25 is what's inside.
Creating Variables
Creating a variable is super easy:
name = "John"
age = 30
price = 19.99Format: variable_name = value
Using Variables
Once you create a variable, you can use it:
price = 100
quantity = 3
total = price * quantity
print(total)
# Shows: 300Naming Rules
What You CAN Do
✅ Use letters:
name = "Sarah"
city = "Paris"✅ Use numbers (but not at start):
student1 = "Mike"
room2 = "Lab"✅ Use underscores:
user_name = "Alex"
total_price = 500What You CANNOT Do
❌ No spaces:
user name = "Alex" # Wrong!
user_name = "Alex" # Correct!❌ No special characters:
user-name = "Alex" # Wrong!
user@email = "test" # Wrong!❌ Can't start with number:
1student = "Mike" # Wrong!
student1 = "Mike" # Correct!❌ Can't use Python words:
print = 100 # Wrong! (print is a Python command)
for = 50 # Wrong! (for is a Python command)Good Naming Habits
Use Clear Names
Bad:
a = 100
b = 5
c = a * bGood:
price = 100
quantity = 5
total = price * quantityClear names help you understand code later!
Use Lowercase
# Good style
user_age = 25
total_cost = 100
# Works but not standard
UserAge = 25
TOTAL_COST = 100Use Underscores for Long Names
# Easy to read
user_first_name = "John"
total_price_with_tax = 120
# Hard to read
userfirstname = "John"
totalprice = 120Changing Variable Values
You can change what's in a variable:
score = 10
print(score) # Shows: 10
score = 20
print(score) # Shows: 20
score = score + 5
print(score) # Shows: 25Multiple Variables at Once
# One way
name = "Sarah"
age = 28
city = "London"
# Shorter way
name, age, city = "Sarah", 28, "London"Practice Examples
Example 1: Store personal info
first_name = "Emma"
last_name = "Smith"
age = 25
country = "USA"
print("Name:", first_name, last_name)
print("Age:", age)
print("Country:", country)Example 2: Simple calculator
number1 = 10
number2 = 5
sum_result = number1 + number2
difference = number1 - number2
print("Sum:", sum_result)
print("Difference:", difference)Example 3: Shopping cart
item_name = "Laptop"
item_price = 800
tax = 50
shipping = 20
total_cost = item_price + tax + shipping
print("Item:", item_name)
print("Total:", total_cost)
# Shows: Total: 870Quick Tips
- Choose names that explain what the variable holds
- Use
total_pricenottp - Use
user_agenotua - Longer names are better than confusing short names
Common Mistakes
Mistake 1: Forgetting quotes for text
name = John # Wrong!
name = "John" # Correct!Mistake 2: Using spaces
user age = 25 # Wrong!
user_age = 25 # Correct!Mistake 3: Starting with number
1user = "Mike" # Wrong!
user1 = "Mike" # Correct!Summary
- Variables store information
- Use clear, meaningful names
- Follow naming rules (no spaces, no special characters)
- Use lowercase with underscores
- Change variable values anytime
What's Next?
Now let's learn about different types of data you can store in variables!