2. Keywords

2.1. List of keywords

import keyword

print(keyword.kwlist)
# ['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']

2.2. pass

  • Avoid error when you don't specify the body of a block

Listing 135. Exceptions has all the content needed inherited from Exception class. You need something to avoid IndentationError
class MyError(Exception):


print('hello')
# IndentationError: expected an indented block
class MyError(Exception):
    pass


print('hello')
# hello

2.3. __file__

print(__file__)
# /home/twardowski/bin/my_file.py
from os.path import dirname


dir = dirname(__file__)

print(f'Working directory: {dir}')
# Working directory: /home/twardowski/bin
from os.path import dirname, join


dir = dirname(__file__)
path = join(dir, 'main.py')

print(f'My file: {path}')
# My file: /home/twardowski/bin/main.py

2.4. del

DATA = {
    'first_name': 'Jan',
    'last_name': 'Twardowski',
}

print(DATA)
# {'first_name': 'Jan', 'last_name': 'Twardowski'}

del DATA['first_name']

print(DATA)
# {'last_name': 'Twardowski'}