Python Keywords
Python keywords are special reserved words in Python source code that have specific meanings and purposes and can’t be used for anything but those specific purposes.
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.
-
and
A logical operator used to combine two expressions and return a truthy value if both expressions are true. -
as
Creates an alias or alternative name for a module, exception, or context manager. -
assert
Lets you create an assertion or sanity check to test whether a given condition is true. -
async
Lets you define asynchronous functions, loops, context managers, generators, and iterators, which perform non-blocking operations. -
await
Pauses the execution of a coroutine until the result is available, allowing other tasks to run concurrently. -
break
Exits a loop immediatelly, and Python continues executing the code that follows the loop. -
class
Lets you create user-defined classes, which are the cornerstone of object-oriented programming (OOP). -
continue
Lets you skip the rest of the code inside a loop for the current iteration and jump to the next iteration. -
def
Defines a function, which is a reusable block of code that may accept data input, perform a specific computation or task, and return a result or produce a side effect. -
del
Deletes names from a given scope or namespace. -
elif
Defines a branch in a conditional statement to check alternative expressions for truth value. -
else
Specifies a block of code to be executed when a certain condition is false in control flow statements such as if, while, and for. -
except
Catches and handles exceptions that occur during the execution of a program. -
False
A built-in constant that represents the Boolean value for false. -
finally
Is used in a try statement to define a block of code that will always execute, regardless of whether an exception was raised. -
for
Creates a loop that iterates over an iteratble, such as a list, tuple, string, or range. It also defines comprehensions and generator expressions. -
from
Is primarily used to import specific objects from a module into your current namespace. -
global
Lets 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. -
if
Starts a conditional statement that allows you to execute a code block only if a specified condition is true. -
import
Allows you to include modules in your program and use their functionalities directly in your code. -
in
Is used for membership tests, for loops, comprehensions, and generator expressions. -
is
An operator that lets you test for object identity by checking whether two variables point to the same object in memory. -
lambda
Creates anonymous functions, also known as lambda functions, which can take any number of arguments and whose body must be a single expression. -
None
A built-in constant that represents the absence of a value or a null value. -
nonlocal
Lets you declare a variable in a nested function as not local to that function. -
not
A logical operator that inverts the truth value of the expression returning True or False. -
or
A logical operator that evaluates two expressions and returns True if at least one of the expressions is true. -
pass
A placeholder statement that allows you to write syntactically correct code without executing any operations. -
raise
Allows you to trigger exceptions manually when a particular error arises in your code. -
return
Exits a function and returns a value back to the caller. -
True
A built-in constant that represents the Boolean value for true. -
try
Defines a block of code that you want to attempt to execute while also providing a way to handle exceptions that may occur during its execution. -
while
Defines a loop that executes a block of code as long as a specified condition remains true. -
with
Wraps the execution of a block of code within methods defined by a context manager. -
yield
Turns a function into a generator function, which returns an iterator that generates values on demand rather than generating them all at once.