Variables Introduction and Rules0%
Variables Introduction and Rules
Beginner8 min read•Updated: 2026-09-03
Variables Introduction and Rules
A variable is a labeled name that references an object stored in computer memory.
Key Concepts & Detailed Explanation
In Python, variables do not store values directly; they store references (memory addresses) to objects. Rules for identifiers:
- 1Must begin with a letter (a-z, A-Z) or an underscore (_).
- 2Cannot begin with a number.
- 3Can only contain alphanumeric characters and underscores.
- 4Cannot be a Python keyword (e.g. if, for, class, def, return).
Code Examples & Output
Python
# Valid identifiers
student_name = "Amit"
_system_id = 9921
batch_2026 = "Morning"
print(student_name, _system_id, batch_2026)
# Check all reserved keywords
import keyword
print("Reserved keywords count:", len(keyword.kwlist))
Expected Output:
Output
Amit 9921 Morning
Reserved keywords count: 35
Best Practices & Common Pitfalls
Never name your variables 'l' (lowercase L), 'O' (uppercase O), or 'I' (uppercase i) as they resemble numbers.
Practice Quiz
1. Which of these is NOT a valid Python identifier?
- A) total_sum
- B) _cache
- C) 4th_quarter
- D) valueTwo
Answer: C
Explanation: Identifiers cannot start with a digit.
2. How many reserved keywords exist approximately in modern Python 3?
- A) 10
- B) 35
- C) 100
- D) 256
Answer: B
Explanation: Python 3 has approximately 35 reserved keywords.
Practice Challenge
List 5 valid and 3 invalid variable names, explaining why each invalid one fails Python rules.
Next Lesson
Creating a Variable
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Comments & Best Practices | Creating a Variable |
Practice Quiz
Test your understanding of this lesson with 1 questions. Each question has one correct answer.