Python Keywords and Identifiers

This sections explains keywords (reserved words in Python) and identifiers (names given to variables, functions, classes, etc.).

Python Keywords

There are 35 keywords in Python 3.8.2. The number of keywords varies based on the Python version.

  • Keywords are the reserved words in Python.

  • Keyword cannot be used as a variable name, function name or any other name identifiers.

  • Keywords are fundamental part of Python language, its syntax and structure.

  • Keywords, similar to variable or function names, are case-sensitive.

Only three keywords has first letter as CAPITAL (True, False,None), the rest is written in lower case (remember, keywords are case-sensitive)

All the keywords except True, False and None are in lowercase and they must be written as it is. The list of all the keywords is given below.

Keywords in Python version 3.8.2

False

True

None

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

You can check the desired name against the list above easily to determine if it is a reserved keyword. This module allows a Python program to determine if a string or identifier is a keyword.

import keyword
keyword.iskeyword(variable_name)

OUTPUT:

  • Returns True if variable_name is reserved keyword

  • Returns False if variable_name is NOT reserved keyword

True
False

Python Identifiers

An identifier is a name that the user gives to any user created variables, functions, classes, decorators, generators etc.

Rule of thumb for identifiers nomenclature :

  • There is NO limit on the length of an identifier name. It can be a character or 1000.

  • Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore _.Names like MyFunc, myFunc, my_variable_1 and my_awesome_coding_skills, all are valid example.

  • Identifiers can start with _, for example; __variable is viable but it cannot start with number, for example; 22variable or 22_variable is not viable identifiers and Python throws syntax error

  • Python reserved keywords cannot be used as identifiers.

class = 2020
  • We cannot use special symbols like !, @, #, $, % etc. in our identifier.

group@ = 3

Last updated