What are variable names in Python?
This chapter covers Python variables names with examples, which we hope will satisfy the learning requirements.
Python Variables Names
You can have a variable with a short name, such as A and B. However, you can assign it a more meaningful name, such as your phone number, your surname, or your school’s name.
Variables in Python are subject to the following restrictions:
- The name of a variable cannot contain a reserved word (keyword).
- It is not possible to start a variable name with a number.
- There must be a letter or underscore at the beginning of a variable name.
- It is imperative to keep in mind that variable names are case-sensitive (tesla, Tesla, and TESLA).
- During the creation of a variable name, you must avoid using characters other than alphanumeric characters (A-z, 0-9, and _).
The correct way to define variable names:
Example: 
Incorrect syntax to declare variable names:
Example: 
Tip: You should always remember that case is significant when naming variables.
Reserved Words (Keywords) in Python
The names of identifiers are also restricted. Reserved words cannot be used as names for objects.
Several keywords are reserved in Python for identifying special language functionality.
The following 33 keywords are reserved in Python 3.6:
Python Keywords:
False | def | if | raise |
None | del | import | return |
True | elif | in | try |
and | else | is | while |
as | except | lambda | with |
assert | finally | nonlocal | yield |
break | for | not | |
class | from | or | |
continue | global | pass |
Python Variables names with multiple words
Reading variable names with a significant number of words can take time and effort.
Python variable names can be made more readable using several techniques:
Camel Case:
There is a capital letter at the beginning of each word, except for the first:
Pascal Case
The first letter of each word in the following example is capitalized:
Snake Case
An underscore character separates each word in the following manner:
You can use whatever looks most attractive to you visually. You should pick one and use it consistently.
You can assign names not only to variables but also to other objects.
The same applies to functions, classes, modules, etc.
Program objects and variables are both named according to the same rules that apply to variable names.
Python variables names and how to declare them in Python programs are now clear to you.
Python Variable Names Importance
Proper variable names in Python, or any programming language, are important for several reasons:
- Using descriptive and meaningful variable names makes your code more readable and understandable. When you or others read the code, clear variable names provide context and make it easier to grasp the purpose and functionality of the code. This can save time and effort when reviewing or maintaining the code in the future.
- Proper variable names contribute to code maintainability. When variable names accurately reflect their purpose, it becomes easier to modify or extend the code later on. You or other developers can quickly identify which variables to update or work with, reducing the chance of introducing errors during code modifications.
- When working on a project with a team, using clear and consistent variable names helps with collaboration. Team members can easily understand each other’s code and work together more effectively. Good variable names also promote communication and reduce confusion when discussing code or reviewing each other’s work.
- Inevitably, code may contain errors or bugs that need to be resolved. With proper variable names, debugging becomes easier as you can quickly identify variables involved in problematic sections of the code. Meaningful names can help pinpoint the source of the issue and facilitate the troubleshooting process.
- Variable names serve as a form of self-documentation. When reviewing or revisiting your code, well-chosen names can act as reminders of the purpose and intent behind specific variables. This reduces the need for extensive comments or documentation solely for explaining variable functionality.
- Using proper variable names demonstrates professionalism and good coding practices. It shows that you care about code clarity and maintainability. Employing meaningful names is considered a best practice in programming and contributes to the overall quality of your code.