Imagine writing a shopping list on a piece of paper which you can use again and again. You don’t need to rewrite the list every time—you just keep it safe and use it when needed. Variables in Python work the same way! A variable is a container for storing data. Instead of writing values directly in code, we can assign them to variables and use them repeatedly.
💡 Analogy: A variable is like a labeled storage box—you put something inside, label it, and open it later to retrieve the item.
Understand what a variable is and why it's used.
Declare and assign values to variables in Python.
Use variables in expressions and print their values.
Variables – Containers for storing data values, which can be updated or reused later in the program.
Assignment (=) – The operator used to assign a value to a variable (name = "Alice").
Data storage – The process of keeping information in variables so it can be accessed later in the program.
Naming conventions – Rules for naming variables, such as using descriptive names (user_age instead of x).
Dynamic typing – A feature of Python where variables do not have a fixed type and can store different types of data at different times.
Example
name = "Alice"
age = 14
print("My name is", name, "and I am", age, "years old.")
Using spaces in variable names: my age = 15 ❌, use my_age = 15 ✅.
Using special characters: name@ = "Alice" ❌ is not allowed.
Forgetting case sensitivity: Name and name are different variables in Python.
🔗 Practice on W3Schools: Variables Tutorial – Learn how to create and use variables in Python.
🔗 Try It Online: Run Your Code Here – Test your Python code instantly.
1️⃣ Basic Challenge:
Write a program that creates a variable called name and stores your name in it. Then, print a sentence using this variable.
2️⃣ Extended Challenge:
Modify your program to include a second variable for age and print both values in a single sentence.
3️⃣ Advanced Challenge:
Write a program that stores your name, age, and favorite color in separate variables and prints a well-formatted introduction using all three.
On paper, write the example from this lesson. Be sure to include:
✅ A variable storing a string
✅ A variable storing a number
✅ A print() statement using these variables
Read the example code before starting!