Keywords in Python

Keywords in Python

Every programming language has its own set of keywords that are reserved for special purposes. These keywords cannot be used for any other purpose such as variable name or class name.

Python has several keywords and a few rules for writing identifiers. In this tutorial, we will discuss Python keywords.

List of keywords in python

Types of Python Keywords

The Python keywords can be divided into several categories based on their usage.

Control flow keywords – Used to control the flow of execution

  • if
  • else
  • elif

Iteration keywords – Used for creating and working with loops

  • for
  • while
  • break
  • continue
  • else

Value keywords – Used as values

  • True
  • False
  • None

Returning keywords – Used for returning data from methods or functions

  • return
  • yield

Operator keywords – Used as operators

  • and
  • or
  • in
  • not
  • is

Import keywords – Used for importing modules

  • import
  • from
  • as

Structuring keywords – Used in structuring the program

  • def
  • with
  • as
  • class
  • pass
  • lambda

Exception-handling keywords – Used in exception handling

  • try
  • finally
  • except
  • raise
  • assert

Variable handling keywords – Used for working with variables

  • global
  • del
  • nonlocal

Asynchronous keywords – Used to handle asynchronous data

  • async
  • await

Note: Except for value keywords (True, False, None) all other keywords are case sensitive.

Remember, the keywords should not be used as variable names, class names, or function names. Observe the following code.

If keywords are are used else where not intended to use python interpreter throws a Syntax Error: Invalid syntax

Example of Syntax error

def global():
	print("This is a function")
global()

The code will give a syntax error because “global” is one of the Python keywords and it cannot be used as a function name. Let’s make some changes to the above code.

Syntax error: invalid syntax
def Global():
	print("This is a function")
Global()

The first letter of the function name is capitalized, meaning, it not a keyword anymore (because Python keywords are case-sensitive).


Thank you for reading, Happy Learning, drop your suggestion in the comments.

Feel free to follow us on Youtube, Linked In , Instagram

Loading comments...
Mastodon