Python -Keywords,Operators
Keywords in Python Keywords are special reserved words that are part of the language itself. They define the rules and structure of Python programs which means you cannot use them as names for your variables, functions
Keywords in Python
Keywords are special reserved words that are part of the language itself. They define the rules and structure of Python programs which means you cannot use them as names for your variables, functions, classes or any other identifier.
Getting List of all Python keywords
import keyword
print("The list of keywords are : ")
print(keyword.kwlist)
The list of keywords are:
['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']
Keyword-Description
and --> A logical operator
as --> To create an alias
assert --> For debugging
async --> Define an asynchronous function
await --> Wait for and get a result from an awaitable
break --> To break out of a loop
case --> Pattern in a match statement
class --> To define a class
continue --> To continue to the next iteration of a loop
def --> To define a function
del --> To delete an object
elif --> Used in conditional statements, same as else if
else --> Used in conditional statements
except -->Used with exceptions, what to do when an exception occurs
False --> Boolean value, result of comparison operations
finally --> Used with exceptions, a block of code that will be executed no matter if there is an exception or not
for --> To create a for loop
from --> To import specific parts of a module
global--> To declare a global variable
if -->To make a conditional statement
import --> To import a module
in --> To check if a value is present in a list, tuple, etc.
is --> To test if two variables are equal
lambda -->To create an anonymous function
match --> Start a match statement (compare a value against cases)
None --> Represents a null value
nonlocal--> To declare a non-local variable
not --> A logical operator
or --> A logical operator
pass --> A null statement, a statement that will do nothing
raise --> To raise an exception
return --> To exit a function and return a value
True --> Boolean value, result of comparison operations
try --> To make a try...except statement
while --> To create a while loop
with --> Used to simplify exception handling
yield -->To return a list of values from a generator
Identify Python Keywords
Ways to identify Keywords are:
- With Syntax Highlighting: Most of IDEs provide syntax-highlight feature. You can see Keywords appearing in different color or style.
- Look for SyntaxError: This error will encounter if you have used any keyword incorrectly. Keywords can not be used as identifiers like variable or a function name.
Keywords as Variable Names
If we attempt to use a keyword as a variable, Python will raise a SyntaxError.
Let's look at an example:
for = 10
print(for)
Output :
Let's categorize all keywords based on context for a more clear understanding
** Keywords vs Identifiers**
Variables vs Keywords
Reference :
Python Keywords - GeeksforGeeks
Your All-in-One Learning Portal: GeeksforGeeks is a comprehensive educational platform that empowers learners across domains-spanning computer science and programming, school education, upskilling, commerce, software tools, competitive exams, and more.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.


