The Wayback Machine - https://web.archive.org/web/20240710004611/https://www.geeksforgeeks.org/keyword-module-in-python/
Open In App

Keyword Module in Python

Last Updated : 27 Feb, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Python provides an in-built module keyword that allows you to know about the reserved keywords of python.

The keyword module allows you the functionality to know about the reserved words or keywords of Python and to check whether the value of a variable is a reserved word or not. In case you are unaware of all the keywords of Python you can use this module to retrieve this information. Also, it helps you to check whether a word is a keyword or not just by using its functions on Python shell mode.

The functions of this module are:

  • keyword.iskeyword(parameter)
    This function returns True if the parameter passed is a Python keyword else returns False. The parameter can be a string or a variable storing a string. It accordingly compares the parameter with Python keywords defined in the language and returns the output.

    Example:




    # Program to check whether a given
    # word is a Python keyword or not
      
      
    import keyword
      
      
    s ="if"
    t ="in"
    u ="GeeksforGeeks"
       
    # using iskeyword() function to check
    print(s, "is a keyword in Python:", 
          keyword.iskeyword(s))
      
    print("lambda is a keyword in Python:",
          keyword.iskeyword("lambda"))
      
    print("print is a keyword in Python:",
          keyword.iskeyword("print"))
      
    print(t, "is a keyword in Python:",
          keyword.iskeyword(t))
      
    print(u, "is a keyword in Python:", 
          keyword.iskeyword(u))

    
    

    Output:

    if is a keyword in Python: True
    lambda is a keyword in Python: True
    print is a keyword in Python: False
    in is a keyword in Python: True
    GeeksforGeeks is a keyword in Python: False

    As you can see from the above example that the value of variable s and t is a keyword in Python, thus the function returns True. Similarly, the string GeeksforGeeks is not a keyword, thus the function returns False.

  • keyword.kwlist
    This is a predefined variable of keyword module that stores all the keywords of Python. Thus it can be used to display all the keywords of Python just by calling it.

    Example:




    # Program to display the list of Python keywords
      
    # importing keyword module
    import keyword
      
    # using keyword.kwlist to display the list of keywords
      
    print(keyword.kwlist)
      

    
    

    Output:

    [‘and’, ‘as’, ‘assert’, ‘break’, ‘class’, ‘continue’, ‘def’, ‘del’, ‘elif’, ‘else’, ‘except’, ‘exec’, ‘finally’, ‘for’, ‘from’, ‘global’, ‘if’, ‘import’, ‘in’, ‘is’, ‘lambda’, ‘not’, ‘or’, ‘pass’, ‘print’, ‘raise’, ‘return’, ‘try’, ‘while’, ‘with’, ‘yield’]

    Note: keyword.kwlist is not a function, hence no parenthesis are used with it. kwlist is a variable defined previously in the keyword module.



Similar Reads

Python | Passing dictionary as keyword arguments
Many times while working with Python dictionaries, due to advent of OOP Paradigm, Modularity is focussed in different facets of programming. Hence there can be many use cases in which we require to pass a dictionary as argument to a function. But this required the unpacking of dictionary keys as arguments and it's values as argument values. Let's d
3 min read
Python assert keyword
Python Assertions in any programming language are the debugging tools that help in the smooth flow of code. Assertions are mainly assumptions that a programmer knows or always wants to be true and hence puts them in code so that failure of these doesn't allow the code to execute further. Assert Keyword in PythonIn simpler terms, we can say that ass
7 min read
finally keyword in Python
Prerequisites: Exception Handling, try and except in Python In programming, there may be some situation in which the current method ends up while handling some exceptions. But the method may require some additional steps before its termination, like closing a file or a network and so on. So, in order to handle these situations, Python provides a ke
3 min read
Use of nonlocal vs use of global keyword in Python
Prerequisites: Global and Local Variables in PythonBefore moving to nonlocal and global in Python. Let us consider some basic scenarios in nested functions. C/C++ Code def fun(): var1 = 10 def gun(): print(var1) var2 = var1 + 1 print(var2) gun() fun() Output: 10 11 The variable var1 has scope across entire fun(). It will be accessible from nested f
3 min read
is keyword in Python
In programming, a keyword is a “reserved word” by the language that conveys special meaning to the interpreter. It may be a command or a parameter. Keywords cannot be used as a variable name in the program snippet. Python language also reserves some of the keywords that convey special meaning. In Python, keywords are case-sensitive. Python is Keywo
2 min read
Python IMDbPY - Searching keyword
In this article we will see how we can search a keyword in the IMDb database.Keyword : It is a word (or group of connected words) attached to a title (movie / TV series / TV episode) to describe any notable object, concept, style or action that takes place during a title. The main purpose of keywords is to allow visitors to easily search and discov
1 min read
Python IMDbPY – Searching movies matching with keyword
In this article we will see how we can find movies that have similar keyword. Keyword is a word (or group of connected words) attached to a title (movie / TV series / TV episode) to describe any notable object, concept, style or action that takes place during a title. The main purpose of keywords is to allow visitors to easily search and discover t
1 min read
Python None Keyword
None is used to define a null value or Null object in Python. It is not the same as an empty string, a False, or a zero. It is a data type of the class NoneType object. None in Python Python None is the function returns when there are no return statements. C/C++ Code def check_return(): pass print(check_return()) Output: NoneNull Vs None in Python
2 min read
Python as Keyword
In this article, we are going to see 'as' Keyword. The keyword 'as' is used to create an alias in python. Advantages with 'as' keyword: It is useful where we cannot use the assignment operator such as in the import module.It makes code more understandable to humans.The keyword as is used to make alias with programmer selected name, It decreases the
2 min read
Python nonlocal Keyword
Python nonlocal keyword is used to reference a variable in the nearest scope. Python nonlocal Keyword Example In this example, we demonstrate the working of the nonlocal keyword. C/C++ Code def foo(): name = "geek" # Our local variable def bar(): nonlocal name # Reference name in the upper scope name = 'GeeksForGeeks' # Overwrite this var
3 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg