Python Naming Conventions Identifiers Reserved Words in Python

Python Identifiers, Reserved Words, Python Naming Convention – Learn Python With Me

In our previous chapters, we learned about Python Introduction, Install Python and Run First Python Program, Python Variables and Datatypes, Python Comments, Literals, Constants, Docstrings. We will discuss some concepts in this post, such as Python Identifiers, Reserved Words, Python Naming Convention, which depend on these previous chapters. So, if you are visiting our blog for the first time, you might want to read the earlier posts first. Please also find the link below to take you to the list of all chapters published in our “Learn Python with me” tutorial series. Without wasting much time, let us dive into the topics of this post.


Link to all Python Chapters: Learn Python


Python Identifiers

Python Variable Operator and Literal
  • An identifier refers to the names we provide for labels, functions, types, or variables in any programming language.
  • Similarly, in Python, we use identifiers to name variables, functions, modules, or objects. There are certain naming convention rules on how to name each item in Python.
  • An identifier in Python can contain the underscore character (_), numbers, and letters.
  • However, they should begin with a nonnumeric character.
  • Symbols such as @, %, $, #, and ? are not permitted in identifiers.
  • Examples for identifiers are money, firstname11, gross_salary, etc.

We should also note that Python is a case-sensitive programming language as it recognizes the names with capital letters and lower case letters differently. It means that names, salary, and Salary are treated differently and thus refer to different variables in Python.

Reserved Words in Python

Don't Use Python Reserved Words as Identifiers

Just as the name sounds, the Reserved Words in any programming language are the set of words that you cannot use as an identifier. They are prevented from using as they are already booked for a particular purpose. Following is the list of Python reserved words.

return
lambda
for
def
True
False
yield
with
while
try
nonlocal
not
or
pass
print
raise
from
global
if
import
in
is
del
elif
else
except
exec
finally
continue
class
break
assert
as
and

You can also use the below command to list the set of Python Reserved words in your interpreter.

import keyword
keyword.kwlist
Output

>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']

Python Naming Convention

A naming convention in the arena of the computer programming field is the set of practices that a programmer has to follow to name functions, variables, etc. Like other programming languages, Python also has certain Naming Convention rules that Python developers suggested. This will be helpful for aspirant python learners like us to know how to name a function or variable in Python.

Python Naming Convention
  1. Global Variables or Module Level Variables: All Global Variables should be named in lower case letters. If several words are combined together for a name, each of them should be divided using an underscore (_).
  2. Instance Variables: Like Global Variables, the Instance Variables should also be named in lower case letters and an underscore (_) should be used to divide the words in the variable if several words are in the name. Also, an underscore (_) should be used at the beginning of the non-public instance variables.
  3. Functions: You should name the Python function names only in lower letters and rely on underscore (_) if several words are used in the name to divide each one of them.
  4. Methods: All Python method names should be in lower case letters. And you should use underscore (_) to divide if multiple words are used in a name.
  5. Constants: As discussed in our previous chapter, all Constants in Python should be declared in Capital letters. And you should use underscore (_) to divide if multiple words are used in a name.
  6. Method Arguments: The first argument should be cls in the case of class methods and the first argument should be self in the case of instance methods.
  7. Classes: In Python, each word of Class name that we create should begin with a capital letter. However, the inbuilt classes of Python uses only lower case letters. Also, if you are going to add a class for treating exceptions, Error should be mentioned at the end of its name.
  8. Modules: You should name the module names in lower case letters in Python. And you should use underscore (_) to divide if multiple words are used in a name.
  9. Packages: Python packages should be named in lower case letters. And you should use underscore (_) to divide if multiple words are used in a name.
  10. Entities that are not accessible: Some methods, functions and variables have limitations in terms of their visibility and can be used only within a program. You should name such entities with two double quotes before and after. For instance, __init__(self) is a function used in a class to initialize variables.

These naming conventions might look confusing to you now. However, do not worry or bother thinking. Because we will follow these rules as we keep progressing our Python learning curve in the further chapters and you will be able to understand the proper naming convention by the time you complete the entire “Learn Python with me” course on our blog.

In this post, we learned about Python Identifiers, Python Reserved Words, and Python Naming Convention. These topics will be helpful for you while declaring any variable or function in your Python programs, and your code can easily be understood with these sets of rules.

Would you mind sharing with us your experience of learning these Python concepts in the comments section below? We would love to hear from you.

1 thought on “Python Identifiers, Reserved Words, Python Naming Convention – Learn Python With Me”

Comments are closed.

Scroll to Top