python

Python Keywords and Identifiers


In this tutorial, you will learn about keywords (reserved words in Python) and identifiers (names given to variables, functions, etc.).


Python Keywords

Keywords are the reserved words in Python. It is also known as reserved words. A keyword in a programming language is defined as which has a fixed meaning and cannot be redefined by the programmer or used as identifiers.
In Python, keywords are case sensitive. There are 35 keywords in Python. This number can vary slightly over the course of time. All the keywords except True, False and None are in lowercase and they must be written as they are. The list of all the keywords is given below.

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

We can get the above list by using help() in Python Shell


Python Identifiers

What is an identifier?

An identifier is a name given to entities like class, functions, variables, etc. It helps to differentiate one entity from another.

Rules for Identifiers

  1. 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 (_).
  2. No special character except underscore is allowed in the name of a variable
    m@n='Raj'
    print(m@n)

    Output
    m@n='Raj'
    ^
    SyntaxError: cannot assign to operator
  3. It can begin with a underscore ( _ ) or a letter but not with a digit . Although after the first letter we can have as many digits as we want. So 1a is invalid , while a1 or _a or _1 is a valid name for an identifier.
  4. Identifiers are case sensitive , so pi and Pi are two different identifiers
  5. Keywords cannot be used as identifiers
    class = 'vehicle'
    Output
    class = 'vehicle'
    ^
    SyntaxError: invalid syntax
  6. Identifier can be of any length.There is no word limit provided for identifier's name in Python.

Things to remember.

As we discussed above ,Python is a case-sensitive language. This means, Class and class are not the same. Try to name the identifiers which makes sense.Give simple and understandable name to the identifier. While am = 2 is a valid name. You can also use multiple words for an identifier's name but use underscore(_) to diffrentiate between them, like this_is_a_car_vehicle.