Python keywords are special reserved words in Python source code that have specific meanings and purposes and can’t be used for anything else.
Each keyword serves a specific purpose in Python’s syntax, and together they form the foundation of the language’s grammar. Understanding these keywords is crucial for writing effective Python code.
These keywords are always available in your source code—you’ll never have to import them. Python keywords are different from Python’s built-in functions and types. The built-in functions and types are also always available, but they aren’t as restrictive as the keywords in their usage.
andA logical operator used to combine two expressions and return a truthy value if both expressions are true.
asCreates an alias or alternative name for a module, exception, or context manager.
assertLets you create an assertion or sanity check to test whether a given condition is true.
asyncLets you define asynchronous functions, loops, context managers, generators, and iterators, which perform non-blocking operations.
awaitPauses the execution of a coroutine until the result is available, allowing other tasks to run concurrently.
breakExits a loop immediately, and Python continues executing the code that follows the loop.
caseA soft keyword that’s used in conjunction with the match statement to perform pattern matching.
classLets you create user-defined classes, which are the cornerstone of object-oriented programming (OOP).
continueLets you skip the rest of the code inside a loop for the current iteration and jump to the next iteration.
defDefines a function, which is a reusable block of code that can accept data input, perform a specific computation or task, and return a result or produce a side effect.
elifDefines a branch in a conditional statement to check alternative expressions for truth value.
elseSpecifies a block of code to be executed when a certain condition is false in control flow statements such as if, while, and for.
exceptCatches and handles exceptions that occur during the execution of a program.
FalseA built-in constant that represents the Boolean value for false.
finallyIs used in a try statement to define a block of code that will always execute, regardless of whether an exception was raised.
forCreates a loop that iterates over an iteratble, such as a list, tuple, string, or range. It also defines comprehensions and generator expressions.
fromIs primarily used to import specific objects from a module into your current namespace.
globalLets you declare that a variable used inside a function is global, which allows you to modify the variable defined outside of the current function scope.
ifStarts a conditional statement that allows you to execute a code block only if a specified condition is true.
importAllows you to include modules in your program and use their functionalities directly in your code.
inUsed for membership tests, for loops, comprehensions, and generator expressions.
isAn operator that lets you test for object identity by checking whether two variables point to the same object in memory.
lambdaCreates anonymous functions, also known as lambda functions, which can take any number of arguments and whose body must be a single expression.
matchA soft keyword that’s used to perform structural pattern matching.
NoneA built-in constant that represents the absence of a value or a null value.
nonlocalLets you declare a variable in a nested function as not local to that function.
notA logical operator that inverts the truth value of the expression returning True or False.
orA logical operator that evaluates two expressions and returns True if at least one of the expressions is true.
passA placeholder statement that allows you to write syntactically correct code without executing any operations.
raiseAllows you to trigger exceptions manually when a particular error arises in your code.
returnExits a function and returns a value back to the caller.
TrueA built-in constant that represents the Boolean value for true.
tryDefines a block of code you want to run, along with a way to manage exceptions that might occur during its execution.
typeA soft keyword used to explicitly define generic type aliases in the context of type hints or type annotations.
underscore (_)A soft keyword that acts as a wildcard, useful when you want to handle all unmatched cases in a match statement.
whileDefines a loop that executes a block of code as long as a specified condition remains true.
withWraps the execution of a block of code within methods defined by a context manager.
yieldThe yield keyword turns a function into a generator, returning an iterator that produces values on demand instead of computing them all at once.