W3Basic Logo

Python Keywords and Identifiers

Keywords are predefined words, and these are reserved words that hold a special meaning and purpose in Python.

For Eg: True, False, if, and for are some of the predefined words.

Identifiers are user-defined words. It is the name given to the entities such as variables, constants, classes, and functions.

For Eg:

num = 1
text = "Hello, World!!!"

def print_data:
    print(num)
    print(text)

In the above example num, text and print_data are the identifiers.


Python Keywords

Keywords are reserved words in Python, and they hold a special meaning. Keywords define the Syntax and structure of the Python language.

We cannot use keywords as the name of the entities, such as variables, functions, classes, etc. If by mistake, use keywords as the names of these entities Python interpreter will throw SyntaxError.

As of Python 3.11, there are 35 reserved keywords in Python.

Falseawaitelseimportpass
Nonebreakexceptinraise
Trueclassfinallyisreturn
andcontinueforlambdatry
asdeffromnonlocalwhile
assertdelglobalnotwith
asyncelififoryield

You can also get the list of keywords by using the python interpreter help utility.

$ python

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51)

>>> help()

help> keywords

Here is a list of the Python keywords. Enter any keyword to get more help.

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

Alternatively, you can write a small piece of Python code to find out the list of keywords.

import keyword

print("Total number of Keywords:", len(keyword.kwlist))
print(keyword.kwlist)

Output

Total number of Keywords: 35

['False', 'None', 'True', '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 Identifiers

Identifiers are user-defined words. It is the name given to the entities such as variables, constants, classes, modules, packages, and functions.

Rules for creating Identifiers:

  • Identifier can be a combination of characters, digits, and underscore. The characters include both uppercase [A-Z] and lowercase [a-z], digits include [0-9] and underscore [ _ ].
  • An identifier cannot start with a digit; doing so will raise SyntaxErrror. For Eg: 1company is invalid, whereas company1 is valid.
  • Keywords cannot be used as identifiers
  • Identifiers are case-sensitive. For Eg: myVar and MyVar are not the same.
  • There is no limit on the length of the identifier name.
  • Identifiers can start with an underscore ( _ ). For Eg: _user is a valid identifier in Python.

We have seen the rules of identifiers; let us look at best practices and naming conventions in Python while creating identifiers.

Variable Naming Convention in Python

  • Python prefers a snake case for creating variable names.
  • Create the variable names in all lowercase. Eg: var1, count etc
  • In case of multiple words, separate them by an underscore. For Eg: my_name, employee_id etc
  • Ensure that you have a meaningful name for the variable. Avoid single-character variable names.

Constants Naming Convention in Python

  • Create constant names in all uppercase. For Eg: USER
  • In case of multiple words, separate them by an underscore. For Eg: EMPLOYEE_STATUS.
  • Ensure to have a meaningful name for constants.

Class Naming Convention in Python

*Class names should use the CapWords convention. Start each word of the class name with a Captial letter. For Eg: Student, EmployeeAddress

  • Do not separate with an underscore in case of multiple words.
  • Use camel case or pascal case while naming the class.

Function Naming Convention in Python

  • Create function names in all lowercase. Eg: check() , find() etc
  • In case of multiple words, separate them by an underscore. Eg: check_status(), get_name()
  • Ensure to have a meaningful function name.

Module Naming Convention in Python

  • Module names are preferred to be in all lowercase
  • Keep the module names simple and short. Do not use long names.
  • In case of multiple words, separate them by an underscore. For Eg: module.py, file_module.py.

Package Naming Convention in Python

  • Package names are preferred to be in all lowercase
  • Keep the package names simple and short. Do not use long names. For Eg: package, mypackage
  • DO NOT separate package names with an underscore in case of multiple words.

© 2023 W3Basic. All rights reserved.

Follow Us: