Python Basics · Lesson 2
Variables & Naming Rules
Learn how Python stores information using variables, how values can be changed, and how to create valid and readable variable names.
What you will learn
- • What a variable is
- • How assignment works
- • How to change a variable's value
- • How Python evaluates variables
- • Rules for variable names
- • Valid and invalid variable names
- • Good naming practices
- • How to debug variable mistakes
Part 1
What is a variable?
A variable is a name that refers to a value in a Python program. Variables allow us to store information and use that information later.
name = "Yogesh"
age = 20Here, name refers to the value "Yogesh", while age refers to the value 20.
CHECK YOUR UNDERSTANDING
What is a variable in Python?
Part 2
Assigning a value
The equals sign = is used for assignment in Python. It connects a variable name with a value.
score = 100
city = "Dehradun"CHECK YOUR UNDERSTANDING
What does score = 100 do?
Predict the output
Use a variable with print()
name = "Yogesh"
print(name)CHECK YOUR UNDERSTANDING
What will the code above display?
Using multiple variables
A program can contain many variables. Each variable can refer to its own value.
name = "Aman"
age = 21
city = "Dehradun"
print(name)
print(age)
print(city)CHECK YOUR UNDERSTANDING
Which value will print(age) display?
Experiment
Changing a variable
A variable can be assigned a new value. The latest assignment determines the value currently referred to by that variable.
score = 50
score = 80
print(score)CHECK YOUR UNDERSTANDING
What will print(score) display?
Variables can be used in calculations
price = 100
quantity = 3
total = price * quantity
print(total)CHECK YOUR UNDERSTANDING
What will print(total) display?
Part 3
Python variable naming rules
Variable names must follow Python's syntax rules. A good variable name should also make the code easier to understand.
✓ A variable name can contain letters, numbers and underscores.
✓ A variable name cannot start with a number.
✓ Variable names are case-sensitive.
✓ Spaces are not allowed in variable names.
✓ Python keywords cannot be used as variable names.
CHECK YOUR UNDERSTANDING
Which variable name is valid Python?
Valid or invalid?
Valid
student_name
age2
total_sales
city
Invalid
2age
student name
total-sales
CHECK YOUR UNDERSTANDING
Why is 2age an invalid variable name?
Python is case-sensitive
Python treats uppercase and lowercase letters as different characters in variable names.
name = "Aman"
Name = "Ravi"
print(name)
print(Name)CHECK YOUR UNDERSTANDING
Are name and Name the same variable in Python?
Your turn
Complete the code
Fill in the variable name: _____ = 25
Fill in the blank: city = _____
Complete the statement to store 500 in sales: _____ = 500
Debug the variable
2name = "Aman"
print(2name)CHECK YOUR UNDERSTANDING
What is the problem with this code?
Good variable names matter
Python allows many valid names, but readable names make programs easier to understand.
x = 15000
Valid, but unclear.
monthly_sales = 15000
More descriptive.
CHECK YOUR UNDERSTANDING
Which name is clearer for storing monthly sales?
Challenge: follow the variable
city = "Delhi"
city = "Dehradun"
print(city)CHECK YOUR UNDERSTANDING
What is the final output?
Mini Challenge
Create a simple student profile
Create three variables called name, age and city. Give each variable an appropriate value and then print all three.
name = "Your Name"
age = 20
city = "Your City"
print(name)
print(age)
print(city)Final Challenge
Build a simple sales calculation
Create variables for product price and quantity. Calculate the total sales amount and print the result.
price = 500
quantity = 4
total = price * quantity
print(total)Try changing price and quantity to experiment with the result.
Lesson recap
- ✓ Variables give names to values.
- ✓ The = operator performs assignment.
- ✓ Variables can be reassigned.
- ✓ Variables can be used in calculations.
- ✓ Variable names cannot start with numbers.
- ✓ Spaces are not allowed in variable names.
- ✓ Python variable names are case-sensitive.
- ✓ Descriptive names make code easier to understand.