List of all Keywords in Python

In this article, we will discuss the keywords in Python. Keywords are reserved words and have special meaning and cannot be used for naming variables, functions or classes. Each of these keywords serves a special purpose. Let’s discuss in detail about keywords.

Read: Keyword Module in Python

List of keywords in Python:

  1. False – used to indicate the boolean false value.
  2. None – used to indicate a null value.
  3. True – used to indicate a boolean true value.
  4. and – used to indicate the logical and operator.
  5. as – used for aliasing.
  6. assert – used to check certain conditions which upon failing will throw a runtime error.
  7. break – used for breaking out of loops (control flow).
  8. class – used to declare and define a class.
  9. continue – used to continue to the next iteration of a loop.
  10. def – used to define a function.
  11. del – used to delete an object.
  12. elif – used to specify the else if condition.
  13. else – used to indicate the else block.
  14. except – used to catch errors when used along with a try block.
  15. finally – used to specify a block of code along with try and except that is always executed.
  16. for – used to indicate a for loop.
  17. from – used along with import to import packages.
  18. global – used to specify the usage of global variables.
  19. if – used to create an if block.
  20. import – used to import packages.
  21. in – used to check existence of an element in an iterable.
  22. is – used to check the object identity.
  23. lambda – used to create an inline function.
  24. nonlocal – used to declare variables in nested functions.
  25. not – used as a logical not operator.
  26. or – used as a logical or operator.
  27. pass – used for semantic purposes with no real function.
  28. raise – used to raise errors.
  29. return – used to return values from a function (control flow).
  30. try – for error handling.
  31. while – used to indicate a while loop.
  32. with – used for readability.
  33. yield – used to return a generator.

Any of the above keywords cannot be used as identifiers, violation of which will result in a syntax error.

Getting the list of keywords in Python:

You can get a list of all keywords in Python with the following code:

import keyword

# Print the list of all keywords in Python
print(keyword.kwlist)

# Checks if it is a keyword
print(keyword.iskeyword("for"))

The output is:

['False', 'None', 'True', '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']
True

I hope you found this article useful for understanding keywords in Python.

See also:

Leave a Reply

Your email address will not be published. Required fields are marked *