Table of Contents » Reference Materials : Python Keywords
Python Keywords
Python keywords are reserved words that have a specific, predefined meaning in the Python programming language. These words cannot be used as identifiers, such as names for variables, functions, or classes, because they are already used by the language syntax for defining the structure and control flow of the code. Examples of Python keywords include if, else, for, while, break, continue, def, return, class, try, except, import, from, as, with, True, False, and None. Each of these keywords serves a unique purpose in Python, ranging from creating conditional statements, loops, functions, classes, to handling exceptions and importing modules. The number and nature of keywords can vary slightly between different versions of Python, but they are all integral to the language's structure. Understanding and correctly using these keywords is essential for writing syntactically correct and efficient Python code.
Keyword | Description | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
and | A logical operator | |||||||||||
as | To create an alias | |||||||||||
assert | For debugging | |||||||||||
break | To break out of a loop | |||||||||||
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 | 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 | |||||||||||
None | Represents a null value | |||||||||||
nonlocal | declare a non-local variable | |||||||||||
not | logical operator | |||||||||||
or | logical operator | |||||||||||
pass | null statement, a statement that will do nothing | |||||||||||
return | To exit a function and return a value | |||||||||||
raise | To raise an exception | True | 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 end a function, returns a generator | |