Data Structure
 Networking
 RDBMS
 Operating System
 Java
 MS Excel
 iOS
 HTML
 CSS
 Android
 Python
 C Programming
 C++
 C#
 MongoDB
 MySQL
 Javascript
 PHP
- Selected Reading
 - UPSC IAS Exams Notes
 - Developer's Best Practices
 - Questions and Answers
 - Effective Resume Writing
 - HR Interview Questions
 - Computer Glossary
 - Who is Who
 
List of Keywords in Python Programming
Keywords in Python are reserved words. You cannot use them as variable name, function name, class name, etc.
Following are the Keywords in Python ?
| Keywords in Python | ||||
|---|---|---|---|---|
| FALSE | await | else | import | pass | 
| None | break | except | in | raise | 
| TRUE | class | finally | is | return | 
| and | continue | for | lambda | try | 
| as | def | from | nonlocal | while | 
| assert | del | global | not | with | 
| async | elif | if | or | yield | 
The keyword module is to be used for getting all the keywords. At first, learn to install the keyword module.
Install the keyword module
To install the keyword module, use the pip ?
pip install keyword
Fetch all the keywords in Python
Use the kwlist attribute to fetch all the keywords after importing the keyword module. Let's see the example ?
Example
import keyword # Fetch all the Keywords kwlist = keyword.kwlist # Display the Keywords print("Keywords = ",kwlist)
Output
Keywords = ['False', 'None', 'True', 'and', 'as', 'assert', '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']
Check for valid keywords in Python
We will check multiple values for valid and invalid keywords in Python. With that, use the iskeyword() method for checking ?
Example
import keyword # Create a List myList = ["for", "amit", "val", "while"] # Display the List print("List = ",myList) keyword_list = [] non_keyword_list = [] # Looping and verifying for keywords for item in myList: if keyword.iskeyword(item): keyword_list.append(item) else: non_keyword_list.append(item) print("\nKeywords= " + str(keyword_list)) print("Non-Keywords= " + str(non_keyword_list))
Output
List = ['for', 'amit', 'val', 'while'] Keywords= ['for', 'while'] Non-Keywords= ['amit', 'val']
Advertisements