Python Programming Language

Basics of Python

Python Keywords with Examples - Python Tutorials

Python Keywords

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 KeywordsDescriptions
TrueRepresents the truth value True.
FalseRepresents the truth value False.
NoneRepresents the absence of a value.
andReturns True if both operands are True, otherwise returns False.
orReturns True if either operand is True, otherwise returns False.
notInverts the truth value of the operand.
inUsed to check if a value is present in a sequence (e.g., list, tuple, string).
isUsed to check if two variables refer to the same object in memory.
ifIntroduces a conditional statement.
elifIntroduces an alternative conditional branch.
elseIntroduces the optional final branch of a conditional statement.
forIntroduces a loop that iterates over a sequence.
whileIntroduces a loop that continues as long as a condition is true.
breakUsed to exit a loop prematurely.
continueUsed to skip to the next iteration of a loop.
defUsed to define a function.
classUsed to define a class.
withUsed to simplify resource management.
asUsed to define an alias for a name.
passUsed as a placeholder statement when a statement is required syntactically but no action is needed.
lambdaUsed to define an anonymous function (a function without a name).
returnUsed to return a value from a function.
yieldUsed to define a generator function, which returns values one at a time.
importUsed to import modules into the current namespace.
fromUsed to import specific names from a module.
asUsed to define an alias for an imported name.
globalUsed to declare a variable as global.
delUsed to delete a variable or object.
tryIntroduces a try…except block for handling exceptions.
exceptIntroduces an exception handler.
finallyIntroduces a block of code that always executes, regardless of whether an exception is raised.
assertUsed 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.

Categories