Table of Contents
ToggleThis tutorial we will discuss about identifiers in python. We will also see the naming rules, best practices, and reserved keywords and classes for Python Identifiers. We will also answer some of the questions like Is Python Case Sensitive When Dealing With Identifiers and what is maximum length of identifier in python.
Let’s get started with Identifiers in Python.
What are Identifiers in Python?
An identifier is a user-defined name to represent the basic building blocks of Python like a variable, function, class, module, etc. The identifier is a combination of character digits and underscore. The identifier should start with a character or Underscore then use a digit. The characters are A-Z or a-z, an UnderScore ( _ ) , and digit (0-9). we should not use special characters ( #, @, $, %, ! ) in identifiers.
Examples of valid identifiers:
- variable1
- _abc2
- _2_variable
- pqr_3
Examples of invalid identifiers:
- !abc1
- 2pqr
- 2_abc
- variable#2
Rules for Naming Identifiers in Python
- A Python identifier can be a combination of lowercase/ uppercase letters, digits, or an underscore. The following are allowed in Identifiers in Python:
- Lowercase letters (a to z)
- Uppercase letters (A to Z)
- Digits (0 to 9)
- Underscore (_)
- An identifier cannot begin with a digit
- Special symbols like ( #, @, $, %, ! ) are not allowed in the identifier name.
- Keywords cannot be used as an identifiers in python.
We will discuss more about keywords further in the article.
Is Python Case Sensitive When Dealing With Identifiers
Python is a case sensitive programming language. Thus, “Variable” and “variable” are two different identifiers in Python.Thus, if a variable is named ‘Name’, then an error will occur if the variable is called ‘name’. Thus, Python is Case Sensitive When Dealing With Identifiers
Name="Joseph" print(name) # Output: NameError: name 'name' is not defined
Maximum Length Of Identifier In Python
Python identifiers can be unlimited in length but as per PEP-8 you should limit identifier to a maximum of 79 character for better readability but there is no restriction.
Reserved Keywords And Classes In Python
Reserved Keywords
Keywords are predefined words and reserved words that can be used in Python to indicate special meanings. The keywords are used to specify the syntax of the code. Keywords cannot be used to identifier, function or name variables. With the exception of None,True and False all keywords in Python are written in lowercase. Python 3.7 contains 33 keywords.
None | break | except | in | raise |
False | await | else | import | pass |
and | continue | for | lambda | try |
True | class | finally | is | return |
as | def | from | nonlocal | while |
async | elif | if | not | with |
assert | del | global | or | yield |
Reserved Classes
- Single leading underscore (_*): This identifier allows you to store the results of the last evaluation in your interactive interpreter. These results are stored within the module __builtin__. These variables are private and cannot be imported using “from module import *”.
- Double leading and trailing underscores (__*__): This notation is only used by system-defined names. These names are defined by the interpreter, and it’s implementations. This convention is not recommended for additional names.
- Leading double underscores (__*): These category names can be used in the context of a class description. These are rewritten to avoid name conflicts between private variables of base classes and derived classes.
Best Practices for Identifiers In Python
It is important to adhere to the rules but it is also important to follow these recommended practices.
- Capital letters should be used for class names. For example Student, Person etc.
- Uppercase should be used for any class names that contain multiple words. For example StudentsRecord, PersonDetails etc.
- For variables, functions and module names, you should use small letters. For example, name, age, foo() etc.
- If variables, functions, or module names contain multiple words, then you should separate them by an underscore(_). For example student_name, person_object etc.
- Private variables can be starts with an underscore(_). For e.g. _private_details etc.
- Avoid underscore at beginning and last of variable name as it is used by the python-built-in types.
- If there are two underscores at the end of an identifier, it is likely that the identifier refers to a language-defined name. Avoid having underscores at both the beginning and end of your identifier name.
- To clarify the intent of identifier names, keep them meaningful. For example moblie_number, permanent_address
- It’s best to begin a function name with “is” if it returns boolean values. For example, isPalindrome(), isDigit() etc.
- The length of an identifier name in python is unlimited. Keep it short and to the point. For example, the_employee_permanent_address can be better named as emp_permanent_addr.
Conclusion
In this article we discussed all about identifiers in python, naming rules and best practices to follow to make your code better readable.
Got a question or just want to chat? Comment below or drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out!