Python Fundamentals - Keywords

In this tutorial, you will explore the basic introduction about all keywords, the basic building block of the python programming language. You will learn what keywords are, how to identify keywords from other words and their uses .

Python Keywords are special words reserved in python language used for some unique programming purposes. A keyword cannot be used as a variable name, function name, class name, or as any other identifier but can be used for specific purposes. For instance, Python Keyword – ‘break’ is used to break the flow of python code. Hence, ‘break’ cannot be used to name a variable or function or any other entities.

The New Version of python has 35 keywords (reserved words) which contribute a good vocabulary to the python language. 

How to List Keywords in your List?

Follow below mentioned easy steps to access the python keyword list in your OS. 

  1. Open the python shell or command prompt
  2. Type ‘Python’ then hit the enter button.
  3. Type the‘ help()’ function and hit enter.
  4. Now simply type ‘keywords’ in the help utility.
  5. You will get the list of python keywords available for your installed python version.

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

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

How to recognize keywords in Python? 

Python has facilitated numerous ways to find out or to check which words are reserved for keywords. They are listed below:

  1. Use help() utility: The simplest way is to pass the specific keywords in the help()utility which will give you the information on that keyword. Here is the instance for your understanding.
  2. Keyword Module to work with keywords: Another way to identify the keywords in python is by utilizing the keyword module which contains two entities for dealing with keywords. They are:
    • kwlist: provides the list of all keywords in the python version you are executing.
    • iskeyword(): returns true if the provided word is a keyword. 
  3. Inspect the syntax error: this is not a handy way to recognize the keyword, still, we can consider it as an option if any syntax error happens while attempting to assign values with keywords to use it for a function name or variable name. 
  4. Make use of IDE with syntax highlighters: A lot of IDE’s are available to work with python which uses highlighters for differentiating keywords from other words and is a handy way to quickly recognize the keywords while coding.

Python Keywords and their usage

Based on the usage, Keywords can be categorized as follows. 

  1. Value Keywords: True, False, None 

    These keywords are valued-based keywords as they can be used over and over again. These singleton values always reference the same objects.

    • True: Boolean True; - outputs True if the statement is True
    • False: Boolean False; - outputs False if the statement is False
    • None: Indicates no value.
  2. Operator Keywords: and, or, not, in, is:

    These keywords are used for computing arithmetic and logical operations.

    • and: Indicates the function of the logical operator “AND”  and returns true if and only if all operands are true otherwise return false
    • or: Indicates the function of the logical operator “OR” and returns false if and only if all operands are false otherwise return true
    • not: Indicates the function of the logical operator “NOT”: and returns the reverse of a given statement or operand.
    • in: is the membership operator that performs like the mathematical term contains,∈. This keyword checks whether a value is present or contained in a list or a set.
    • is: Used to check the identities of two objects 
  3. Control flow Keywords: if, elif, else

    These keywords are used to control the flow of code by allowing the user to use conditional logic and execute blocks of code that favor the condition.

    • if: A conditional statement that returns True Result when the condition is encountered correctly.
    • elif: CanThis can be considered as a switch statement in python where multiple if conditions are evaluated one after the other.
    • else: Executes the block of codes when the condition in ana conditional statement (if or elif ) is False 
  4. Iteration Keywords: for, while break, continue

    These keywords are used to create and work with iterations.

    • for: used to create iterations.
    • while: keyword used to represent loops.
    • break: breaks or terminates the loop and move to the next statement.
    • continue Skips the current iteration when a condition is triggered but continues with the remaining iteration in for loop or while loop.
  5. Structure Keywords: def, class, with,  pass, lambda

    • def : used to define a function or a class 
    • class: defines a class
    • with: Explored by context managers for controlling the resources like file or sub-processes or locks by properly exiting no matter if raises an exception
    • pass: A null statement used as a placeholder to combat indentation error
    • lamda: Defines an anonymous or nameless function in python
  6. Returning Keywords: return, yield

    • return: Returns the result to the caller once it terminates the execution of the function call.
    • yield: Returns a generator as the result.
    • Import Keywords: import, from, as
    • import: Used to import a specific module to a python program.
    • from: Used to include a particular section or functionality from the imported module. Always comes with import.
    • as: Offers a new name to the imported module.Eg: import calculator as calc
  7. Exception Handling Keywords: try, except, raise, finally, assert

    • try: Tries to catch an exceptional error in the program 
    • except: Works together with ‘try’  for error and exception handling
    • raise: Raises an exceptional error defined by the user.
    • finally: Used to ensure the smooth execution of codes no matter what happens in a try, except or else blocks.
    • assert: Used as debugging tool to test a condition: if returns false, an exceptional error will raise and is idle if returns true
  8. Asynchronous programming Keywords: async, await
    • async: used to define an asynchronous function or coroutine.
    • await: used to specify a point in the asynchronous function.
  9. Scope Keywords: global, nonlocal
    • global: Indicates the scope of a variable throughout the program even if the variable is declared inside a function
    • nonlocal: Indicates the scope of a variable is declared neither locally nor globally. Usually associated with nested functions.
  10. Deletion Keyword: del
    • del: Abbreviation of delete. It helps to remove objects present in the python script.

Python keywords and their corresponding activity is described below in a tabular form for easy access. For better understanding and readability keywords starting with uppercase characters are mentioned on the top rows followed by the lowercase keywords. From this, we can realize that Python Keywords are case-sensitive.

Sl No Python Keywords Description
1 True Boolean True; outputs True if the statement is True
2 False Boolean False; outputs False if the statement is False
3 None Indicates a void or null value.
4 and Indicates the function of the logical operator “AND”: returns true only if all operands are true otherwise return false
5 or Indicates the function of logical operator “OR”: return false only if all operands are false otherwise return true
6 not Indicates the function of the logical operator “NOT”: returns the reverse of a given statement or operand.
7 is Used to check if two objects reside in the same memory location or not thus testing the object's identities.
8 if A conditional statement that returns True Result when the condition is encountered correctly.
9 else Determines the next step perform when the condition in a conditional statement (if) is False
10 elif Short form of else-if:  Refer if and else
11 for Used to represent looping in python language.
12 while Used to represent looping in python language.
13 break breaks or terminate the loop and moves to the next statement
14 continue Skips the current iteration when a condition is triggered but continues with the remaining iteration in for loop or while loop.
15 pass A null statement used as a placeholder to combat indentation error.
16 return Returns the result to the caller once it terminates the execution of from function call.
17 yield Used in a generator function to return the result.
18 try Tries to catch an exceptional error in the program
19 except Works together with ‘try’  for error and exception handling
20 raise Raises an exceptional error defined by the user.
21 finally Used to ensure the smooth execution of codes even if ‘try’ raises an error or not
22 assert Used as a debugging tool to test a condition: it returns false, an exceptional error will raise and is idle if returns true
23 import Used to import a specific module to the python program.
24 from Used to include a particular section or functionality from the imported module. Always comes with import.
25 as Offers a new name to the imported module.
Eg: import calculator as calc
26 def Defines a normal function in python
27 lambda Defines an anonymous or nameless function in python
28 class Defines a class in python
29 global Indicates the scope of a variable throughout the program even if the variable is declared inside a function
30 non-local This indicates the scope of a variable is declared neither locally nor globally. Usually, associate with nested functions.
31 with Explored by content managers for controlling the resources like file or sub-processes or locks by properly exiting no matter if raises an exception
32 in Used to check whether a value is present in a list or a set. Also used in for loop.
33 del Abbreviation of delete. It helps to remove objects present in the python script.