What are Identifiers and keywords in python



Python Identifier

Identifiers in python are user-defined names whereas keywords in python are reserved words used for special purposes. Identifiers are used to specify the names of variables, functions, classes, modules, and objects.

There are certain python naming conventions to be followed. Acceptably, the naming conventions of Python’s library are a bit of a mess, so we’ll never get this completely consistent — nevertheless, here are the currently recommended naming standards.

Naming standards in python
  • Identifiers used in the standard library must be ASCII compatible.
  • Class names should normally use the CapWords convention.
  • Function names should be lowercase, with words separated by underscores as necessary to improve readability.
  • Similarly, variable names follow the same convention as function names.
  • Likewise, Method Names and Instance Variables use the function naming rules.
  • Modules should have short, all-lowercase names. Underscores can be used in the module name if it improves readability.
  • Python packages should also have short, all-lowercase names, although the use of underscores is discouraged.
  • Constants are usually defined on a module level and written in all capital letters with underscores separating words.
  • Be careful when using the lowercase letter l and the uppercase letter O because they could be confused with the numbers 1 and 0.
  • Names are case sensitive. For instance, Address and address are two different things in python.
  • Avoid using Python keywords as a user-defined name; that is, do not use words that Python has reserved for a particular purpose, such as the word print.
  • Names can’t contain only digits or numbers. For instance, you can name a class, functions, or variable as twenty but not 20.
Common naming conventions used in programming
  • Camel Case: It is the practice of writing phrases without spaces or punctuation, indicating the separation of words with a single capitalized letter, however, the first word can start with either case. In a python programming language, the camel case is used in Class names. Example: BhutanCoder, eBay, studentDetails,….

  • Snake Case: refers to the style of writing in which each space is replaced by an underscore (_) character and the first letter of each word written in lowercase. In a python programming language, the Snake case is used to name the variable, function, method, and object. Example: bhutan_coder, student_details, permanent_address, etc… 

  • Screaming snake case: refers to names all written in capital letters. In a python programming language, it is often used when defining constants. Example: PI 


Three rules to keep in mind while naming identifiers

Rule 1# Consistency

Sometimes it becomes hard to follow pythons naming standards, however, maintaining the internal naming consistency is preferred. 

Rule 2# Descriptive

Names should be short but descriptive. Moreover, make sure that names themselves can convey some meaning. For example, the name is better than n, permanent_address is better than p_a.

Rule 3# Use Pronounceable Names

Remember that nobody wants a tongue twister or non-searchable words as a name of variable, function, or class. Therefore, pronounceable names are preferred. Example: permanent_address is better than perm_add.

Reserved Keyword

A python keyword is a reserved word which you can’t use as the name of your variable, class, function, module, and object. Moreover, they have a special meaning and they are used for special purposes in the Python programming language.

There are a total of 35 keywords in Python 3.8.2.


FalseNoneTrue
andasassert
asyncawaitbreak
classcontinuebreak
delelifelse
exceptfinallyfor
fromglobalif
importinis
lambdanonlocalnot
orpassraise
returntrywhile
withyield


Program to check the keyword in Python

All the python keywords can be accessed from the keyword module. Make sure that you don’t use any keyword displayed here as a user-defined name. Each keyword has a special meaning. So, to be a good programmer, you should be familiar with all the keywords and their functionality. For example, the def keyword is used to define a function.

main.py

import keyword
kw = keyword.kwlist
print(kw)	
print(f"Total number of keywords:{len(kw)}")

Output:

['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']
Total number of keywords:35

Related article:

Write your first python program

How to write a good python code

Follow us:

Facebook