Lesson 6 of 18 · 4 min
Variables — Named Boxes for Storing Data
The WHY — Why Do We Need Variables?
Imagine you are calculating the bill for 5 students who each bought 3 textbooks at ₹450 each. You could write 5 * 3 * 450 directly. But what if the price changes to ₹500? You would need to hunt through your entire program and change every 450 to 500.
A variable lets you give data a name — then you only need to change it in one place.
The Analogy: A Shelf of Labeled Boxes
Picture a shelf with labeled boxes. Each box has a name ("STUDENT_NAME", "AGE", "SCORE") and you put a value inside. To get the value, you look for the box by its label. You can replace what is inside any time.
Creating Variables
student_name = "Arjun" # create a box called student_name, put "Arjun" inside age = 21 # create a box called age, put the number 21 inside gpa = 3.75 # create a box called gpa, put 3.75 inside is_enrolled = True # create a box called is_enrolled, put True inside department = "CSE" # create a box called department, put "CSE" inside
The = sign is called the assignment operator. It means "put this value into this variable." It does NOT mean "equals" like in math.
Reading and Using Variables
city = "Chennai" # store "Chennai" in the variable called city
print(city) # look inside the city variable and display it
print("I study in", city) # use city inside a larger print statement
Output: Chennai I study in Chennai
Updating Variables
score = 85 # create score variable with value 85 print(score) # display current value: 85 score = 92 # replace 85 with 92 (the old value is gone) print(score) # display new value: 92 score = score + 5 # take current value (92), add 5, store 97 back in score print(score) # display: 97
Output: 85 92 97
Variable Naming Rules — MUST Follow These
| Rule | Good Example | Bad Example | Error If Broken |
|---|---|---|---|
| Letters, numbers, underscores only | student_name, score2 | student-name | SyntaxError |
| Cannot START with a number | total2 | 2total | SyntaxError |
| No spaces | first_name | first name | SyntaxError |
| Cannot be a Python keyword | student_age | if = 5 | SyntaxError |
| Case-sensitive | name and Name are different | — | No error but confusing |
Python Keywords — Cannot Use as Variable Names
if else elif for while def class return import from True False None and or not in is break continue pass try except finally raise with as lambda global nonlocal
Naming Convention — snake_case
# RECOMMENDED — snake_case (all lowercase, words joined by underscores) student_name = "Priya" # readable variable name total_price = 999.99 # clear what this stores number_of_students = 30 # descriptive is_active = True # booleans often start with "is_" or "has_" # WORKS BUT NOT PREFERRED for variables in Python StudentName = "Priya" # PascalCase (used for class names instead) totalPrice = 999.99 # camelCase (used in JavaScript, not Python) TOTAL_PRICE = 999.99 # ALL_CAPS (used for constants that never change)
Multiple Assignment Shortcuts
a = b = c = 0 # assign same value (0) to all three at once print(a, b, c) # display all three: 0 0 0 x, y, z = 10, 20, 30 # assign different values in one line print(x) # display x: 10 print(y) # display y: 20 print(z) # display z: 30 first = "Apple" second = "Banana" first, second = second, first # swap two variables elegantly in one line print(first) # display: Banana print(second) # display: Apple
Output: 0 0 0 10 20 30 Banana Apple
