Python for AI/Python basics/Lesson 02
Variables
Give data a name and remember it, use it later, and change it — the most fundamental concept in programming.
- Time
- 15 min
- Exercises
- 1
- Challenges
- 1
- Quiz
- 3 questions
Python for AI/Python basics/Lesson 02
Give data a name and remember it, use it later, and change it — the most fundamental concept in programming.
A variable is like a named box that stores data.
name age cgpa
+---------+ +------+ +--------+
| "Karim" | | 21 | | 3.75 |
+---------+ +------+ +--------+
Each box has a name (name, age), and a value inside it. Whenever you need the value later, you can retrieve it by using its name.
Imagine you're training an ML model. You're using the model's learning_rate in 10 different places. If you write 0.001 directly each time, you'll have to change it in 10 places when you want to update the value — and forgetting even one can cause a bug. If you store it in a variable, you only need to change it in one place.
In Python, you can create a variable by simply writing name = value:
Here, = doesn't mean "equal" — it means "store the value on the right under the name on the left". This is called assignment.
A variable's value can be changed at any time — that's why it's called a "variable" (something that can vary).
Pay attention to the last line: score = score + 10. Python first evaluates the right side (75 + 10 = 85), then stores the result in score.
| Rule | Valid | Invalid |
|---|---|---|
Start with a letter or _ | score, _temp | 2score |
Use only letters, numbers, and _ | user_name | user-name |
| Must not be a keyword | my_class | class, for |
The convention in Python is snake_case: lowercase letters with _ between words. For example, learning_rate, num_epochs, train_data. You'll see this style everywhere in PyTorch and scikit-learn code.
Choosing good names is a skill. Instead of
xora1, writetotal_priceornum_students— your code will explain itself.
If you use a variable that hasn't been defined, Python raises a NameError:
The error message says name 'totl' is not defined — it's a spelling mistake! Typos like this are very common. Fix it and run the code again.
Exercise
Create three variables:
name — value "Karim"age — value 21cgpa — value 3.75Then print all three using print(name, age, cgpa).
Challenge
You are given a = 10 and b = 20. Write code so that, at the end, a has the value 20 and b has the value 10.
Condition: You cannot directly write a = 20 or b = 10 — you must swap the values using variables.
In ML projects, hyperparameters are usually stored in variables at the beginning:
This makes experimentation easier because you only need to change the values at the top.
a = b = []? (Both names refer to the same list object — changes made through one name will also be visible through the other. We'll explore this in detail in the List lesson.)We've seen that variables can store text, integers, and decimal numbers. These are actually different Data Types. That's what we'll learn in the next lesson.