What are Python keywords?
Python keywords are unique words reserved with defined meaning and function that we can only apply for those functions. You’ll never need to import any keywords into your program because they’re permanently present.
Python has several keywords that are reserved. It means those keywords cannot be used as variable names, function names, or any other identifier.
Reserved keyword means those words are reserved and we can’t use them for any other purpose in our code. For example, class is a reserved keyword in Python. If we try to use class as a variable name, we will get an error:
Code:
class = 'Python'
print(class)
Output:
SyntaxError: invalid syntax
So we get SyntaxError, which means we are not following the rules for writing Python code. So it’s simple: we can’t use the reserved keywords for any other purpose in our code.
How Many Keywords In Python?
Python contains 35 keywords in the most recent Python version (3.13). To see all the keywords in Python, we can use help() function in Python.
Code:
help("keywords")
Output:
Here is a list of the Python keywords. Enter any keyword to get more help.
False class from or
None continue global pass
True def if raise
and del import return
as elif in try
assert else is while
async except lambda with
await finally nonlocal yield
break for not
So this is how we can see the list of keywords in Python. These are the 35 keywords. The number of keywords can be different in different Python versions.
Note: the list of Python keywords changed with time and version. For example, await and async keywords were added in the Python 3.7 version. Also, print and exec were keywords in Python 2.7, but they turned into built-in functions in Python 3.
Python is a case-sensitive language. In all 35 keywords, only three start with uppercase (False, None, True) and all other keywords are in lowercase. So you should remember this. For example, def is a reserved keyword but Def is nothing in Python.
We can also use help() with a specified keyword for more information on that keyword. For example:
Code:
help('def')
So this is how we can get documented information on def keyword.
Difference Between Keywords And Built-in Function
Python keywords are always available; we don’t need to import them like modules or functions. Python keywords are different from Python’s built-in functions. Built-in functions are also available, but they aren’t as restrictive as the keywords in their usage.
For example, we cannot assign Python keywords as variables. In the upper example, when we try to assign class as a variable, we get an error. But we can assign a built-in function as a variable. For example, type()
is a built-in function in Python but we can assign type as a variable.
Code:
type = "built-in function"
print(type)
Output:
built-in function
We get results without error but as recommended by Python, it’s also not good practice to use the built-in function name as a variable.
How to Identify Keywords in Python?
In Python, we can import modules. In Python, modules contain codes that we can use in our program. Keyword is a module in Python, which we can import in Python.
Code:
import keyword
print(keyword.kwlist)
Output:
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
So this is how we can import modules and see the list of keywords. We can use len()
function with the keyword module.
Code:
import keyword
print(len(keyword.kwlist))
Output:
35
We get 35 in the output. Now if we are not sure that particular word is a keyword or not. We can check. For example, if we want to check if 'class'
is a keyword or not.
Code:
import keyword
print(keyword.iskeyword('class'))
Output:
True
We get True in the result because class is a reserved keyword in Python. Now if we check 'type'
, we already know type is a built-in function.
Code:
import keyword
print(keyword.iskeyword('type'))
Output:
False
We get False in the output. So this is how we can check if any word is a reserved keyword or not.
End of Python Keyword Part 1
4 thoughts on “Python Keywords (Part 1): Keywords in Python? Keywords vs. Built-in Function?”