Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Understanding Keywords

To learn more about the concepts covered in this lesson, you can check out History of the Python language (syntax).

00:00 In the last lesson, you got an overview of all the keywords. In this lesson, you will learn what keywords in Python actually are, and you will learn that there are normal keywords and soft keywords, and what the difference between them is.

00:16 Keywords in Python are special, reserved words that have a specific meaning and are meant for a specific purpose in Python. These keywords are always available.

00:28 That means you’ll never have to import them into your code. They are fundamental building blocks. They are essential for Python syntax, but they also make the Python programming language more human-readable.

00:42 They are special, reserved words that can’t be used for anything but those specific purposes that they are meant for. And this makes them quite restrictive in their usage.

00:54 Now you might wonder why the last two bullets don’t have the checkmark on our friendly green background. Well, I did this on purpose to foreshadow that these are key differences to soft keywords.

01:09 Soft keywords are also always available, and they are also fundamental building blocks, but they are not special, reserved words, and therefore not as restrictive in their usage.

01:22 Let me show you what I mean by that.

01:26 Python keywords are always available. That means you don’t need to import them to use them, like pass, None, True, which returns True, and False, which returns False.

01:38 But they are restrictive in their usage. That means what just worked with pass, None, True, and False doesn’t work, for example, with yield. This one raises a SyntaxError.

01:52 Same case if I just type and. This one also raises a SyntaxError. You will learn more about how you actually use them correctly as building blocks in your Python code in the next lesson. For now, just remember this SyntaxError that’s raised.

02:12 Python keywords are special, reserved words. That means you can’t just use them as variable names. If you type pass = "hello", you will also get a SyntaxError because pass is a keyword and you can’t use it like that.

02:29 Let’s move to soft keywords and see how they behave differently. If I just type match, I will also get an error. So that’s not that different to some of the keywords I just typed above, but this error is different. So it’s not a SyntaxError, which would mean I used this keyword wrong. But it’s a NameError, which means the variable match isn’t known to Python yet.

02:54 Now this error is an indicator that you can actually use match and case like normal variables. You can say match = True or case = "wallet", and this is proper Python code.

03:07 You won’t get a SyntaxError, and you are allowed to do this. However, besides using them as normal variable names, you are allowed to use them in a special way. Now with Python 3.10, you can use match, case, and _ (underscore) for the structural pattern matching.

03:26 Let me show you. If I create a function called greet(), greet() takes a name as an argument, and I have a match pattern in it that says match name:

03:39 case "Tappan": print("Hi, Tappan!"). Or in any other case,

03:50 print("Howdy, stranger!"). Then I can use match, case, and _ (underscore) like normal Python keywords without importing them. That’s why they are always available as well.

04:05 So what you’re seeing here, that’s what is meant with the word soft. You can use them as normal variables, but they are also fundamental building blocks in your Python code. For example, structural pattern matching. Before we move on, let me quickly prove that this function works. Let’s call it.

04:27 greet("Tappan") prints Hi, Tappan!

04:32 The reason why you’re now having soft keywords as well is because keywords in general can change over time. So before Python 3, there were already a bunch of keywords. And with Python 3.0, there were a few changes introduced.

04:49 So maybe if you have programmed with Python 2.7, for example, you remember that if you upgrade to Python 3, you had to change your print statements into print() functions.

05:02 So print and exec are not keywords anymore. Python 3.5 introduced async and await. And with Python 3.10, match, case, and _ (underscore) were introduced.

05:16 However, not as keywords but as soft keywords. That means if you have an existing codebase, and you use variable names like match or case, you don’t have to change your codebase just to work with Python 3.10.

05:31 However, to use this structural pattern matching, you have to use match and case in a special way.

05:40 So far, we have generally grouped keywords into normal keywords and soft keywords, but it makes sense to categorize them even more. We will do this in the next lesson.

Become a Member to join the conversation.