Back to: Python Tutorials For Beginners and Professionals
Naming Conventions in Python
In this article, I am going to discuss Naming Conventions in Python with Examples. Please read our previous article where we discussed Python Coding Instructions. At the end of this article, you will understand the following pointers in detail.
- Why naming conventions
- Identifiers and rules
- Validating identifier names with examples
- Identifiers in a table
- Smart suggestions while writing identifiers in Python
- Indentation and Comments in Python
- Multiple Programs to understand the above concepts
What is an identifier in Python?
A Python identifier is a name used to identify a variable, function, class, module, or other objects. This name can be
- class name
- package name
- variable name
- module-name
- function name
- method name
Python developers made some suggestions to the programmers regarding how to write identifiers in a program.
Rules to define identifiers in Python:
The only allowed characters to write identifier in python are,
- Alphabets can be either lowercase or uppercase.
- Digits (0 to 9)
- Underscore symbol (_)
- Usage of any other symbol (like $,!,#, @) apart from these will result in Syntax Error.
Identifiers allow digits, but identifiers should not start with digits. Identifiers are case-sensitive. We cannot use keywords as identifiers. Spaces are not allowed between identifiers.
Example: Program to print variable name
Code:
emp_id = 11
print(emp_id)
Output: 11
Example: Variable name with symbols
Code:
$tudent_id = 23
print($tudent_id)
Output: SyntaxError: invalid syntax
Example: Variable name with digits
Code:
student1 = 12
print(student1)
Output: 12
Example: Variable name starts with digits
Code:
1stundent = 12
print(student1)
Output: SyntaxError: invalid syntax
Example: Case sensitive
Code:
a = 10
b = 20
print(A+B)
Output: NameError: name ‘A’ is not defined
Example: Keyword as variable name
Code:
if = 10
Output: SyntaxError: invalid syntax
Example: Space in variable name
Code:
student id = 10
print(student id)
Output: SyntaxError: invalid syntax
Examples of Identifiers in Python:
- 435student → invalid (Should not start with a digit)
- student564 → valid
- student565info → valid
- $tudent → invalid (Should not start with symbol)
- _student_info → valid (Can start with underscore )
- class → invalid (Keyword can’t be used as identifier)
- def → invalid (Keyword can’t be used as identifier)
Identifiers best practices in a table
Suggestions:
Be descriptive – A variable name should depict exactly what it contains and a function should describe exactly what it does
- a = 10 # valid but not recommended
- student_id = 10 # valid and highly recommended
- abc() # valid but not recommended
- sum() # valid and highly recommended
Don’t use abbreviations unnecessarily. Abbreviations may be ambiguous and more difficult to read.
- fourth_bentch_middle_student_id = 10 #valid but not recommended
- student_id = 10 #valid and highly recommended
Indentation in Python
In Python, we need to group the statements by using indentation.
- Indentation keeps the group of statements separate.
- The recommended indentation is 4 spaces.
- We must follow the order of indentation otherwise we will get IndentationError
valid Indentation
Code:
print(“statement one”)
print(“statement two”)
print(“statement three”)
Output:
statement one
statement two
statement three
Invalid Indentation
Code:
print(“statement one”)
print(“statement two”)
print(“statement three”)
Output: IndentationError: unexpected indent
Invalid Indentation
Code:
print(“statement one”)
print(“statement two”)
print(“statement three”)
Output: IndentationError: unexpected indent
Comments in Python:
- Comments are useful to describe the code in an easy way.
- Python ignores comments while running the program.
- To comment python code, we can use hash symbol #
- For multi-line comments, we can use single/double/triple quotation marks.
Comments Example in Python:
Code:
# This is a Basic program in python
print(“Welcome to python programming”)
Output: Welcome to python programming
Why naming conventions are important in Python?
Naming conventions are a set of rules or best practices that are suggested to be followed while naming an identifier. The benefits of following the naming conventions are:
- They help us in understanding what a particular identifier is.
- When we are able to identify what is the class name, variable name, etc. then our time and effort to understand the code will be decreased.
- Following naming convention will increase the readability of code
- The naming conventions will help us in debugging the code fastly.
In the next article, I am going to discuss Variables in Python. Here, in this article, I try to explain Naming Conventions in Python. I hope you enjoy this Naming Conventions in Python article. I would like to have your feedback. Please post your feedback, question, or comments about this article.