Python Keywords: Complete List with Examples

Introduction

When learning Python, one of the fundamental concepts you need to understand is keywords. Keywords are reserved words that have special meanings in Python and are used to define the language’s syntax and structure. These words are predefined by Python and cannot be used as variable names, function names, class names, or identifiers.

For example, words such as if, else, for, while, and class are Python keywords. When the Python interpreter encounters these words, it treats them as special instructions rather than ordinary text.

What are Python Keywords?

Python keywords are reserved words that have predefined meanings in the Python language.

These words are part of Python’s syntax and cannot be used as identifiers.

For example:


if
for
while
class
return

Each keyword performs a specific function within a Python program.

For instance:

  • if is used for decision-making.
  • for is used for looping.
  • class is used for creating classes.
  • return is used to return values from functions.

Since keywords are reserved by Python, they cannot be used as variable names.

Incorrect Example:


if = 10

Output:

SyntaxError

Python generates an error because if is a reserved keyword.

Why are Keywords Important?

Keywords form the foundation of Python programming.

Benefits of Keywords:

  • Define program structure
  • Control execution flow
  • Create functions and classes
  • Handle exceptions
  • Improve code readability
  • Ensure consistent syntax

Note: Without keywords, Python programs would not be able to perform logical operations or follow structured programming principles.

How to View Python Keywords

Python provides the keyword module to display all available keywords.

Example:


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’]

The exact list may vary slightly depending on the Python version.

Syntax of Keywords

Keywords are used directly as part of Python statements.

Example:


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

Here:

  • if is a keyword.
  • print() is a function.
  • age is a variable.

Keywords define the structure of the statement.

Categories of Python Keywords

Python keywords can be grouped into several categories.

1. Boolean Keywords

Boolean keywords represent truth values.

True:


is_logged_in = True

print(is_logged_in)

Output:

True

False:


is_admin = False

print(is_admin)

Output:

False

2. Conditional Keywords

These keywords help make decisions.

if


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

Output:

Adult

else


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

Output:

Minor

elif


marks = 75
if marks >= 90:
    print("A")
elif marks >= 60:
    print("B")
else:
    print("C")

Output:

B

3. Loop Keywords

for


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

Output:

0
1
2

while


count = 1
while count <= 3:
    print(count)
    count += 1

Output:

1
2
3

break


for i in range(5):
    if i == 3:
        break
    print(i)

Output:

0
1
2

continue


for i in range(5):
    if i == 2:
        continue
    print(i)

Output:

0
1
3
4

4. Function Keywords

def

Used to create functions.


def greet():
    print("Hello")

return

Returns a value from a function.


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

Output:

15

lambda

Creates anonymous functions.


square = lambda x: x * x
print(square(5))

Output:

25

5. Class and Object Keywords

class

Used to create classes.


class Student:
    pass

pass

Acts as a placeholder.


class Student:
    pass

No error occurs even though the class body is empty.

6. Exception Handling Keywords

try


try:
    print(10 / 0)
except:
    print("Error")

Output:

Error

except

Handles exceptions.


try:
    num = int("abc")
except ValueError:
    print("Invalid Number")

finally

Always executes.


try:
    print("Try Block")
finally:
    print("Finally Block")

Output:

Try Block
Finally Block

raise

Generates exceptions manually.


raise ValueError("Invalid Value")

7. Import Keywords

import


import math
print(math.sqrt(25))

Output:

5.0

from


from math import sqrt
print(sqrt(16))

Output:

4.0

as


import math as m
print(m.sqrt(36))

Output:

6.0

8. Membership Keywords

in


fruits = ["Apple", "Banana"]
print("Apple" in fruits)

Output:

True

not in


fruits = ["Apple", "Banana"]
print("Mango" not in fruits)

Output:

True

9. Logical Keywords

and


age = 20
print(age > 18 and age < 30)

Output:

True

or


print(True or False)

Output:

True

not


print(not True)

Output:

False

Real-Life Example

Suppose you are building an employee management system.


employee_age = 25
if employee_age >= 18:
    print("Eligible for employment")
else:
    print("Not eligible")

Output:

Eligible for employment

Keywords used:

  • if
  • else

These keywords help control decision-making in real-world applications.

Common Mistakes

1. Using Keywords as Variable Names

Incorrect:


class = "Student"

Output:

SyntaxError

Correct:


student_class = "Student"

2. Incorrect Keyword Spelling

Incorrect:


iff age > 18:
    print("Adult")

Correct:


if age > 18:
    print("Adult")

3. Wrong Capitalization

Incorrect:


If age > 18:

Correct:


if age > 18:

Keywords are case-sensitive.

4. Missing Colon

Incorrect:


if age > 18
    print("Adult")

Correct:


if age > 18:
    print("Adult")

5. Misusing pass

Incorrect:


class Student:

Correct:


class Student:
    pass

Conclusion

Python keywords are reserved words that form the backbone of the Python language. They help define program structure, control flow, create functions and classes, handle exceptions, and perform logical operations. Since keywords have predefined meanings, they cannot be used as identifiers such as variable names or function names.

Related Python Tutorials