Python Keywords

Python keywords are reserved words that have special meanings and are used to define the syntax and structure of the Python language. These keywords cannot be used as variable names or other identifiers because they are reserved for specific purposes in the language. The number of keywords might change in different python versions. There are 35 keywords in Python 3.10.4

List of Python Keywords:

'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'

Since the keywords for a specific version might change, you can always get a list of keywords using below code:

import keyword

keywords_list = keyword.kwlist
print('Total No. Of Keywords: ', len(keywords_list))
print('All Keywords: ',keywords_list)

##output will be 
Total No. Of Keywords:  35
All Keywords:  ['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']

Soft Keywords:

Some identifiers are only reserved under specific contexts. These are known as soft keywords. The identifiers match, case and _ can syntactically act as keywords in contexts related to the pattern-matching statement, but this distinction is done at the parser level, not when tokenizing. As soft keywords, their use with pattern matching is possible while still preserving compatibility with existing code that uses match, case and _ as identifier names

import keyword

keywords_list = keyword.softkwlist
print('Total No. Of Soft Keywords: ', len(keywords_list))
print('All Soft Keywords: ',keywords_list)

##output will be
#Total No. Of Soft Keywords:  3
#All Soft Keywords:  ['_', 'case', 'match']

How to check if a string is a valid keyword in Python?

#how to check if a string is a reserved keyword or not
import keyword

s = 'while'

if keyword.iskeyword(s):
    print(s,' is a reserved keyword')
else:
    print(s,' is a not a reserved keyword')

# output will be
# while  is a reserved keyword

How to check if a string is a valid soft keyword in Python?

#how to check if a string is a soft keyword or not
import keyword

s = 'match'

if keyword.issoftkeyword(s):
    print(s,' is a soft keyword')
else:
    print(s,' is a not a soft keyword')

##output will be
#match  is a soft keyword

Keywords In Detail

Here’s an explanation of each Python keyword:

  1. False: This keyword represents the boolean value False.
  2. class: This keyword is used to define a new class in Python.
  3. finally: This keyword is used to define a block of code that is executed after a try block, regardless of whether an exception was raised or not.
  4. is: This keyword is used to test if two variables refer to the same object in memory.
  5. return: This keyword is used to return a value from a function.
  6. None: This keyword represents the null value in Python.
  7. continue: This keyword is used to skip the current iteration of a loop.
  8. for: This keyword is used to define a loop that iterates over a sequence of elements.
  9. lambda: This keyword is used to create a small anonymous function.
  10. try: This keyword is used to define a block of code that may raise an exception.
  11. True: This keyword represents the boolean value True.
  12. def: This keyword is used to define a new function.
  13. from: This keyword is used to import specific attributes or functions from a module.
  14. nonlocal: This keyword is used to reference a variable in the closest enclosing scope outside of the current function.
  15. while: This keyword is used to define a loop that executes while a certain condition is true.
  16. and: This keyword is used to perform a logical and operation.
  17. del: This keyword is used to delete an object.
  18. global: This keyword is used to indicate that a variable is a global variable.
  19. not: This keyword is used to perform a logical not operation.
  20. with: This keyword is used to define a block of code that uses a context manager.
  21. as: This keyword is used to give a different name to an imported module or attribute.
  22. elif: This keyword is used to define an alternate condition in an if statement.
  23. if: This keyword is used to define a conditional statement.
  24. or: This keyword is used to perform a logical or operation.
  25. yield: This keyword is used in a function to yield a value to the caller.
  26. assert: This keyword is used to assert that a certain condition is true.
  27. else: This keyword is used to define the alternate branch of an if statement.
  28. import: This keyword is used to import a module.
  29. pass: This keyword is used as a placeholder where code will eventually go.
  30. break: This keyword is used to break out of a loop.
  31. except: This keyword is used to catch an exception.
  32. in: This keyword is used to test if a sequence contains a certain value.
  33. raise: This keyword is used to raise an exception.
  34. async: This keyword is used to define a coroutine function.
  35. await: This keyword is used to wait for a coroutine function to complete.

    The codes mentioned in this post can be downloaded from github.com. Share the post with someone you think can benefit from the information.

    Leave a Comment

    Share this