Naming Your Variables the Right Way
Setting the stage
Imagine a school system that prints student timetables. Each subject, teacher, and room number has to be stored somewhere in the program. To make sense of all this, we use variables. But what if we named every variable x, y, or data? The code would quickly become a mess!
That’s why naming variables correctly is so important. In this lesson, we’ll build a small version of a school timetable system and learn the rules for naming variables in Python.
Here’s a sample timetable:

You’re the programmer hired to set up this timetable generator. To succeed, you must understand how to choose clear and valid names for variables.
The core principle
A variable name is simply the label you give to a piece of stored information. But Python has rules you must follow:
- Names can include letters, numbers, and underscores.
- Names cannot start with a number.
- Names cannot use spaces or special characters like !, @, or #.
- Names are case-sensitive (math_class and Math_Class are different).
- Names cannot be Python keywords like for, if, or class.
Example of valid names:
1 2 3student_name = "Ali" math_teacher = "Mr. Ahmed" room101 = "Mathematics"
Example of invalid names:
- 1teacher (starts with number)
- class (reserved word)
- room number (space not allowed)
If you look at our timetable, it contains multiple subjects. If we named every variable poorly, like a, b, and c, nobody could understand which subject is which. Clear names make your code easy to read, easy to update, and less error-prone.
Key points to remember:
- Use descriptive names: english_teacher, not just et.
- Use underscores for readability: science_room, not scienceroom.
- Keep names consistent (all lowercase with underscores is common).
Try it in code
Let’s create a few variables for a timetable:
1 2 3 4 5 6 7 8 9student_name = "Sara" math_class = "Mathematics" math_teacher = "Mr. Ali" math_room = "101" print("Student:", student_name) print("Subject:", math_class) print("Teacher:", math_teacher) print("Room:", math_room)
When run, this will display:
1 2 3 4Student: Sara Subject: Mathematics Teacher: Mr. Ali Room: 101
Notice how the variable names clearly describe what they hold.
Work smarter with AI
If you’re using an AI assistant, you can give a prompt like this:
Prompt:
Write a Python program that stores a timetable for three subjects using well-named variables (student name, subject, teacher, room). Print the details in a neat timetable format.
The AI will return clear, ready-to-use code with good variable names.
Practice challenge
Awesome job so far! Let’s test your understanding with a small exercise.
Task
Create variables for a student named Omar with the following timetable:
- English with Ms. Fatima in Room 202
- Science with Mr. Bilal in Room 303
- History with Ms. Ayesha in Room 404
Print the timetable exactly in this format:
1 2 3 4 5 6=== TIMETABLE === Student: Omar English - Teacher: Ms. Fatima - Room: 202 Science - Teacher: Mr. Bilal - Room: 303 History - Teacher: Ms. Ayesha - Room: 404
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17student_name = "Omar" english_teacher = "Ms. Fatima" english_room = "202" science_teacher = "Mr. Bilal" science_room = "303" history_teacher = "Ms. Ayesha" history_room = "404" print("=== TIMETABLE ===") print("Student:", student_name) print() print("English - Teacher:", english_teacher, "- Room:", english_room) print("Science - Teacher:", science_teacher, "- Room:", science_room) print("History - Teacher:", history_teacher, "- Room:", history_room)
Perfect! You’ve now mastered the basics of variable naming in Python.
Frequently Asked Questions
Variable names in Python are identifiers used to reference data values. They must start with a letter or an underscore and can include letters, numbers, and underscores.
In Python, the four types of variables are local, global, instance, and class variables, each having a different scope and usage.
Variable names are identifiers in Python used to store and reference values. They follow specific rules like starting with a letter or an underscore.
The __name__ variable in Python indicates the name of the current module. If the module is being run directly, __name__ is set to '__main__'.
Still have questions?Contact our support team