00:13 So far, we have grouped the keywords into two main categories: normal keywords and soft keywords. To grasp them better and have a closer look at them in this lesson, let’s categorize them even more.
01:05 All right. First up, there are the value keywords. There are three Python keywords that are used as values. These values are singleton values that can be used over and over again and always reference the exact same object.
You can use as many
elif statements as you need. The
else statement in conjunction with the Python keyword
elif denotes a block of code that should be executed only if the other conditional blocks,
elif, are all considered to be false.
Python code was designed for readability. That’s why many of the operators that use symbols in other programming languages are actual words in Python. We can group them as operator keywords. The Python keyword
and is used to determine if both the left and the right operands are considered true.
in keyword is a so-called containment check. It will return
False, indicating whether the element was found in the container or not. Containers can be anything that can be iterated over—lists, dictionaries, sets, strings, and actually anything else that defines a
is keyword is an identity check. Sometimes two things can be considered equal, but they are not the same object in memory. The
is keyword determines whether two objects are exactly the same object in memory. A common use case for
is is checking a variable against
The example on the right shows a number guessing game. And the first keyword here is
while. As long as the condition that follows the
while keyword is considered true, the block following the
while statement will continue to be executed over and over again,
In addition to using the
else keyword with conditional
if statements, you can also use it as part of a loop. When used with a loop, the
else keywords specifies code that should be run if the loop exits normally, meaning
break was not called to exit the loop early.
Let’s move on to structure keywords. Python’s keyword
def is used to define a function or method of a class. To define a class in Python, you use the
class keyword. Since Python doesn’t have block indicators to specify the end of a block, the
pass keyword is used to specify that the block is intentionally left blank. Using
with gives you a way to define code to be executed within the context manager’s scope.
A common use case is when you’re working with files in Python. Context managers are really helpful structure in Python. Each context manager executes specific code before and after the statements you specify, like opening and closing a file. To use one, you use the
The default behavior of the
sorted() function would sort the strings alphabetically. This example sorts the list based not on alphabetical order, but on the numerical order of the last characters of the strings after converting them to integers. Without
lambda, you would have to have to define a function, give it a name, and then pass it to the
There are two Python keywords that are used to specify what gets returned from functions or methods:
return keyword is valid only as part of a function defined with
There are three importing keywords in Python:
import keyword is used to import a module for use in your Python program. The
from keyword is used together with
import to import something specific from a module. The
as keyword is used to alias an imported module or tool.
08:27 One of the aspects of any Python program is raising and catching exceptions. Because this is such a fundamental aspect of all Python code, there are several Python keywords available to help make this part of your code clear and concise.
assert keyword in Python is used to specify an
assert statement or an assertion about an expression. Generally,
assert statements will be used to make sure something that needs to be true is true. You shouldn’t rely on them, however, as they can be ignored, depending on how your Python program is executed.
The code in the
try block is code that might raise an exception. Several other Python keywords are associated with
try and are used to define what should be done if different exceptions are raised or in different situations. These are
try block isn’t valid unless it has at least one of the other Python keywords used for exception handling as part of the overall
You can use
else in this way only if you also use at least one
except block. In this context, the code in the
else block is executed only if an exception was not raised in the
try block. In other words, if the
try block executed all the code successfully, then the
else block code would be executed. Python’s
finally keyword is helpful for specifying code that should be run no matter what happens in the
There are two Python keywords defined to help make asynchronous code more readable and cleaner:
async keyword is used with
def to define an asynchronous function or coroutine. The syntax is just like defining a function, with the addition of
async at the beginning.
nonlocal are keywords that are used to work with the scope of variables. If you need to modify a variable that isn’t defined in a function but is defined in the global scope, then you’ll need to use the
This works by specifying in the function which variables need to be pulled into the function from the global scope. The
nonlocal keyword is similar to
global in that it allows you to modify variables from a different scope. With
global, the scope you’re pulling from is the global scope. With
nonlocal, the scope you’re pulling from is the parent scope.
And finally, the soft keywords again. So while the other groups—I have to admit—are basically made up to group the keywords for you, the soft keywords are an official category, and you already met them before in a lesson. Also, as you learned before, their use is with pattern matching, and this is possible while still preserving compatibility with existing code that uses
_ (underscore) as identifier names.
case construct is new in Python 3.10. And this is how you perform structural pattern matching. You start with a
match statement that specifies what you want to match. One or several
case statements follow
12:52 Each case describes one pattern, and the indented block beneath it says what should happen if there is a match. You can use the underscore as a wild-card pattern that matches anything without binding it to a name.
13:09 So with that were quite a few keywords. I admit even with a cheat sheet, it can be quite hard to remember them all. But luckily there are ways of identifying keywords, and you will learn about those ways in the next lesson.
Become a Member to join the conversation.