Python Variable Names
Variable names in Python are used to identify data stored in memory.
Choosing correct and meaningful variable names makes code readable, clean, and professional.
Python follows specific naming rules and conventions that every developer should follow.
Rules for Python Variable Names
Before learning naming styles, understand these basic rules:
- A variable name must start with a letter (a–z, A–Z) or underscore (_)
- It cannot start with a number
- It can contain letters, numbers, and underscores
- Variable names are case-sensitive
- Python keywords cannot be used as variable names
Code Example 1: Valid Variable Names
python
name = "Python"
_version = 3
user1 = "Admin"
Code Example 2: Invalid Variable Names
python
# 1name = "Error"
# user-name = "Error"
# class = "Error"
Multi Words Variable Names
When a variable name contains more than one word, it must be written in a readable format.
Python does not allow spaces in variable names.
Instead of:
text
user name
We use naming conventions.
Key Idea
Multi-word variable names improve clarity and understanding.
Camel Case
In Camel Case, the first word starts with a lowercase letter,
and each new word starts with a capital letter.
Format
text
myVariableName
Code Example 1: Camel Case
python
userName = "Jayesh"
accountBalance = 5000
Code Example 2: Camel Case with Numbers
python
userAge1 = 25
Code Example 3: Camel Case Usage
python
totalMarksObtained = 450
Usage
Camel Case is commonly used in Java and JavaScript, not preferred in Python.
Pascal Case
In Pascal Case, every word starts with a capital letter, including the first one.
Format
text
MyVariableName
Code Example 1: Pascal Case
python
StudentName = "Amit"
TotalScore = 95
Code Example 2: Pascal Case Usage
python
EmployeeSalary = 60000
Code Example 3: Pascal Case in Classes
python
class StudentDetails:
pass
Best Practice
Pascal Case is mainly used for class names in Python.
Snake Case
In Snake Case, words are separated using underscores (_),
and all letters are usually lowercase.
Format
text
my_variable_name
Code Example 1: Snake Case
python
user_name = "Python"
account_balance = 10000
Code Example 2: Snake Case with Numbers
python
student_mark_1 = 85
Code Example 3: Recommended Style
python
total_order_count = 120
Python Standard
Snake Case is the recommended naming convention in Python (PEP 8).
Which Naming Style Should You Use in Python?
| Style | Usage in Python |
|---|---|
| Camel Case | ❌ Not recommended |
| Pascal Case | ✅ Class names |
| Snake Case | ✅ Variables and functions |
Learning Insight
Following naming conventions makes your code Pythonic and professional.
Summary
- Variable names identify stored data
- Multi-word names need proper formatting
- Camel Case uses capital letters inside words
- Pascal Case capitalizes every word
- Snake Case uses underscores and lowercase
- Snake Case is best for Python variables
Exercise
- Create three variables using Snake Case
- Create a class using Pascal Case
- Identify incorrect variable names and fix them