Identifying Keywords
00:00 In the last lesson, you categorized the keywords into groups, and we went through the groups one by one. In this lesson, I will show you how to identify Python keywords in your code.
00:15 The first way of identifying keywords in your code is using a code editor that supports syntax highlighting. With syntax highlighting, words in your code are colored or even formatted differently if they are somehow special. And what could be more special than Python keywords?
00:36
So here you see three examples you already saw in the former lesson, and the keywords—now you might remember some of them—are all in pink. So by just looking at this screen, you can easily spot that def
, assert
, try
, except
, raise
, else
, finally
, return
, with
, as
, and lambda
down there are Python keywords.
01:04
Another way of identifying keywords is just trying to use them and see if you get any syntax errors. So if you try to assign the string "1234"
to a variable named pass
, Python will throw a SyntaxError
.
01:20
You already saw this in one of the former lessons why this is. Same if I just type while
. You will also get a SyntaxError
. If this wouldn’t be keywords, you would get a NameError
that the variables are undefined, or it would just work.
01:36
But this is kind of like a harsh way of finding out what a Python keyboard is, or if a certain word is a Python keyword. Okay, I know I said that I will show two ways, but if you remember at the beginning of this course, I also showed you that you could import the keyword
module to list all keywords, so basically this is a way of finding out what the Python keywords are as well.
02:01
And the other thing is just to use the help()
function. So if you type help("def")
in this case, then you will get information about the def
keyword.
02:13
Same goes if you type something that isn’t a Python keyword or anything that Python knows of. Then Python will let you know that there is no Python documentation found for this word—in this example, "undefined"
.
02:28 So that’s basically like a third way of finding out if something is a keyword or not.
02:37 And then there are words that were a keyword, but are not anymore, and you will learn about them in the next lesson.
Become a Member to join the conversation.