2 min read

Python Keywords

Python has a set of keywords that are reserved words that cannot be used as variable names, function names, or any other identifiers.

Getting the List of Keywords

You can get the list of keywords programmatically using the keyword module.

import keyword

print(keyword.kwlist)

List of Keywords

Here is a categorized list of the Python keywords:

Value Keywords

  • True: Boolean true.
  • False: Boolean false.
  • None: Represents the absence of a value.

Operator Keywords

  • and: Logical AND.
  • or: Logical OR.
  • not: Logical NOT.
  • in: Check membership.
  • is: Check identity.

Control Flow Keywords

  • if: Conditional statement.
  • elif: Else if.
  • else: Else.
  • for: Loop over an iterable.
  • while: Loop while a condition is true.
  • break: Break out of a loop.
  • continue: Continue to the next iteration of a loop.
  • pass: Null statement (does nothing).

Function and Class Keywords

  • def: Define a function.
  • class: Define a class.
  • lambda: Define an anonymous function.
  • return: Return a value from a function.
  • yield: Yield a value from a generator.

Exception Handling Keywords

  • try: Start an exception handling block.
  • except: Catch an exception.
  • finally: Execute code regardless of whether an exception occurred.
  • raise: Raise an exception.

Variable Handling Keywords

  • global: Declare a global variable.
  • nonlocal: Declare a variable from an outer scope.
  • del: Delete an object.

Import Keywords

  • import: Import a module.
  • from: Import specific parts of a module.
  • as: Create an alias.

Context Manager Keywords

  • with: Context manager statement.

Asynchronous Keywords

  • async: Define a coroutine.
  • await: Wait for a coroutine.

Debugging Keywords

  • assert: Debugging assertion.

Checking for Keywords

You can check if a string is a keyword using keyword.iskeyword().

import keyword

print(keyword.iskeyword("if"))  # Output: True
print(keyword.iskeyword("hello")) # Output: False

programming/python/python