Skip to content

Reserved Keywords

View Keywords

1. List All

import keyword

# Get all keywords
print(keyword.kwlist)

# Count
print(len(keyword.kwlist))

2. Check Keyword

import keyword

print(keyword.iskeyword('class'))   # True
print(keyword.iskeyword('myvar'))   # False

Control Flow

1. Conditionals

# if, elif, else
if x > 0:
    pass
elif x == 0:
    pass
else:
    pass

# Cannot use
# if = 5      # SyntaxError

2. Loops

# for, while, break, continue
for i in range(10):
    if i == 5:
        break
    if i % 2:
        continue

# Cannot use
# for = 10     # SyntaxError
# while = 5    # SyntaxError

Functions

1. Definition

# def, class, return
def my_function():
    return 42

class MyClass:
    pass

# Cannot use
# def = 5      # SyntaxError  
# class = 10   # SyntaxError

2. Lambda

# lambda
square = lambda x: x**2

# Cannot use
# lambda = 5  # SyntaxError

Boolean

1. Values

# True, False, None
is_valid = True
result = None

# Cannot reassign
# True = 1   # SyntaxError
# None = 0   # SyntaxError

2. Operators

# and, or, not
if x > 0 and x < 10:
    pass

if not is_empty:
    pass

# Cannot use
# and = True   # SyntaxError

Exception Handling

1. Try Block

# try, except, finally, raise
try:
    risky_operation()
except ValueError:
    handle_error()
finally:
    cleanup()

2. Assert

# assert
assert x > 0, "Must be positive"

# Cannot use
# assert = True  # SyntaxError

Import

1. Statements

# import, from, as
import math
from os import path
import numpy as np

# Cannot use
# import = 1   # SyntaxError

Scope

1. Global/Nonlocal

# global
count = 0

def increment():
    global count
    count += 1

# nonlocal
def outer():
    x = 10
    def inner():
        nonlocal x
        x += 1

Complete List

1. All Keywords

False      await      else       import     pass
None       break      except     in         raise
True       class      finally    is         return
and        continue   for        lambda     try
as         def        from       nonlocal   while
assert     del        global     not        with
async      elif       if         or         yield

Workarounds

1. Trailing Underscore

# Use trailing underscore
class_ = "MyClass"
type_ = "data"
from_ = "source"

2. Different Word

# Instead of 'class'
klass = MyClass
cls = MyClass

# Instead of 'type'
data_type = "string"
kind = "integer"