Keywords in Python: Definition, Examples, and Uses

← PrevNext →

While learning Python, it is important to understand keywords, as they play a key role in writing and organizing programs. In this tutorial, we will explore what Python keywords are, their types, common examples, and how to check the complete list of reserved words.

What Are Keywords in Python?

Keywords are special reserved words in Python that have predefined meanings. They are used to define the structure and behavior of a Python program.

Python keywords have specific purposes, such as creating conditional statements, defining loops, handling functions, and managing exceptions.

Since keywords are reserved by Python, they cannot be used as variable names, function names, or other identifiers.

For example:

if age >= 18:
    print("You are eligible")

Here, if is a keyword used to create a conditional statement.

List of Python Keywords

Python provides several keywords for different programming operations. The exact list can vary between Python versions.

Some commonly used Python keywords include:

Keyword Purpose
ifChecks a condition
elseExecutes an alternative block
elifChecks another condition
forCreates a loop
whileCreates a loop
breakTerminates a loop
continueSkips the current iteration
defDefines a function
returnReturns a value
classDefines a class
TrueRepresents a Boolean true value
FalseRepresents a Boolean false value
NoneRepresents the absence of a value
importImports a module
fromImports specific objects
tryStarts exception handling
exceptHandles an exception
finallyExecutes cleanup code
passRepresents an empty statement
andLogical AND operator
orLogical OR operator
notLogical NOT operator
inChecks membership
isChecks object identity

How to Get the List of Keywords in Python

Python provides a built-in module called keyword that allows you to view all reserved keywords.

Example:

import keyword
print(keyword.kwlist)

This program displays the list of keywords supported by your installed Python version.

You can also check whether a particular word is a keyword using iskeyword().

Example:

import keyword

print(keyword.iskeyword("if"))
print(keyword.iskeyword("name"))

Output:

True
False

Here, if is a reserved keyword, whereas name is not.

Categories of Python Keywords

Let's understand some important categories of keywords.

1. Conditional Keywords

Conditional keywords are used to make decisions in a program.

Examples: if, elif, and else.

age = 20

if age >= 18:
    print("Adult")
else:
    print("Minor")

2. Looping Keywords

Looping keywords are used to control repetitive operations.

Examples: for, while, break, and continue.

for i in range(5):
    print(i)

3. Function Keywords

The def keyword is used to define a function, while return is used to send a result back from it.

def add(a, b):
    return a + b
    print(add(10, 20))

Output:

30

4. Logical Keywords

Logical keywords are used to combine or modify conditions.

Examples: and, or, and not.

age = 25

if age >= 18 and age <= 60:
    print("Eligible")

5. Exception Handling Keywords

These keywords help handle errors during program execution.

Examples: try, except, finally, and raise.

try:
    print(10 / 0)
except ZeroDivisionError:
    print("Cannot divide by zero")

Difference Between Keywords and Identifiers

Keywords and identifiers are different concepts in Python. Let's understand their differences:

Keywords Identifiers
Reserved words in Python Names created by programmers
Have predefined meanings Used to name variables, functions, and classes
Cannot be used as identifiers Must follow naming rules
Example: if, for, while Example: name, age, total

Conclusion

Python keywords are reserved words that help define how a program works. They are used in conditions, loops, functions, classes, and exception handling.

Understanding keywords is important for writing correct Python programs. You can use the keyword module to explore the reserved words available in your Python version.

← PreviousNext →