Naming Conventions in Python

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.

  1. Why naming conventions
  2. Identifiers and rules
  3. Validating identifier names with examples
  4. Identifiers in a table
  5. Smart suggestions while writing identifiers in Python
  6. Indentation and Comments in Python
  7. Multiple Programs to understand above concepts
What is an identifier in Python?

A Python identifier is a name used to identify a variable, function, class, module or other object. This name can be

  1. class name
  2. package name
  3. variable name
  4. module name
  5. function name
  6. 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,

  1. Alphabets, these can be either lowercase or uppercase.
  2. Digits (0 to 9)
  3. Underscore symbol (_)
  4. 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

emp_id = 11

Output: 11

Example: Variable name with symbols

$tudent_id = 23

Output: SyntaxError: invalid syntax

Example: Variable name with digits

student1 = 12

Output: 12

Example: Variable name starts with digits

1stundent = 12

Output: SyntaxError: invalid syntax

Example: Case sensitive

a = 10
b = 20

Output: NameError: name ‘A’ is not defined

Example: Keyword as variable name

if = 10

Output: SyntaxError: invalid syntax

Example: Space in variable name

student id = 10
print(student id)

Output: SyntaxError: invalid syntax

Examples of Identifiers in Python:
  1. 435student invalid (Should not start with a digit)
  2. student564 → valid
  3. student565info → valid
  4. $tudentinvalid (Should not start with symbol)
  5. _student_info → valid (Can start with underscore )
  6. class invalid (Keyword can’t be used as identifier)
  7. def invalid (Keyword can’t be used as identifier)
Identifiers best practices in a table

Naming Conventions in Python with examples


Be descriptive – A variable name should depict exactly what it contains and a function should describe exactly what it does

  1. a = 10 # valid but not recommended
  2. student_id = 10 # valid and highly recommended
  3. abc() # valid but not recommended
  4. sum() # valid and highly recommended

Don’t use abbreviations unnecessarily. Abbreviations may be ambiguous and more difficult to read.

  1. fourth_bentch_middle_student_id = 10 #valid but not recommended
  2. student_id = 10 #valid and highly recommended
Indentation in Python

In Python, we need to group the statements by using indentation.

  1. Indentation keeps the group of statements separate.
  2. The recommended indentation is 4 spaces.
  3. We must follow the order of indentation otherwise we will get IndentationError
valid Indentation

print(“statement one”)
print(“statement two”)
print(“statement three”)

statement one
statement two
statement three

Invalid Indentation

print(“statement one”)
         print(“statement two”)
print(“statement three”)

Output: IndentationError: unexpected indent

Invalid Indentation

print(“statement one”)
         print(“statement two”)
                  print(“statement three”)

Output: IndentationError: unexpected indent

Comments in Python:
  1. Comments are useful to describe the code in an easy way.
  2. Python ignores comments while running the program.
  3. To comment python code, we can use hash symbol #
  4. For multi line comments we can use single/double/triple quotation marks.
Comments Example in Python:

# This is 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 which are suggested to be followed while naming an identifier. The benefits of following the naming conventions are:

  1. They help us in understanding what a particular identifier is.
  2. When we are able to identify what is class name, variable name etc. then our time and effort to understand the code will be decreased.
  3. Following naming convention will increase the readability of code
  4. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *