Python has a set of reserved words that have special meanings called “Python keywords.” These keywords cannot be used as variable names, function names, or any other identifiers.
Understanding what these keywords do is important for effectively writing Python programs. For more on Python’s powerful features like dynamic typing and high-level data types, see my blog post on Python Features.
List of Python Keywords
Here is a list of Python keywords with short descriptions of their functionality:
Python Keywords | Descriptions |
---|---|
True | Represents the truth value True. |
False | Represents the truth value False. |
None | Represents the absence of a value. |
and | Returns True if both operands are True, otherwise returns False. |
or | Returns True if either operand is True, otherwise returns False. |
not | Inverts the truth value of the operand. |
in | Used to check if a value is present in a sequence (e.g., list, tuple, string). |
is | Used to check if two variables refer to the same object in memory. |
if | Introduces a conditional statement. |
elif | Introduces an alternative conditional branch. |
else | Introduces the optional final branch of a conditional statement. |
for | Introduces a loop that iterates over a sequence. |
while | Introduces a loop that continues as long as a condition is true. |
break | Used to exit a loop prematurely. |
continue | Used to skip to the next iteration of a loop. |
def | Used to define a function. |
class | Used to define a class. |
with | Used to simplify resource management. |
as | Used to define an alias for a name. |
pass | Used as a placeholder statement when a statement is required syntactically but no action is needed. |
lambda | Used to define an anonymous function (a function without a name). |
return | Used to return a value from a function. |
yield | Used to define a generator function, which returns values one at a time. |
import | Used to import modules into the current namespace. |
from | Used to import specific names from a module. |
as | Used to define an alias for an imported name. |
global | Used to declare a variable as global. |
del | Used to delete a variable or object. |
try | Introduces a try…except block for handling exceptions. |
except | Introduces an exception handler. |
finally | Introduces a block of code that always executes, regardless of whether an exception is raised. |
assert | Used to check a condition, and raise an AssertionError if the condition is false. |
These Python keywords play an important role in how Python programs function. Understanding what each one does will help in writing Python code that executes properly.