Python Keywords
Published Apr 23, 2022
Contribute to Docs
Certain keywords are reserved in Python and are not available to be assigned as variables or named functions. There are many keywords where some specialize in control-flow (e.g. if/else and for) while others work as operators (e.g. and, in, and is).
Keywords
- assert
- Confirms the truthiness of a given statement.
- async
- Allows users to define an asynchronous function in Python.
- await
- Pauses the execution of an asynchronous function until the awaited coroutine, task, or awaitable object finishes.
- break
- Immediately exits the nearest enclosing `for` or `while` loop.
- class
- Used for defining classes in Python.
- continue
- Skips the rest of the current loop iteration and moves to the next one immediately.
- def
- Allows users to define a function.
- del
- Removes an object from the namespace of a Python shell or environment.
- except
- Catches and handles exceptions raised in the try block.
- finally
- Defines a block of code that always runs after a `try` block, regardless of whether an exception was raised.
- from
- Used to import specific attributes, classes, or functions from a Python module.
- global
- Creates a global variable that can be updated.
- lambda
- Produces an anonymous function expression.
- nonlocal
- Declares a variable inside a nested function that refers to a variable in the nearest enclosing scope (not global).
- pass
- Acts as a placeholder in Python code, allowing empty code blocks to run without causing errors.
- return
- Ends a function and sends a value back to the caller.
- try
- Attempts to execute a block of code and lets the program handle exceptions gracefully if errors occur.
- with
- Simplifies resource management by automatically handling setup and teardown actions using context managers.
- yield
- Turns a function into a generator, producing values one at a time while retaining state between calls.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours