- Rules for Naming Variables
- Example 1: Valid Variable Names
- Example 2: Variable Names are Case-Sensitive
- Example 3: Variable Names Can Contain Numbers
- Example 4: Variable Names Can Contain Underscores
- Example 5: Meaningful Variable Names
- Example 6: Avoiding Single-Letter Variable Names
- Invalid Variable Names
- Common Mistakes
- Best Practices
- Key Points to Remember
Variable names are identifiers used to refer to values stored in memory. Choosing valid and meaningful variable names is important because it makes programs easier to read, understand, and maintain.
Python follows a set of rules for naming variables. If these rules are violated, the program will produce a syntax error.
Rules for Naming Variables #
A variable name:
- Can contain letters (
A-Z,a-z) - Can contain digits (
0-9) - Can contain the underscore (
_) - Must begin with a letter or an underscore (
_) - Cannot begin with a digit
- Cannot contain spaces
- Cannot contain special characters such as
@,#,$,%,&, etc. - Cannot be a Python keyword such as
if,for, orwhile - Is case-sensitive
Example 1: Valid Variable Names #
main.py
name = "Alice"
age = 25
salary_2026 = 45000
_marks = 95
print(name)
print(age)
print(salary_2026)
print(_marks)
Output #
Alice
25
45000
95
Explanation #
The following are valid variable names:
namebegins with a letter.agebegins with a letter.salary_2026contains letters, digits, and an underscore._marksbegins with an underscore, which is allowed.
All these names follow Python’s naming rules.
Example 2: Variable Names are Case-Sensitive #
main.py
name = "Alice"
Name = "Bob"
NAME = "Charlie"
print(name)
print(Name)
print(NAME)
Output #
Alice
Bob
Charlie
Explanation #
nameNameNAME
are three different variables.
Python treats uppercase and lowercase letters as different characters.
Example 3: Variable Names Can Contain Numbers #
main.py
student1 = "Alice"
student2 = "Bob"
print(student1)
print(student2)
Output #
Alice
Bob
Explanation #
- Digits can appear in a variable name.
- However, they cannot appear as the first character.
- Appending numbers is useful when multiple similar variables are required.
Example 4: Variable Names Can Contain Underscores #
main.py
first_name = "John"
last_name = "Smith"
print(first_name)
print(last_name)
Output #
John
Smith
Explanation #
- The underscore (
_) is commonly used to separate words. - This naming style is called snake_case.
- According to the Python style guide (PEP 8), variables should generally be written using
snake_case.
Example 5: Meaningful Variable Names #
main.py
radius = 5
area = 3.14 * radius * radius
print(area)
Output #
78.5
Explanation #
radiusclearly indicates what the value represents.areaclearly describes the calculated result.- Meaningful names make programs easier to understand.
Example 6: Avoiding Single-Letter Variable Names #
main.py
employee_salary = 45000
print(employee_salary)
Output #
45000
Explanation #
Using descriptive names such as employee_salary is much better than using names like:
x
a
temp
Good names make code easier to read and maintain.
Invalid Variable Names #
The following examples violate Python’s naming rules.
Example 7: Variable Name Starting with a Digit #
Incorrect
1name = "Alice"
Output #
SyntaxError: invalid decimal literal
Explanation #
Variable names cannot begin with a number.
Correct
name1 = "Alice"
Example 8: Variable Name Containing Spaces #
Incorrect
first name = "John"
Output #
SyntaxError: invalid syntax
Explanation #
Spaces are not allowed inside variable names.
Correct
first_name = "John"
Example 9: Variable Name with Special Characters #
Incorrect
salary$ = 45000
Output #
SyntaxError: invalid syntax
Explanation #
Special characters such as:
$@#%&
cannot be used in variable names.
Correct
salary = 45000
Example 10: Using a Python Keyword #
Incorrect
for = 10
Output #
SyntaxError: invalid syntax
Explanation #
for is a reserved keyword in Python.
Keywords have predefined meanings and cannot be used as variable names.
Common Mistakes #
1. Starting a Variable Name with a Number #
Incorrect
2marks = 90
Correct
marks2 = 90
2. Using Spaces in Variable Names #
Incorrect
student name = "Alice"
Correct
student_name = "Alice"
3. Using Special Characters #
Incorrect
price@home = 500
Correct
price_home = 500
4. Using Keywords as Variable Names #
Incorrect
if = 100
Correct
value = 100
5. Using Meaningless Variable Names #
Poor Practice
x = 45000
Better Practice
employee_salary = 45000
Best Practices #
- Use meaningful and descriptive variable names.
- Follow the
snake_casenaming convention. - Keep variable names short but descriptive.
- Avoid abbreviations unless they are widely understood.
- Do not use Python keywords as variable names.
- Use lowercase letters for most variable names.
Key Points to Remember #
- Variable names can contain letters, digits, and underscores.
- A variable name must begin with a letter or an underscore.
- Variable names cannot begin with a digit.
- Spaces and special characters are not allowed.
- Variable names are case-sensitive.
- Python keywords cannot be used as variable names.
- Use descriptive names to improve code readability.
- Follow the
snake_casenaming convention recommended by PEP 8.