Python Keywords
Table of Contents
In the last lesson, we learned how to write Python comments. Now let’s learn about the special words Python keeps for itself.
After learning Python Comments, we learned how comments help humans understand code without changing what Python runs. Now let’s learn about the special words Python keeps for itself.
Let’s Talk About Special Words
Every language has some special words. In English, words like “the” and “is” are special. We cannot name our dog “the.” We cannot name our cat “is.” These words have a job. They do something in a sentence.
Python is the same. It has special words called keywords.
These words have meaning in Python. We cannot use them for anything else. We cannot use them as variable names. We cannot use them as function names. They belong to Python. Not to us.
How Many Keywords Does Python Have?
Python 3 has 35 keywords. That sounds like a lot. But do not worry. We do not need to memorize all of them. We will learn them naturally as we go. Just like we learned English words without sitting down to memorize a list.
The List of Python Keywords
Here they are, in alphabetical order:
False await else import passNone break except in raiseTrue class finally is returnand continue for lambda tryas def from nonlocal whileassert del global not withasync elif if or yieldThat is a lot. But look at some of them. We already know what if means. We know what True and False mean. We know what and and or and not mean. So it is not as scary as it looks.
Let Me Explain a Few Common Python Keywords
True and False
These are boolean values. They mean exactly what they say.
is_raining = Trueis_sunny = FalseTrue means yes. False means no. Simple.
None
None means “nothing” or “no value.”
result = NoneWe will use this a lot later. It is like saying — “there is no answer yet.”
if, elif, else
These are for making decisions.
if age > 18: print("Adult")elif age > 12: print("Teenager")else: print("Child")If the first thing is true, do this. Otherwise, check the next thing. Otherwise, do that. We will learn more about these later.
for and while
These are loops. They repeat things.
for i in range(5): print(i)This prints 0, 1, 2, 3, 4. One at a time. Automatically.
def
This is how we create a function.
def greet(): print("Hello")We will learn about functions later. For now, let’s just know that def means “define a function.”
return
This sends a value back from a function.
def add(a, b): return a + bThe return sends the answer back to whoever asked.
import
This brings in code from other files or libraries.
import mathNow we can use all the math functions that come with Python.
and, or, not
These are logical operators.
if age > 18 and has_id: print("You can enter")Both things must be true. That is what and means.
try, except
These are for handling errors.
try: x = 10 / 0except: print("Something went wrong")If something breaks, do not crash. Just handle it. That is what try and except do.
How to See All Python Keywords
Yes. We open our Python shell and type:
import keywordprint(keyword.kwlist)We will see the full list. All 35 of them.
Can We Use a Keyword as a Variable Name?
No. If we try, Python will give us an error.
if = 5 # ErrorPython will say:
SyntaxError: invalid syntaxBecause if is a keyword. It belongs to Python. We cannot use it as our own name.
But Wait — Can We Use a Keyword as Part of a Name?
Yes. We can use a keyword as part of a longer name.
if_condition = True # This is finemy_class = "Math" # This is fineSee? if_condition is okay because it is not exactly if. It just contains it. Same with my_class. It is not exactly class. It just contains it. So we have some flexibility. Just do not use the exact keyword.
Python Keywords: A Real-Life Example
Think of keywords like reserved seats on a train. There are some seats that are always reserved. We cannot sit there. They belong to someone else. But we can sit in the seat next to them. Or the seat behind them. Just not the reserved one.
Python keywords are like that. They are reserved. They belong to Python. But we can use similar names.
A Quick Tip About Python Keywords
Let’s not try to memorize the keyword list. Just code. When we accidentally use a keyword, Python will tell us. It will give us an error. Then we fix it. That is how we learn. By doing. By making mistakes. By fixing them.
I have been coding for years. And I still sometimes forget that a word is a keyword. Then Python reminds me. And I change it. It is normal. Let’s not stress about it.
What If We Really Want to See the Keywords?
We can always check. Just type this in our Python shell:
import keywordprint(keyword.kwlist)Or we can search online for “Python keywords list.” We will find it. But honestly, we do not need to. We will learn them as we go.
Let Me Show a Few Keywords in Action
Here is a small program that uses several keywords:
# This program checks if a number is positive or negative
number = 10
if number > 0: print("The number is positive")elif number < 0: print("The number is negative")else: print("The number is zero")Keywords used here: if, elif, else. Simple, right?
Here is another one:
# This program adds two numbers
def add(a, b): return a + b
result = add(5, 3)print(result)Keywords used here: def, return. See? They are not scary. They are just words that do specific jobs.
Next, we will learn about Python Variables so we can start storing and reusing data in our programs.