The Python Standard Library

There are several standard libraries in Python that are essential to master for various programming tasks. Some of the most important ones include:

  1. os: For interacting with the operating system, such as file and directory manipulation.
  2. sys: Useful for system-specific parameters and functions.
  3. math: Provides mathematical functions and constants.
  4. datetime: For working with dates and times.
  5. collections: Contains data structures like lists, dictionaries, and sets.
  6. json: To work with JSON data.
  7. csv: For reading and writing CSV files.
  8. urllib: For making HTTP requests.
  9. re: Regular expressions for pattern matching.
  10. random: For generating random numbers.
  11. sqlite3: To work with SQLite databases.
  12. logging: Used for event logging in your applications.
  13. argparse: For parsing command-line arguments.
  14. unittest: Python’s built-in testing framework.

The libraries you should master depend on your specific programming needs. If you’re doing web development, you might need to focus on libraries like requests for HTTP requests or flask or Django for web applications. For data science, libraries like numpy, pandas, and matplotlib are crucial. Always consider your specific project requirements when deciding which libraries to master.

1. Introduction

2. Built-in Functions

Built-in Functions
abs(x) dict(**kwarg) help([object]) min(iterable, *[, key, default]) setattr(object, name, value)
all(iterable) dir([object]) hex(x) next(iterator[, default]) slice([start,] stop[, step])
any(iterable) divmod(a, b) id(object) object() sorted(iterable, key=func, reverse=False)
ascii(object) enumerate(iterable, start=0) input([prompt]) oct(int) @staticmethod
bin(int) eval(expression, globals=None, locals=None) int(x, base=10) open(file, mode=‘r’,…) str(object=’’)
bool([x]) exec(object[, globals[, locals]]) isinstance(object, classinfo) ord(char) sum(iterable[, start])
bytearray(s) filter(function, iterable) issubclass(class, classinfo) pow(x, y[, z]) super([type[, object-or-type]])
bytes(source) float([x]) iter(object[, sentinel]) print() tuple([iterable])
callable(object) format(value[, format_spec]) len(s) @property type(object)
chr(int) frozenset([iterable]) list([iterable]) range([start], stop[, step]) vars([object])
@classmethod getattr(object, name[, default]) locals() repr(object) zip(*iterables)
compile() globals() map(function, iterable, …) reversed(seq) _import_()
complex([real[, imag]]) hasattr(object, name) max(iterable, *[, key, default]) round(number[, ndigits])
delattr(object, name) hash(object) memoryview(bytes_obj) set([iterable])
  • @classmethod

    A class method receives the class as implicit first argument, just like an instance method receives the instance.

    1
    2
    3
    
    class C:
        @classmethod
        def f(cls, arg1, arg2, ...): ...
    

    It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.

  • globals()

    Return a dictionary representing the current global symbol table. This is always the dictionary of the current module (inside a function or method, this is the module where it is defined, not the module from which it is called).

  • iter(object[, sentinel])

    Return an iterator object.

    Without a second argument, object must be a collection object which supports the iteration protocol (the iter() method), or it must support the sequence protocol (the getitem() method with integer arguments starting at 0).

    If the second argument, sentinel, is given, then object must be a callable object. The iterator created in this case will call object with no arguments for each call to its next() method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.

    1
    2
    3
    
    with open('mydata.txt') as fp:
        for line in iter(fp.readline, ''):
            process_line(line)
    
  • locals()

    Update and return a dictionary representing the current local symbol table. Free variables are returned by locals() when it is called in function blocks, but not in class blocks.

    Note: The contents of this dictionary should not be modified; changes may not affect the values of local and free variables used by the interpreter.

  • open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)

    available modes: 'r', 'w', 'x', 'a', 'b', 't', '+'

    newline controls how universal newlines mode works (it only applies to text mode). It can be None, '', '\n', '\r', and '\r\n'.

  • ord(c) Unicode character to integer

  • print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

  • class property(fget=None, fset=None, fdel=None, doc=None)

    Return a property attribute. A property object has getter, setter, and deleter methods usable as decorators

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    
    class C:
        def __init__(self):
            self._x = None
    
        @property
        def x(self):
            """I'm the 'x' property."""
            return self._x
    
        @x.setter
        def x(self, value):
            self._x = value
    
        @x.deleter
        def x(self):
            del self._x
    
  • sorted(iterable, *, key=None, reverse=False)

    1
    2
    
    >>> sorted("This is a test string from Andrew".split(), key=str.lower)
    >>> sorted(student_objects, key=lambda student: student.age)   # sort by age
    
    1
    2
    3
    
    >>> from operator import itemgetter, attrgetter
    >>> sorted(student_tuples, key=itemgetter(2), reverse=True)
    >>> sorted(student_objects, key=attrgetter('age'), reverse=True)
    
    1
    2
    3
    4
    
    >>> from functools import cmp_to_key
    >>> def reverse_numeric(x, y):
    ...     return y - x
    >>> sorted([5, 2, 4, 1, 3], key=cmp_to_key(reverse_numeric))
    
  • @staticmethod

    A static method does not receive an implicit first argument.

    1
    2
    3
    
    class C:
        @staticmethod
        def f(arg1, arg2, ...): ...
    

    It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.

    Static methods in Python are similar to those found in Java or C++. @classmethod for a variant that is useful for creating alternate class constructors.

  • sum(iterable[, start])

    The iterable’s items are normally numbers. Fast way to concatenate a sequence of strings is by calling ‘’.join(sequence). To add floating point values with extended precision using math.fsum(). To concatenate a series of iterables, consider using itertools.chain().

  • super([type[, object-or-type]])

    Return a proxy object that delegates method calls to a parent or sibling class of type

    There are two typical use cases for super.

    1. super can be used to refer to parent classes without naming them explicitly with single inheritance.
    2. support cooperative multiple inheritance in a dynamic execution environment. see guide to using super().

    the zero argument form, super().method(arg) does the same thing as super(C, self).method(arg)

  • vars([object])

    Return the _dict_ attribute for a module, class, instance, or any other object with a _dict_ attribute.

  • zip(*iterables)

    zip() in conjunction with the * operator can be used to unzip a list:

    1
    2
    3
    4
    5
    6
    7
    8
    
    >>> x = [1, 2, 3]
    >>> y = [4, 5, 6]
    >>> zipped = zip(x, y)
    >>> list(zipped)
    [(1, 4), (2, 5), (3, 6)]
    >>> x2, y2 = zip(*zip(x, y))
    >>> x == list(x2) and y == list(y2)
    True
    

    itertools.zip_longest() can be used with unequal length inputs.

3. Built-in Constants

False, True, None, NotImplemented, Ellipsis, _debug_,

NotImplementedError and NotImplemented are not interchangeable.

3.1. Constants added by the site module

quit(code=None), exit(code=None), copyright, credits, license

4. Built-in Types

4.1. Truth Value Testing

An object is considered false either a __bool__() method that returns False or a __len__() method that returns zero. Here are most of the built-in objects considered false:

  • constants defined to be false: None and False.
  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • empty sequences and collections: '', (), [], {}, set(), range(0)

4.2. Boolean Operations — and, or, not

or and not

4.3. Comparisons

< <= > >= == != is is not

__lt__(), __le__(), __gt__(), __ge__() , __lt__(), __eq__()

4.4. Numeric Types — int, float, complex

To extract these parts from a complex number z, use z.real and z.imag

Python fully supports mixed arithmetic: when a binary arithmetic operator has operands of different numeric types, the operand with the “narrower” type is widened to that of the other, where integer is narrower than floating point, which is narrower than complex.

x + y x - y x * y x / y x // y x % y

-x +x abs(x) int(x)

float(x) complex(re,im) c.conjugate()

divmod(x, y) pow(x, y) x ** y

The resultant value operation // is a whole integer, though the result’s type is not necessarily int. The result is always rounded towards minus infinity1//2 is 0(-1)//2 is -11//(-2) is -1, and (-1)//(-2) is 0.

float() also accepts the strings “nan” and “inf” with an optional prefix “+” or “-” for Not a Number (NaN) and positive or negative infinity.

pow(0, 0) and 0 ** 0 to be 1

4.4.1. Bitwise Operations on Integer Types

x | y x ^ y x & y x << n x >> n ~x

  • A left shift by n bits is equivalent to multiplication by pow(2, n)without overflow check.
  • A right shift by n bits is equivalent to division by pow(2, n) without overflow check.

4.4.2. Additional Methods on Integer Types

int.bit_length()

int.to_bytes(length, byteorder, *, signed=False)

int.from_btes(bytes, byteorder, *, signed=False)

4.4.3. Additional Methods on Float

float.as_integer_ratio() Return a pair of integers whose ratio equal to the float.

float.is_integer()

float.hex()

float.fromhex(s)

4.4.4. Hashing of numeric types

4.5. Iterator Types

container.__iter__()

Return an iterator object. An example of an object supporting multiple forms of iteration would be a tree structure which supports both breadth-first and depth-first traversal.

two distinct methods allow user-defined classes to support iteration

  • iterator.__iter__()

    Return the iterator object itself.

  • iterator.__next__()

    Return the next item from the container. If there are no further items, raise the StopIteration exception.

4.5.1. Generator Types

If a container object’s __iter__() method is implemented as a generator, it will automatically return a generator object supplying the __iter__() and __next__() methods.

4.6. Sequence Types — list, tuple, range

basic sequence types: lists, tuples, and range objects. 

Additional sequence types: binary data and text strings.

4.6.1. Common Sequence Operations

x in s x not in s s + t s * n or n * s s[i] s[i:j] s[i:j:k] len(s) min(s) max(s) s.index(x[, i[, j]]) s.count(x)

Note that items in the sequence s are not copied; they are referenced multiple times.

1
2
3
4
5
6
>>> lists = [[]] * 3
>>> lists
[[], [], []]
>>> lists[0].append(3)
>>> lists
[[3], [3], [3]]
1
2
3
4
5
6
>>> lists = [[] for i in range(3)]
>>> lists[0].append(3)
>>> lists[1].append(5)
>>> lists[2].append(7)
>>> lists
[[3], [5], [7]]

Concatenating immutable sequences always results in a new object. This means that building up a sequence by repeated concatenation will have a quadratic runtime cost in the total sequence length. To get a linear runtime cost, you must switch to one of the alternatives below:

  • if concatenating str objects, you can build a list and use str.join() at the end or else write to an io.StringIO instance and retrieve its value when complete
  • if concatenating bytes objects, you can similarly use bytes.join() or io.BytesIO, or you can do in-place concatenation with a bytearray object. bytearray objects are mutable and have an efficient overallocation mechanism
  • if concatenating tuple objects, extend a list instead
  • for other types, investigate the relevant class documentation

s.index(x[, i[, j]])

index of the first occurrence of x in s (at or after index i and before index j)

4.6.2. Immutable Sequence Types

Immutable sequence support the hash() operation, but mutable sequence do not support.

4.6.3. Mutable Sequence Types

s[i] = x s[i:j] = t del s[i:j] s[i:j:k] = t del s[i:j:k] s.append(x) s.clear() s.copy() s.extend(t) or s += t s.insert(i, x) s.pop([i]) s.remove(x) s.reverse()

remove raises ValueError when x is not found in s.

The reverse() method modifies the sequence in place, it does not return the reversed sequence.

s *= n Items in the sequence are not copied; they are referenced multiple times.

4.6.4. Lists

class list([iterable])

sort(***, key=Nonereverse=False) functools.cmp_to_key()

4.6.5. Tuples

class tuple([iterable])

Note that it is actually the comma which makes a tuple, not the parentheses. he parentheses are optional, except in the empty tuple case, or when they are needed to avoid syntactic ambiguity, such function arguments. 

4.6.6. Ranges

class range([start,] stop[, step])

4.7. Text Sequence Type — str

Triple quoted strings may span multiple lines

the r (“raw”) prefix that disables most escape sequence processing.

There is also no mutable string type, but str.join() or io.StringIO can be used to efficiently construct strings from multiple fragments.

class str(objectencoding=‘utf-8’errors=‘strict’)

If object does not have a __str__() method, then str() falls back to returning repr(object).

If at least one of encoding or errors is given, object should be a bytes-like object (e.g. bytes or bytearray).

4.7.1. String Methods

  • str.capitalize()

  • str.casefold() is similar to lower()

  • str.center(width[, fillchar])

    1
    2
    3
    
    >>> a = 'test'
    >>> a.center(10, '*')
    '***test***'
    
  • str.count(sub[, start[, end]])

    1
    2
    3
    
    >>> a = 'test tree and flower'
    >>> str.count('e', 4, 10)
    2
    
  • str.encode(encoding="utf-8", errors="strict")

  • str.endswith(suffix[, start[, i]])

  • str.expandtabs(tabsize=8)

    1
    2
    3
    4
    
    >>> '01\t012\t0123\t01234'.expandtabs()
    '01      012     0123    01234'
    >>> '01\t012\t0123\t01234'.expandtabs(4)
    '01  012 0123    01234'
    
  • str.find(sub[, start[, end]])

    Return the lowest index in the string where substring sub is found.

  • str.format(*args, **kwargs)

    1
    2
    
    >>> "The sum of 1 + 2 is {0}".format(1+2)
    'The sum of 1 + 2 is 3'
    
  • str.format_map(mapping)

    This is useful if for example mapping is a dict subclass.

  • str.index(sub[, start[, end]])

    Like find(), but raise ValueError when the substring is not found.

  • str.isalnum()

    Return true if all characters in the string are alphanumeric and there is at least one alphanumeric.

  • str.isalpha()

    Return true if all characters in the string are alphabetic and there is at least one character.

  • str.isdecimal()

    Return true if all characters in the string are decimal characters and there is at least one character.

  • str.isdigit()

    Return true if all characters in the string are digits and there is at least one character.

  • str.isidentifier()

    Return true if the string is a valid identifier.

  • str.islower()

    Return true if all cased characters in the string are lowercase and there is at least one cased character.

  • str.isnumeric()

    Return true if all characters in the string are numeric characters, and there is at least one character, false otherwise.

  • str.isprintable()

  • str.isspace()

    Return true if there are only whitespace characters in the string and there is at least one character, false otherwise.

  • str.istitle()

    1
    2
    
    >>> 'This Is Title'.istitle()
    True
    
  • str.isupper()

    Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.

  • str.join(iterable)

  • str.ljust(width[, fillchar])

    1
    2
    
    >>> "this is a string".ljust(30, '0')
    'this is a string00000000000000'
    

    The original string is returned if width is less than or equal to len(s).

  • str.lower()

  • str.lstrip([chars])

    1
    2
    3
    4
    
    >>> '   spacious   '.lstrip()
    'spacious   '
    >>> 'www.example.com'.lstrip('cmowz.')
    'example.com'
    
  • static str.maketrans(x[, y[, z]])

    1
    2
    3
    4
    
    >>> transtable = str.maketrans('aeiou', '12345')
    >>> origin = 'this is a string example.'
    >>> origin.translate(transtable)
    'th3s 3s 1 str3ng 2x1mpl2.'
    
  • str.partition(sep)

    Return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing the string itself, followed by two empty strings.

  • str.replace(old, new[, count])

    If the optional argument count is given, only the first count occurrences are replaced.

  • str.rfind(sub[, start[, end]])

    Return the highest index in the string where substring sub is found, return -1 on failure.

  • str.rindex(sub[, start[, end]])

    Like rfind() but raises ValueError when the substring sub is not found.

  • str.rjust(width[, fillchar])

    1
    2
    
    >>> "this is a string".rjust(30, '0')
    '00000000000000this is a string'
    

    The original string is returned if width is less than or equal to len(s).

  • str.rpartition(sep)

    Split the string at the last occurrence of sep, and return a 3-tuple containing the part before the separator, the separator itself, and the part after the separator. If the separator is not found, return a 3-tuple containing two empty strings, followed by the string itself.

  • str.rsplit(sep=None, maxsplit=-1)

  • str.rstrip([chars])

    1
    2
    3
    4
    
    >>> '   spacious   '.rstrip()
    '   spacious'
    >>> 'mississippi'.rstrip('ipz')
    'mississ'
    
  • str.split(sep=None, maxsplit=-1)

    1
    2
    3
    4
    5
    6
    7
    8
    
    >>> '1,2,3'.split(',')
    ['1', '2', '3']
    >>> '1,2,3'.split(',', maxsplit=1)
    ['1', '2,3']
    >>> '1,2,,3,'.split(',')
    ['1', '2', '', '3', '']
    >>> '   1   2   3   '.split()
    ['1', '2', '3']
    
  • str.splitlines([keepends])

    1
    2
    3
    4
    
    >>> 'ab c\n\nde fg\rkl\r\n'.splitlines()
    ['ab c', '', 'de fg', 'kl']
    >>> 'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
    ['ab c\n', '\n', 'de fg\r', 'kl\r\n']
    
  • str.startswith(prefix[, start[, end]])

  • str.strip([chars])

    1
    2
    3
    4
    
    >>> '   spacious   '.strip()
    'spacious'
    >>> 'www.example.com'.strip('cmowz.')
    'example'
    
  • str.swapcase()

    1
    2
    
    >>> 'tEsT'.swapcase()
    'TeSt'
    
  • str.title()

    1
    2
    
    >>> "they're bill's friends from the UK".title()
    "They'Re Bill'S Friends From The Uk"
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    >>> import re
    >>> def titlecase(s):
    ...     return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
    ...                   lambda mo: mo.group(0)[0].upper() +
    ...                              mo.group(0)[1:].lower(),
    ...                   s)
    ...
    >>> titlecase("they're bill's friends.")
    "They're Bill's Friends."
    
  • str.translate(table)

    1
    2
    3
    4
    
    >>> transtable = str.maketrans('aeiou', '12345')
    >>> origin = 'this is a string example.'
    >>> origin.translate(transtable)
    'th3s 3s 1 str3ng 2x1mpl2.'
    
  • str.upper()

  • str.``zfill(width)

    Return a copy of the string left filled with ASCII '0' digits to make a string of length width. A leading sign prefix ('+'/'-') is handled by inserting the padding after the sign character rather than before. The original string is returned if width is less than or equal to len(s).

    1
    2
    3
    4
    
    >>> "42".zfill(5)
    '00042'
    >>> "-42".zfill(5)
    '-0042'
    

4.7.2. printf-style String Formatting

String objects have one unique built-in operation: the % operator (modulo). This is also known as the string formatting or interpolation operator.

Using the newer formatted string literals or the str.format() interface helps avoid these errors.

4.8. Binary Sequence Types — bytes, bytearray, memoryview

4.8.1. Bytes Objects

Bytes objects are immutable sequences of single bytes. 

class bytes([source[, encoding[, errors]]])

b'still allows embedded "double" quotes'

1
2
3
4
>>> bytes.fromhex('2Ef0 F1f2  ')
b'.\xf0\xf1\xf2'
>>> b'\xf0\xf1\xf2'.hex()
'f0f1f2'

Since bytes objects are sequences of integers (akin to a tuple), for a bytes object bb[0] will be an integer, while b[0:1] will be a bytes object of length 1. (This contrasts with text strings, where both indexing and slicing will produce a string of length 1)

The representation of bytes objects uses the literal format (b'...') since it is often more useful than e.g. bytes([46, 46, 46]). You can always convert a bytes object into a list of integers using list(b).

4.8.2. Bytearray Objects

bytearray objects are a mutable counterpart to bytes objects.

class bytearray([source[, encoding[, errors]]])

1
2
3
4
>>> bytearray.fromhex('2Ef0 F1f2  ')
bytearray(b'.\xf0\xf1\xf2')
>>> bytearray(b'\xf0\xf1\xf2').hex()
'f0f1f2'

Since bytearray objects are sequences of integers (akin to a list), for a bytearray object bb[0] will be an integer, while b[0:1] will be a bytearray object of length 1. (This contrasts with text strings, where both indexing and slicing will produce a string of length 1)

The representation of bytearray objects uses the bytes literal format (bytearray(b'...')) since it is often more useful than e.g. bytearray([46,46, 46]). You can always convert a bytearray object into a list of integers using list(b).

4.8.3. Bytes and Bytearray Operations

Both bytes and bytearray objects support the common sequence operations.

1
2
3
4
a = "abc"
b = a.replace("a", "f")
a = b"abc"
b = a.replace(b"a", b"f")
  • bytes.count(sub[, start[, end]])

  • bytearray.count(sub[, start[, end]])

    The subsequence to search for may be any bytes-like object or an integer in the range 0 to 255.

  • bytes.decode(encoding="utf-8", errors="strict")

  • bytearray.decode(encoding="utf-8", errors="strict")

    Return a string decoded from the given bytes.

  • bytes.endswith(suffix[, start[, end]])

  • bytearray.endswith(suffix[, start[, end]])

    The suffix(es) to search for may be any bytes-like object.

  • bytes.find(sub[, start[, end]])

  • bytearray.find(sub[, start[, end]])

    Return -1 if sub is not found. The subsequence to search for may be any bytes-like object or an integer in the range 0 to 255.

    Note The find() method should be used only if you need to know the position of sub. To check if sub is a substring or not, use the in operator:

  • bytes.index(sub[, start[, end]])

  • bytearray.index(sub[, start[, end]])

    Like find(), but raise ValueError when the subsequence is not found.

    The subsequence to search for may be any bytes-like object or an integer in the range 0 to 255.

  • bytes.join(iterable)

  • bytearray.join(iterable)

1
2
>>> b' '.join([b'this', b'is', b'an', b'example'])
b'this is an example'
  • static bytes.maketrans(from, to)

  • static bytearray.maketrans(from, to)

1
2
3
>>> trans_table = bytes.maketrans(b'abcd', b'efgh')
>>> b'abxxcd'.translate(trans_table)
b'efxxgh'
  • bytes.partition(sep)

  • bytearray.partition(sep)

1
2
3
4
>>> b'This is an example'.partition(b'an')
(b'This is ', b'an', b' example')
>>> b'This is an example'.partition(b'test')
(b'This is an example', b'', b'')
  • bytes.replace(old, new[, count])

  • bytearray.replace(old, new[, count])

    Note The bytearray version of this method does not operate in place - it always produces a new object, even if no changes were made.

  • bytes.rfind(sub[, start[, end]])

  • bytearray.rfind(sub[, start[, end]])

    Return -1 on failure.The subsequence to search for may be any bytes-like object or an integer in the range 0 to 255.

  • bytes.rindex(sub[, start[, end]])

  • bytearray.rindex(sub[, start[, end]])

    Like rfind() but raises ValueError when the subsequence sub is not found.

  • bytes.rpartition(sep)

  • bytearray.rpartition(sep)

  • bytes.startswith(prefix[, start[, end]])

  • bytearray.startswith(prefix[, start[, end]])

    The prefix(es) to search for may be any bytes-like object.

  • bytes.translate(table, delete=b'')

  • bytearray.translate(table, delete=b'')

Return a copy of the bytes or bytearray object where all bytes occurring in the optional argument delete are removed, and the remaining bytes have been mapped through the given translation table, which must be a bytes object of length 256.

1
2
>>> b'read this short text'.translate(None, b'aeiou')
b'rd ths shrt txt'

The following methods on bytes and bytearray objects have default behaviours that assume the use of ASCII compatible binary formats, but can still be used with arbitrary binary data by passing appropriate arguments. Note that all of the bytearray methods in this section do not operate in place, and instead produce new objects.

  • bytes.center(width[, fillbyte])
  • bytearray.center(width[, fillbyte])
1
2
>>> b'read this short text'.translate(None, b'aeiou')
b'rd ths shrt txt'

The original sequence is returned if width is less than or equal to len(s).

  • bytes.ljust(width[, fillbyte])
  • bytearray.ljust(width[, fillbyte])
1
2
>>> b'test'.ljust(10, b' ')
b'test      '

For bytes objects, the original sequence is returned if width is less than or equal to len(s).

  • bytes.lstrip([chars])
  • bytearray.lstrip([chars])
1
2
>>> b'www.example.com'.lstrip(b'cmowz.')
b'example.com'

If omitted or None, the chars argument defaults to removing ASCII whitespace.

  • bytes.rjust(width[, fillbyte])
  • bytearray.rjust(width[, fillbyte])
1
2
>>> b'test'.rjust(10, b' ')
b'      test'
  • bytes.rsplit(sep=None, maxsplit=-1)
  • bytearray.rsplit(sep=None, maxsplit=-1)
1
2
3
4
>>> b'example'.rsplit(b'e', maxsplit=1)
[b'exampl', b'']
>>> b'example'.rsplit(b'a')
[b'ex', b'mple']
  • bytes.rstrip([chars])
  • bytearray.rstrip([chars])
1
2
3
4
>>> b'   spacious   '.rstrip()
b'   spacious'
>>> b'mississippi'.rstrip(b'ipz')
b'mississ'

If omitted or None, the chars argument defaults to removing ASCII whitespace.

  • bytes.split(sep=None, maxsplit=-1)
  • bytearray.split(sep=None, maxsplit=-1)

If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty subsequences.

1
2
3
4
5
6
>>> b'1,2,3'.split(b',')
[b'1', b'2', b'3']
>>> b'1,2,3'.split(b',', maxsplit=1)
[b'1', b'2,3']
>>> b'1,2,,3,'.split(b',')
[b'1', b'2', b'', b'3', b'']

If sep is not specified or is None, runs of consecutive ASCII whitespace are regarded as a single separator

1
2
>>> b'   1   2   3   '.split()
[b'1', b'2', b'3']
  • bytes.strip([chars])
  • bytearray.strip([chars])
1
2
3
4
>>> b'   spacious   '.strip()
b'spacious'
>>> b'www.example.com'.strip(b'cmowz.')
b'example'
  • bytes.capitalize()
  • bytearray.capitalize()
1
2
3
4
>>> b'   spacious   '.strip()
b'spacious'
>>> b'www.example.com'.strip(b'cmowz.')
b'example'
  • bytes.expandtabs(tabsize=8)
  • bytearray.expandtabs(tabsize=8)
1
2
3
4
>>> b'ABCabc1'.isalnum()
True
>>> b'ABC abc1'.isalnum()
False
  • bytes.isalnum()
  • bytearray.isalnum()
1
2
3
4
>>> b'ABCabc1'.isalnum()
True
>>> b'ABC abc1'.isalnum()
False
  • bytes.isalpha()
  • bytearray.isalpha()
1
2
3
4
>>> b'ABCabc'.isalpha()
True
>>> b'ABCabc1'.isalpha()
False
  • bytes.isdigit()
  • bytearray.isdigit()
1
2
3
4
>>> b'1234'.isdigit()
True
>>> b'1.23'.isdigit()
False
  • bytes.islower()
  • bytearray.islower()
1
2
3
4
>>> b'hello world'.islower()
True
>>> b'Hello world'.islower()
False
  • bytes.isspace()
  • bytearray.isspace()
1
2
3
4
5
>>> b'Hello World!'.isspace()
False
# space, tab, newline, carriage return, vertical tab, form feed
>>> b' \t\n\r\x0b\f'.isspace()
True
  • bytes.istitle()
  • bytearray.istitle()
1
2
3
4
>>> b'Hello World'.istitle()
True
>>> b'Hello world'.istitle()
False
  • bytes.isupper()
  • bytearray.isupper()
1
2
3
4
>>> b'HELLO WORLD'.isupper()
True
>>> b'Hello world'.isupper()
False
  • bytes.lower()
  • bytearray.lower()
1
2
>>> b'Hello World'.lower()
b'hello world'
  • bytes.splitlines(keepends=False)
  • bytearray.splitlines(keepends=False)
1
2
3
4
>>> b'ab c\n\nde fg\rkl\r\n'.splitlines()
[b'ab c', b'', b'de fg', b'kl']
>>> b'ab c\n\nde fg\rkl\r\n'.splitlines(keepends=True)
[b'ab c\n', b'\n', b'de fg\r', b'kl\r\n']

Unlike split() when a delimiter string sep is given, this method returns an empty list for the empty string, and a terminal line break does not result in an extra line:

1
2
3
4
>>> b"".split(b'\n'), b"Two lines\n".split(b'\n')
([b''], [b'Two lines', b''])
>>> b"".splitlines(), b"One line\n".splitlines()
([], [b'One line'])
  • bytes.swapcase()
  • bytearray.swapcase()
1
2
>>> b'Hello World'.swapcase()
b'hELLO wORLD'
  • bytes.title()
  • bytearray.title()
1
2
>>> b'Hello world'.title()
b'Hello World'
1
2
>>> b"they're bill's friends from the UK".title()
b"They'Re Bill'S Friends From The Uk"

A workaround for apostrophes can be constructed using regular expressions:

1
2
3
4
5
6
7
8
9
>>> import re
>>> def titlecase(s):
...     return re.sub(rb"[A-Za-z]+('[A-Za-z]+)?",
...                   lambda mo: mo.group(0)[0:1].upper() +
...                              mo.group(0)[1:].lower(),
...                   s)
...
>>> titlecase(b"they're bill's friends.")
b"They're Bill's Friends."
  • bytes.upper()
  • bytearray.upper()
1
2
>>> b'Hello World'.upper()
b'HELLO WORLD'
  • bytes.zfill(width)
  • bytearray.zfill(width)
1
2
3
4
>>> b"42".zfill(5)
b'00042'
>>> b"-42".zfill(5)
b'-0042'

4.9. Set Types — set, frozenset

Common uses include membership testing, removing duplicates from a sequence, and computing mathematical operations such as intersection, union, difference, and symmetric difference.

Being an unordered collection, sets do not support indexing, slicing, or other sequence-like behavior.

The set type is mutable — the contents can be changed using methods like add() and remove(). Since it is mutable, it has no hash value and cannot be used as either a dictionary key or as an element of another set.

The frozenset type is immutable and hashable, it can therefore be used as a dictionary key or as an element of another set.

Instances of set and frozenset provide the following operations:

  • len(s)

  • x in s

  • x not in s

  • isdisjoint(other)

    Return True if the set has no elements in common with other.

  • issubset(other)

  • set <= other

    Test whether every element in the set is in other.

  • set < other

    Test whether the set is a proper subset of other, that is, set <=other and set != other.

  • issuperset(other)

  • set >= other

    Test whether every element in other is in the set.

  • set > other

    Test whether the set is a proper superset of other, that is, set >=other and set != other.

  • union(*others)

  • set | other | ...

    Return a new set with elements from the set and all others.

  • intersection(*others)

  • set & other & ...

    Return a new set with elements common to the set and all others.

  • difference(*others)

  • set - other - ...

    Return a new set with elements in the set that are not in the others.

  • symmetric_difference(other)

  • set ^ other

    Return a new set with elements in either the set or other but not both.

  • copy()

    Return a new set with a shallow copy of s.

Instances of set provide the following operations: that do not apply to immutable instances of frozenset:

  • update(*others)

  • set |= other | ...

    Update the set, adding elements from all others.

  • intersection_update(*others)

  • set &= other & ...

    Update the set, keeping only elements found in it and all others.

  • difference_update(*others)

  • set -= other | ...

    Update the set, removing elements found in others.

  • symmetric_difference_update(other)

  • set ^= other

    Update the set, keeping only elements found in either set, but not in both.

  • add(elem)

  • remove(elem) Raises KeyError if elem is not contained in the set.

  • discard(elem)

    Remove element elem from the set if it is present.

  • pop()

    Remove and return an arbitrary element from the set. Raises KeyError if the set is empty.

  • clear()

4.10. Mapping Types — dict

class dict(**kwarg) class dict(mapping, **kwarg) class dict(iterable, **kwarg)

1
2
3
4
5
6
7
>>> a = dict(one=1, two=2, three=3)
>>> b = {'one': 1, 'two': 2, 'three': 3}
>>> c = dict(zip(['one', 'two', 'three'], [1, 2, 3]))
>>> d = dict([('two', 2), ('one', 1), ('three', 3)])
>>> e = dict({'three': 3, 'one': 1, 'two': 2})
>>> a == b == c == d == e
True
  • len(d)
  • d[key]
  • d[key] = value Raises a KeyError if key is not in the map. __missing__()

  • del d[key] Raises a KeyError if key is not in the map.

  • key in d

  • key not in d

  • iter(d) Return an iterator over the keys of the dictionary.

  • clear()

  • copy() Return a shallow copy of the dictionary.

  • classmethod fromkeys(seq[, value])

    Create a new dictionary with keys from seq and values set to value.

  • get(key[, default]) If default is not given, it defaults to None.

  • items()

  • keys()

  • pop(key[, default])

    If key is in the dictionary, remove it and return its value, else return default. If default is not given and key is not in the dictionary, a KeyError is raised.

  • popitem()

    Remove and return an arbitrary (key, value) pair from the dictionary. If the dictionary is empty, calling popitem() raises a KeyError.

  • setdefault(key[, default])

    If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.

  • update([other])

    update() accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). If keyword arguments are specified, the dictionary is then updated with those key/value pairs.

  • values()

4.11. Context Manager Types

4.12. Other Built-in Types

4.13. Special Attributes

5. Built-in Exceptions

Programmers are encouraged to derive new exceptions from the Exception class or one of its subclasses, and not from BaseException.

5.1. Base classes

5.2. Concrete exceptions

  • exception AssertionError

    Raised when an assert statement fails.

  • exception AttributeError

    Raised when an attribute reference or assignment fails.

  • exception EOFError

    Raised when the input() function hits an end-of-file condition (EOF) without reading any data.

  • exception ImportError

    Raised when the import statement has troubles trying to load a module. Also raised when the “from list” in from ... import has a name that cannot be found.

  • exception IndexError

    Raised when a sequence subscript is out of range.

  • exception KeyError

    Raised when a mapping (dictionary) key is not found in the set of existing keys.

  • exception KeyboardInterrupt

    Raised when the user hits the interrupt key (normally Control-C or Delete). 

  • exception NotImplementedError

    In user defined base classes, abstract methods should raise this exception when they require derived classes to override the method.

5.3. Warnings

5.4. Exception hierarchy

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
BaseException
 +-- SystemExit
 +-- KeyboardInterrupt
 +-- GeneratorExit
 +-- Exception
      +-- StopIteration
      +-- StopAsyncIteration
      +-- ArithmeticError
      |    +-- FloatingPointError
      |    +-- OverflowError
      |    +-- ZeroDivisionError
      +-- AssertionError
      +-- AttributeError
      +-- BufferError
      +-- EOFError
      +-- ImportError
      |    +-- ModuleNotFoundError
      +-- LookupError
      |    +-- IndexError
      |    +-- KeyError
      +-- MemoryError
      +-- NameError
      |    +-- UnboundLocalError
      +-- OSError
      |    +-- BlockingIOError
      |    +-- ChildProcessError
      |    +-- ConnectionError
      |    |    +-- BrokenPipeError
      |    |    +-- ConnectionAbortedError
      |    |    +-- ConnectionRefusedError
      |    |    +-- ConnectionResetError
      |    +-- FileExistsError
      |    +-- FileNotFoundError
      |    +-- InterruptedError
      |    +-- IsADirectoryError
      |    +-- NotADirectoryError
      |    +-- PermissionError
      |    +-- ProcessLookupError
      |    +-- TimeoutError
      +-- ReferenceError
      +-- RuntimeError
      |    +-- NotImplementedError
      |    +-- RecursionError
      +-- SyntaxError
      |    +-- IndentationError
      |         +-- TabError
      +-- SystemError
      +-- TypeError
      +-- ValueError
      |    +-- UnicodeError
      |         +-- UnicodeDecodeError
      |         +-- UnicodeEncodeError
      |         +-- UnicodeTranslateError
      +-- Warning
           +-- DeprecationWarning
           +-- PendingDeprecationWarning
           +-- RuntimeWarning
           +-- SyntaxWarning
           +-- UserWarning
           +-- FutureWarning
           +-- ImportWarning
           +-- UnicodeWarning
           +-- BytesWarning
           +-- ResourceWarning

6. Text Processing Services

6.1. string — Common string operations

6.1.1. String constants

  • string.ascii_letters

  • string.ascii_lowercase 'abcdefghijklmnopqrstuvwxyz'

  • string.ascii_uppercase 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'

  • string.digits '0123456789'

  • string.hexdigits '0123456789abcdefABCDEF'

  • string.octdigits '01234567'

  • string.punctuation

  • string.printable

  • string.whitespace

6.1.3.2. Format examples

Accessing arguments by position:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
>>> '{0}, {1}, {2}'.format('a', 'b', 'c')
'a, b, c'
>>> '{}, {}, {}'.format('a', 'b', 'c')  
'a, b, c'
>>> '{2}, {1}, {0}'.format('a', 'b', 'c')
'c, b, a'
>>> '{2}, {1}, {0}'.format(*'abc')      # unpacking argument sequence
'c, b, a'
>>> '{0}{1}{0}'.format('abra', 'cad')   # arguments' indices can be repeated
'abracadabra'

Accessing arguments by name:

1
2
3
4
5
>>> 'Coordinates: {latitude}, {longitude}'.format(latitude='37.24N', longitude='-115.81W')
'Coordinates: 37.24N, -115.81W'
>>> coord = {'latitude': '37.24N', 'longitude': '-115.81W'}
>>> 'Coordinates: {latitude}, {longitude}'.format(**coord)
'Coordinates: 37.24N, -115.81W'

Accessing arguments’ attributes:

1
2
3
4
5
>>> class Point:
...     def __init__(self, x, y):
...         self.x, self.y = x, y
...     def __str__(self):
...         return 'Point({self.x}, {self.y})'.format(self=self)

Accessing arguments’ items:

1
2
3
>>> coord = (3, 5)
>>> 'X: {0[0]};  Y: {0[1]}'.format(coord)
'X: 3;  Y: 5'

Replacing %s and %r:

1
2
>>> "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')
"repr() shows quotes: 'test1'; str() doesn't: test2"

Aligning the text and specifying a width:

1
2
3
4
5
6
7
8
>>> '{:<30}'.format('left aligned')
'left aligned                  '
>>> '{:>30}'.format('right aligned')
'                 right aligned'
>>> '{:^30}'.format('centered')
'           centered           '
>>> '{:*^30}'.format('centered')  # use '*' as a fill char
'***********centered***********'

Replacing %+f%-f, and % f and specifying a sign:

1
2
3
4
5
6
>>> '{:+f}; {:+f}'.format(3.14, -3.14)  # show it always
'+3.140000; -3.140000'
>>> '{: f}; {: f}'.format(3.14, -3.14)  # show a space for positive numbers
' 3.140000; -3.140000'
>>> '{:-f}; {:-f}'.format(3.14, -3.14)  # show only the minus -- same as '{:f}; {:f}'
'3.140000; -3.140000'

Using the comma as a thousands separator:

1
2
>>> '{:,}'.format(1234567890)
'1,234,567,890'

Expressing a percentage:

1
2
3
4
>>> points = 19
>>> total = 22
>>> 'Correct answers: {:.2%}'.format(points/total)
'Correct answers: 86.36%'

Using type-specific formatting:

1
2
3
4
>>> import datetime
>>> d = datetime.datetime(2010, 7, 4, 12, 15, 58)
>>> '{:%Y-%m-%d %H:%M:%S}'.format(d)
'2010-07-04 12:15:58'

Nesting arguments and more complex examples:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
>>> for align, text in zip('<^>', ['left', 'center', 'right']):
...     '{0:{fill}{align}16}'.format(text, fill=align, align=align)
...
'left<<<<<<<<<<<<'
'^^^^^center^^^^^'
'>>>>>>>>>>>right'
>>>
>>> octets = [192, 168, 0, 1]
>>> '{:02X}{:02X}{:02X}{:02X}'.format(*octets)
'C0A80001'
>>> int(_, 16)
3232235521
>>>
>>> width = 5
>>> for num in range(5,12): 
...     for base in 'dXob':
...         print('{0:{width}{base}}'.format(num, base=base, width=width), end=' ')
...     print()
...
    5     5     5   101
    6     6     6   110
    7     7     7   111
    8     8    10  1000
    9     9    11  1001
   10     A    12  1010
   11     B    13  1011

6.1.4. Template strings

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'

# $$ is an escape; it is replaced with a single $.
>>> d = dict(who='tim')
>>> Template('Give $who $100').substitute(d)
Traceback (most recent call last):
...
ValueError: Invalid placeholder in string: line 1, col 11

>>> Template('$who likes $what').substitute(d)
Traceback (most recent call last):
...
KeyError: 'what'
>>> Template('$who likes $what').safe_substitute(d)
'tim likes $what'

6.2. re — Regular expression operations

The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. It’s highly recommended that you use raw strings for all but the simplest expressions.

6.2.1. Regular Expression Syntax

special characters meaning
. this matches any character except a newline unless the DOTALL flag hash been specified.
^ Matches the start of the string , and in MULTILINEmode also matches immediately after each newline.
$ Matches the end of the string or just before the newline at the end of the string, and in MULTILINE mode also matches before a newline.
* to match 0 or more repetitions, the qualifiers are all greedy.
+ to match 0 or 1 repetitions of the preceding RE. The qualifiers are all greedy.
? to match 0 or 1 repetitions of the preceding RE. The qualifiers are all greedy.
*?, +?, ?? The '*', '+', and '?' qualifiers are all greedy; Adding ? after the qualifier makes it perform the match in non-greedy or minimal fashion;
{m} Specifies that exactly m copies of the previous RE should be matched
{m,n} Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as many repetitions as possible.
{m,n}? Causes the resulting RE to match from m to n repetitions of the preceding RE, attempting to match as few repetitions as possible.
\ Either escapes special characters or signals a special sequence
[] Used to indicate a set of characters. e.g. [amk] will match 'a''m', or 'k'. Ranges of characters can be indicated by giving two characters and separating them by a '-'. Special characters lose their special meaning inside sets. If the first character of the set is '^', all the characters that are not in the set will be matched.
` `
(...) the contents of a group can be matched later in the string with the \number special sequence
(?...) usually do not create a new group; (?P<name>...) is the only exception to this rule.
(?aiLmsux) The group matches the empty string; the letters set the corresponding flags: re.A] (ASCII-only matching), re.I (ignore case), re.L (locale dependent), re.M (multi-line), re.S (dot matches all), re.U (Unicode matching), and re.X (verbose), for the entire regular expression.
(?:...) A non-capturing version of regular parentheses.
(?imsx-imsx:...) removes the corresponding flags.
(?P<name>...) the substring matched by the group is accessible via the symbolic group name name.
(?P=name) A backreference to a named group.
(?#...) A comment
(?=...) For example, Isaac (?=Asimov) will match 'Isaac ' only if it’s followed by 'Asimov'.
(?!...) For example, Isaac (?!Asimov) will match 'Isaac ' only if it’s not followed by 'Asimov'.
(?<=...) This is called a positive lookbehind assertion. (?<=abc)def will find a match in 'abcdef',
(?<!...) This is called a negative lookbehind assertion.
\number Matches the contents of the group of the same number.
\A Matches only at the start of the string.
\b Matches the empty string, but only at the beginning or end of a word. r'\bfoo\b'
\B Matches the empty string, but only when it is not at the beginning or end of a word.
\d If the ASCII flag is used only [0-9] is matched. Using an explicit [0-9] may be a better choice
\D If the ASCII flag is used only [^0-9] is matched. Using an explicit [^0-9] may be a better choice
\s If the ASCII flag is used, only [ \t\n\r\f\v] is matched
\S If the ASCII flag is used this becomes the equivalent of [^ \t\n\r\f\v]
\w If the ASCII flag is used, only [a-zA-Z0-9_] is matched.
\W If the ASCII flag is used this becomes the equivalent of [^a-zA-Z0-9_]
\Z Matches only at the end of the string.

6.2.2. Module Contents

  • re.compile(pattern, flags=0)

    1
    2
    
    prog = re.compile(pattern)
    result = prog.match(string)
    

    is equivalent to

    1
    
    result = re.match(pattern, string)
    
  • re.A

  • re.ASCII

  • re.DEBUG

  • re.I

  • re.IGNORECASE

  • re.L

  • re.LOCALE

  • re.M

  • re.MULTILINE

    When specified, the pattern character '^' matches at the beginning of the string and at the beginning of each line (immediately following each newline); and the pattern character '$' matches at the end of the string and at the end of each line (immediately preceding each newline). By default, '^' matches only at the beginning of the string, and '$' only at the end of the string.

  • re.S

  • re.DOTALL

    without this flag, '.' will match anything except a newline. 

  • re.X

  • re.VERBOSE

    1
    2
    3
    
    a = re.compile(r"""\d +  # the integral part
                       \.    # the decimal point
                       \d *  # some fractional digits""", re.X)
    

    equal to

    1
    
    b = re.compile(r"\d+\.\d*")
    
  • re.search(pattern, string, flags=0)

    Looking for the first location where the regular expression pattern produces a match, and return a corresponding match object. Return None if no position in the string matches the pattern.

    Regular expressions beginning with '^' can be used with search() to restrict the match at the beginning of the string:

    1
    2
    3
    4
    
    >>> re.match("c", "abcdef")    # No match
    >>> re.search("^c", "abcdef")  # No match
    >>> re.search("^a", "abcdef")  # Match
    <_sre.SRE_Match object; span=(0, 1), match='a'>
    
  • re.match(pattern, string, flags=0)

    Return a corresponding match object or None. re.match() checks for a match only at the beginning of the string,

    If you want to locate a match anywhere in string, use search() instead.

    1
    2
    3
    
    >>> re.match("c", "abcdef")    # No match
    >>> re.search("c", "abcdef")   # Match
    <_sre.SRE_Match object; span=(2, 3), match='c'>
    
  • re.fullmatch(pattern, string, flags=0)

    Return a corresponding match object If the whole string matches the regular expression pattern else return None.

  • re.split(pattern, string, maxsplit=0, flags=0)

    If capturing parentheses are used in pattern, then the text of all groups in the pattern are also returned as part of the resulting list.

    1
    2
    3
    4
    5
    6
    7
    8
    
    >>> re.split(r'\W+', 'Words, words, words.')
    ['Words', 'words', 'words', '']
    >>> re.split(r'(\W+)', 'Words, words, words.')
    ['Words', ', ', 'words', ', ', 'words', '.', '']
    >>> re.split(r'\W+', 'Words, words, words.', 1)
    ['Words', 'words, words.']
    >>> re.split('[a-f]+', '0a3B9', flags=re.IGNORECASE)
    ['0', '3', '9']
    

    If there are capturing groups in the separator and it matches at the start of the string, the result will start with an empty string. The same holds for the end of the string:

    1
    2
    
    >>> re.split(r'(\W+)', '...words, words...')
    ['', '...', 'words', ', ', 'words', '...', '']
    
  • re.findall(pattern, string, flags=0)

    Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group.

    1
    2
    3
    
    >>> text = "He was carefully disguised but captured quickly by police."
    >>> re.findall(r"\w+ly", text)
    ['carefully', 'quickly']
    
  • re.finditer(pattern, string, flags=0)

    Return an iterator yielding match objects.

    1
    2
    3
    4
    5
    
    >>> text = "He was carefully disguised but captured quickly by police."
    >>> for m in re.finditer(r"\w+ly", text):
    ...     print('%02d-%02d: %s' % (m.start(), m.end(), m.group(0)))
    07-16: carefully
    40-47: quickly
    
  • re.sub(pattern, repl, string, count=0, flags=0)

    Return the string obtained by replacing the leftmost non-overlapping occurrences of pattern in string by the replacement repl.

    repl can be a string or a function.

    1
    2
    3
    4
    5
    6
    7
    
    >>> def dashrepl(matchobj):
    ...     if matchobj.group(0) == '-': return ' '
    ...     else: return '-'
    >>> re.sub('-{1,2}', dashrepl, 'pro----gram-files')
    'pro--gram files'
    >>> re.sub(r'\sAND\s', ' & ', 'Baked Beans And Spam', flags=re.IGNORECASE)
    'Baked Beans & Spam'
    
  • re.subn(pattern, repl, string, count=0, flags=0)

    Perform the same operation as sub(), but return a tuple (new_string,number_of_subs_made).

  • re.escape(pattern)

    Escape all the characters in pattern except ASCII letters, numbers and '_'

    1
    2
    
    >>> print(re.escape('python.exe'))
    python\.exe
    
  • re.purge()

    Clear the regular expression cache.

6.2.3. Regular Expression Objects

  • regex.search(string[, pos[, endpos]])

    1
    2
    3
    4
    
    >>> pattern = re.compile("d")
    >>> pattern.search("dog")     # Match at index 0
    <_sre.SRE_Match object; span=(0, 1), match='d'>
    >>> pattern.search("dog", 1)  # No match; search doesn't include the "d"
    
  • regex.match(string[, pos[, endpos]])

    If zero or more characters at the beginning of string match this regular expression, return a corresponding match object.

    1
    2
    3
    4
    
    >>> pattern = re.compile("o")
    >>> pattern.match("dog")      # No match as "o" is not at the start of "dog".
    >>> pattern.match("dog", 1)   # Match as "o" is the 2nd character of "dog".
    <_sre.SRE_Match object; span=(1, 2), match='o'>
    
  • regex.fullmatch(string[, pos[, endpos]])

    1
    2
    3
    4
    5
    
    >>> pattern = re.compile("o[gh]")
    >>> pattern.fullmatch("dog")      # No match as "o" is not at the start of "dog".
    >>> pattern.fullmatch("ogre")     # No match as not the full string matches.
    >>> pattern.fullmatch("doggie", 1, 3)   # Matches within given limits.
    <_sre.SRE_Match object; span=(1, 3), match='og'>
    
  • regex.split(string, maxsplit=0)

  • regex.findall(string[, pos[, endpos]])

  • regex.finditer(string[, pos[, endpos]])

  • regex.sub(repl, string, count=0)

  • regex.subn(repl, string, count=0)

  • regex.flags

  • regex.groups

  • regex.groupindex

  • regex.pattern

6.2.4. Match Objects

1
2
3
match = re.search(pattern, string)
if match:
    process(match)
  • match.expand(template)

    Return the string obtained by doing backslash substitution on the template string template.

    numeric backreferences (\1\2) and named backreferences (\g<1>\g<name>) are replaced by the contents of the corresponding group.

    1
    2
    3
    
    >>> m = re.search(r'(e)x(a)', 'example')
    >>> m.expand(r'\1'), m.expand(r'\g<2>')
    ('e', 'a')
    
  • match.group([group1, ...])

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
    >>> m = re.match(r"(\w+) (\w+)", "Isaac Newton, physicist")
    >>> m.group(0)       # The entire match
    'Isaac Newton'
    >>> m.group(1)       # The first parenthesized subgroup.
    'Isaac'
    >>> m.group(2)       # The second parenthesized subgroup.
    'Newton'
    >>> m.group(1, 2)    # Multiple arguments give us a tuple.
    ('Isaac', 'Newton')
    
    1
    2
    3
    4
    5
    
    >>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
    >>> m.group('first_name')
    'Malcolm'
    >>> m.group('last_name')
    'Reynolds'
    

    If a group matches multiple times, only the last match is accessible:

  • match.__getitem__(g)

  • match.groups(default=None)

    Return a tuple containing all the subgroups of the match, from 1 up to however many groups are in the pattern. 

    1
    2
    3
    
    >>> m = re.match(r"(\d+)\.(\d+)", "24.1632")
    >>> m.groups()
    ('24', '1632')
    
  • match.groupdict(default=None)

    1
    2
    3
    
    >>> m = re.match(r"(?P<first_name>\w+) (?P<last_name>\w+)", "Malcolm Reynolds")
    >>> m.groupdict()
    {'first_name': 'Malcolm', 'last_name': 'Reynolds'}
    
  • match.start([group])

  • match.end([group])

  • match.span([group])

  • match.pos

  • match.endpos

  • match.lastindex

  • match.lastgroup

  • match.re

  • match.string

6.2.5.8. Raw String Notation

Raw string notation (r"text") keeps regular expressions sane. Without it, every backslash ('\') in a regular expression would have to be prefixed with another one to escape it.

1
2
3
4
>>> re.match(r"\W(.)\1\W", " ff ")
<_sre.SRE_Match object; span=(0, 4), match=' ff '>
>>> re.match("\\W(.)\\1\\W", " ff ")
<_sre.SRE_Match object; span=(0, 4), match=' ff '>
1
2
3
4
>>> re.match(r"\\", r"\\")
<_sre.SRE_Match object; span=(0, 1), match='\\'>
>>> re.match("\\\\", r"\\")
<_sre.SRE_Match object; span=(0, 1), match='\\'>

6.3. difflib — Helpers for computing deltas

6.4. textwrap — Text wrapping and filling

6.5. unicodedata — Unicode Database

6.6. stringprep — Internet String Preparation

6.7. readline — GNU readline interface

6.8. rlcompleter — Completion function for GNU readline

7. Binary Data Services

7.1. struct — Interpret bytes as packed binary data

7.2. codecs — Codec registry and base classes

8. Data Types

8.1. datetime — Basic date and time types

8.1.1. Available Types

  • class datetime.date

    Attributes: year, month, and day.

  • class datetime.time

    Attributes: hour, minute, second, microsecond, and tzinfo.

  • class datetime.datetime

    Attributes: year, month, and day, hour, minute, second, microsecond, and tzinfo.

  • class datetime.timedelta

    A duration expressing the difference between two date, time, or datetime instances to microsecond resolution.

  • class datetime.tzinfo

    These are used by the datetime and time classes to provide a customizable notion of time adjustment

  • class datetime.timezone

8.1.2. timedelta Objects

  • class datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

    Instance attributes (read-only): days seconds microseconds

    Instance methods: timedelta.total_seconds()

    Supported operations: +, -, *, /, //, %, divmod, +t1, -t1, abs(t), str(t), repr(t)

8.1.3. date Objects

  • class datetime.date(year, month, day)
  • classmethod date.today()
  • classmethod date.fromtimestamp(timestamp)
  • classmethod date.fromordinal(ordinal)

Instance attributes (read-only):

  • date.year
  • date.month
  • date.day

Supported operations:

  • date2 = date1 + timedelta
  • date2 = date1 - timedelta
  • timedelta = date1 - date2
  • date1 < date2

Instance methods:

  • date.replace(year=self.year, month=self.month, day=self.day)

  • date.timetuple()

  • date.toordinal()

  • date.weekday()

    Return the day of the week as an integer, where Monday is 0 and Sunday is 6.

  • date.isoweekday()

    Return the day of the week as an integer, where Monday is 1 and Sunday is 7.

  • date.isocalendar()

    Return a 3-tuple, (ISO year, ISO week number, ISO weekday).

    1
    2
    
    date(2003, 12, 29).isocalendar() == (2004, 1, 1)
    date(2004, 1, 4).isocalendar() == (2004, 1, 7)
    
  • date.isoformat()

    1
    
    date(2002, 12, 4).isoformat() == '2002-12-04'
    
  • date.__str__()

    For a date dstr(d) is equivalent to d.isoformat().

  • date.ctime()

    1
    
    date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'
    
    1
    
    d.ctime() == time.ctime(time.mktime(d.timetuple()))
    
  • date.strftime(format)

  • date.__format__(format)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
>>> import time
>>> from datetime import date
>>> today = date.today()
>>> today
datetime.date(2007, 12, 5)
>>> today == date.fromtimestamp(time.time())
True
>>> my_birthday = date(today.year, 6, 24)
>>> if my_birthday < today:
...     my_birthday = my_birthday.replace(year=today.year + 1)
>>> my_birthday
datetime.date(2008, 6, 24)
>>> time_to_birthday = abs(my_birthday - today)
>>> time_to_birthday.days
202
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
>>> from datetime import date
>>> d = date.fromordinal(730920) # 730920th day after 1. 1. 0001
>>> d
datetime.date(2002, 3, 11)
>>> t = d.timetuple()
>>> for i in t:     
...     print(i)
2002                # year
3                   # month
11                  # day
0
0
0
0                   # weekday (0 = Monday)
70                  # 70th day in the year
-1
>>> ic = d.isocalendar()
>>> for i in ic:    
...     print(i)
2002                # ISO year
11                  # ISO week number
1                   # ISO day number ( 1 = Monday )
>>> d.isoformat()
'2002-03-11'
>>> d.strftime("%d/%m/%y")
'11/03/02'
>>> d.strftime("%A %d. %B %Y")
'Monday 11. March 2002'
>>> 'The {1} is {0:%d}, the {2} is {0:%B}.'.format(d, "day", "month")
'The day is 11, the month is March.'

8.1.4. datetime Objects

Constructor:

  • class datetime.datetime(year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0)

  • classmethod datetime.today()

  • classmethod datetime.now(tz=None)

    1
    2
    
    >>> datetime.now()   
    datetime.datetime(2007, 12, 6, 16, 29, 43, 79043)   # GMT +1
    
  • classmethod datetime.utcnow()

    1
    2
    
    >>> datetime.utcnow()   
    datetime.datetime(2007, 12, 6, 15, 29, 43, 79060)
    
  • classmethod datetime.fromtimestamp(timestamp, tz=None)

  • classmethod datetime.utcfromtimestamp(timestamp)

  • classmethod datetime.fromordinal(ordinal)

  • classmethod datetime.combine(date, time, tzinfo=self.tzinfo)

    1
    2
    3
    4
    5
    
    >>> from datetime import datetime, date, time
    >>> d = date(2005, 7, 14)
    >>> t = time(12, 30)
    >>> datetime.combine(d, t)
    datetime.datetime(2005, 7, 14, 12, 30)
    
  • classmethod datetime.strptime(date_string, format)

    1
    2
    3
    
    >>> dt = datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M")
    >>> dt
    datetime.datetime(2006, 11, 21, 16, 30)
    

Instance attributes (read-only):

  • datetime.year
  • datetime.month
  • datetime.day
  • datetime.hour
  • datetime.minute
  • datetime.second
  • datetime.microsecond
  • datetime.tzinfo
  • datetime.fold

Instance methods:

  • datetime.date()

  • datetime.time()

  • datetime.timetz()

  • datetime.replace(year=self.year, month=self.month, day=self.day, hour=self.hour, minute=self.minute, second=self.second, microsecond=self.microsecond, tzinfo=self.tzinfo, * fold=0)

  • datetime.astimezone(tz=None)

  • datetime.utcoffset()

  • datetime.dst()

  • datetime.tzname()

  • datetime.timetuple()

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    
    >>> tt = dt.timetuple()
    >>> for it in tt:   
    ...     print(it)
    ...
    2006    # year
    11      # month
    21      # day
    16      # hour
    30      # minute
    0       # second
    1       # weekday (0 = Monday)
    325     # number of days since 1st January
    -1      # dst - method tzinfo.dst() returned None
    
  • datetime.utctimetuple()

  • datetime.toordinal()

  • datetime.timestamp()

  • datetime.weekday()

  • datetime.isoweekday()

  • datetime.isocalendar()

    1
    2
    3
    4
    5
    6
    7
    
    >>> ic = dt.isocalendar()
    >>> for it in ic:   
    ...     print(it)
    ...
    2006    # ISO year
    47      # ISO week
    2       # ISO weekday
    
  • datetime.isoformat(sep='T', timespec='auto')

  • datetime.__str__()

  • datetime.ctime()

  • datetime.strftime(format)

    1
    2
    
    >>> dt.strftime("%A, %d. %B %Y %I:%M%p")
    'Tuesday, 21. November 2006 04:30PM'
    
  • datetime.__format__(format)

    1
    2
    
    >>> 'The {1} is {0:%d}, the {2} is {0:%B}, the {3} is {0:%I:%M%p}.'.format(dt, "day", "month", "time")
    'The day is 21, the month is November, the time is 04:30PM.'
    
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
>>> from datetime import timedelta, datetime, tzinfo
>>> class GMT1(tzinfo):
...     def utcoffset(self, dt):
...         return timedelta(hours=1) + self.dst(dt)
...     def dst(self, dt):
...         # DST starts last Sunday in March
...         d = datetime(dt.year, 4, 1)   # ends last Sunday in October
...         self.dston = d - timedelta(days=d.weekday() + 1)
...         d = datetime(dt.year, 11, 1)
...         self.dstoff = d - timedelta(days=d.weekday() + 1)
...         if self.dston <=  dt.replace(tzinfo=None) < self.dstoff:
...             return timedelta(hours=1)
...         else:
...             return timedelta(0)
...     def tzname(self,dt):
...          return "GMT +1"
...
>>> class GMT2(tzinfo):
...     def utcoffset(self, dt):
...         return timedelta(hours=2) + self.dst(dt)
...     def dst(self, dt):
...         d = datetime(dt.year, 4, 1)
...         self.dston = d - timedelta(days=d.weekday() + 1)
...         d = datetime(dt.year, 11, 1)
...         self.dstoff = d - timedelta(days=d.weekday() + 1)
...         if self.dston <=  dt.replace(tzinfo=None) < self.dstoff:
...             return timedelta(hours=1)
...         else:
...             return timedelta(0)
...     def tzname(self,dt):
...         return "GMT +2"
...
>>> gmt1 = GMT1()
>>> # Daylight Saving Time
>>> dt1 = datetime(2006, 11, 21, 16, 30, tzinfo=gmt1)
>>> dt1.dst()
datetime.timedelta(0)
>>> dt1.utcoffset()
datetime.timedelta(0, 3600)
>>> dt2 = datetime(2006, 6, 14, 13, 0, tzinfo=gmt1)
>>> dt2.dst()
datetime.timedelta(0, 3600)
>>> dt2.utcoffset()
datetime.timedelta(0, 7200)
>>> # Convert datetime to another time zone
>>> dt3 = dt2.astimezone(GMT2())
>>> dt3     
datetime.datetime(2006, 6, 14, 14, 0, tzinfo=<GMT2 object at 0x...>)
>>> dt2     
datetime.datetime(2006, 6, 14, 13, 0, tzinfo=<GMT1 object at 0x...>)
>>> dt2.utctimetuple() == dt3.utctimetuple()
True

8.1.5. time Objects

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
>>> from datetime import time, tzinfo, timedelta
>>> class GMT1(tzinfo):
...     def utcoffset(self, dt):
...         return timedelta(hours=1)
...     def dst(self, dt):
...         return timedelta(0)
...     def tzname(self,dt):
...         return "Europe/Prague"
...
>>> t = time(12, 10, 30, tzinfo=GMT1())
>>> t                               
datetime.time(12, 10, 30, tzinfo=<GMT1 object at 0x...>)
>>> gmt = GMT1()
>>> t.isoformat()
'12:10:30+01:00'
>>> t.dst()
datetime.timedelta(0)
>>> t.tzname()
'Europe/Prague'
>>> t.strftime("%H:%M:%S %Z")
'12:10:30 Europe/Prague'
>>> 'The {} is {:%H:%M}.'.format("time", t)
'The time is 12:10.'

8.1.6. tzinfo Objects

8.1.7. timezone Objects

8.1.8. strftime() and strptime() Behavior

Directive Meaning Example
%a Weekday as locale’s abbreviated name. Sun, Mon, …, Sat (en_US);So, Mo, …, Sa (de_DE)
%A Weekday as locale’s full name. Sunday, Monday, …, Saturday (en_US);Sonntag, Montag, …, Samstag (de_DE)
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday. 0, 1, …, 6
%d Day of the month as a zero-padded decimal number. 01, 02, …, 31
%b Month as locale’s abbreviated name. Jan, Feb, …, Dec (en_US);Jan, Feb, …, Dez (de_DE)
%B Month as locale’s full name. January, February, …, December (en_US);Januar, Februar, …, Dezember (de_DE)
%m Month as a zero-padded decimal number. 01, 02, …, 12
%y Year without century as a zero-padded decimal number. 00, 01, …, 99
%Y Year with century as a decimal number. 0001, 0002, …, 2013, 2014, …, 9998, 9999
%H Hour (24-hour clock) as a zero-padded decimal number. 00, 01, …, 23
%I Hour (12-hour clock) as a zero-padded decimal number. 01, 02, …, 12
%p Locale’s equivalent of either AM or PM. AM, PM (en_US);am, pm (de_DE)
%M Minute as a zero-padded decimal number. 00, 01, …, 59
%S Second as a zero-padded decimal number. 00, 01, …, 59
%f Microsecond as a decimal number, zero-padded on the left. 000000, 000001, …, 999999
%z UTC offset in the form +HHMM or -HHMM (empty string if the object is naive). (empty), +0000, -0400, +1030
%Z Time zone name (empty string if the object is naive). (empty), UTC, EST, CST
%j Day of the year as a zero-padded decimal number. 001, 002, …, 366
%U Week number of the year (Sunday as the first day of the week) as a zero padded decimal number. All days in a new year preceding the first Sunday are considered to be in week 0. 00, 01, …, 53
%W Week number of the year (Monday as the first day of the week) as a decimal number. All days in a new year preceding the first Monday are considered to be in week 0. 00, 01, …, 53
%c Locale’s appropriate date and time representation. Tue Aug 16 21:30:00 1988 (en_US);Di 16 Aug 21:30:00 1988 (de_DE)
%x Locale’s appropriate date representation. 08/16/88 (None);08/16/1988 (en_US);16.08.1988 (de_DE)
%X Locale’s appropriate time representation. 21:30:00 (en_US);21:30:00 (de_DE)
%% A literal '%' character. %

8.3. collections — Container datatypes

8.3.1. ChainMap objects

class collections.ChainMap(*maps)

A ChainMap groups multiple dicts or other mappings together to create a single, updateable view. The underlying mappings are stored in a list.

1
2
import builtins
pylookup = ChainMap(locals(), globals(), vars(builtins))
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import os, argparse

defaults = {'color': 'red', 'user': 'guest'}

parser = argparse.ArgumentParser()
parser.add_argument('-u', '--user')
parser.add_argument('-c', '--color')
namespace = parser.parse_args()
command_line_args = {k:v for k, v in vars(namespace).items() if v}

combined = ChainMap(command_line_args, os.environ, defaults)
print(combined['color'])
print(combined['user'])
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
c = ChainMap()        # Create root context
d = c.new_child()     # Create nested child context
e = c.new_child()     # Child of c, independent from d
e.maps[0]             # Current context dictionary -- like Python's locals()
e.maps[-1]            # Root context -- like Python's globals()
e.parents             # Enclosing context chain -- like Python's nonlocals

d['x']                # Get first key in the chain of contexts
d['x'] = 1            # Set value in current context
del d['x']            # Delete from current context
list(d)               # All nested values
k in d                # Check all nested values
len(d)                # Number of nested values
d.items()             # All nested items
dict(d)               # Flatten into a regular dictionary

The ChainMap class only makes updates (writes and deletions) to the first mapping in the chain while lookups will search the full chain.

8.3.2. Counter objects

1
2
3
4
>>> c = Counter()                           # a new, empty counter
>>> c = Counter('gallahad')                 # a new counter from an iterable
>>> c = Counter({'red': 4, 'blue': 2})      # a new counter from a mapping
>>> c = Counter(cats=4, dogs=8)             # a new counter from keyword args

Counter objects return a zero count for missing items instead of raising a KeyError

1
2
>>> c['sausage'] = 0                        # counter entry with a zero count
>>> del c['sausage']                        # del actually removes the entry
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
>>> c = Counter(a=4, b=2, c=0, d=-2)
>>> sorted(c.elements())
['a', 'a', 'a', 'a', 'b', 'b']
>>> Counter('abracadabra').most_common(3)  
[('a', 5), ('r', 2), ('b', 2)]
>>> c = Counter(a=4, b=2, c=0, d=-2)
>>> d = Counter(a=1, b=2, c=3, d=4)
>>> c.subtract(d)
>>> c
Counter({'a': 3, 'b': 0, 'c': -3, 'd': -6})
>>> c.update(d)
>>> c
Counter({'a': 4, 'b': 2, 'c': 0, 'd': -2})
1
2
3
4
5
6
7
8
9
sum(c.values())                 # total of all counts
c.clear()                       # reset all counts
list(c)                         # list unique elements
set(c)                          # convert to a set
dict(c)                         # convert to a regular dictionary
c.items()                       # convert to a list of (elem, cnt) pairs
Counter(dict(list_of_pairs))    # convert from a list of (elem, cnt) pairs
c.most_common()[:-n-1:-1]       # n least common elements
+c                              # remove zero and negative counts

8.3.3. deque objects

Once a bounded length deque is full, when new items are added, a corresponding number of items are discarded from the opposite end.  They are also useful for tracking transactions and other pools of data where only the most recent activity is of interest.

  • append(x)
1
2
3
>>> from collections import deque
>>> d = deque('ghi')                 # make a new deque with three items
>>> d.append('j')                    # add a new entry to the right side
  • appendleft(x)
1
>>> d.appendleft('f')                # add a new entry to the left side
  • clear()
1
>>> d.clear()                        # empty the deque
  • copy()

  • count(x)

  • extend(iterable)

1
2
3
>>> d.extend('jkl')                  # add multiple elements at once
>>> d
deque(['j', 'k', 'l'])
  • extendleft(iterable)
1
2
3
>>> d.extendleft('abc')              # extendleft() reverses the input order
>>> d
deque(['c', 'b', 'a', 'j', 'k', 'l'])
  • index(x[, start[, stop]])
1
2
3
4
>>> d[0]                             # peek at leftmost item
'c'
>>> d[-1]                            # peek at rightmost item
'l'
  • insert(i, x)
1
2
3
>>> d.insert(0, 'd')
>>> d
deque(['d', 'c', 'b', 'a', 'j', 'k', 'l'])
  • pop()
1
2
>>> d.pop()                          # return and remove the rightmost item or raises an IndexError
'l'
  • popleft()
1
2
>>> d.popleft()                      # return and remove the leftmost item or raises an IndexError
'd'
  • remove(value)
1
2
3
>>> d.remove('c')
>>> d
deque(['b', 'a', 'j', 'k'])
  • reverse()
1
2
>>> list(reversed(d))                # list the contents of a deque in reverse in-place and then return None.
['k', 'j', 'a', 'b']
  • rotate(n=1)

Rotate the deque n steps to the right. If n is negative, rotate to the left.

When the deque is not empty, rotating one step to the right is equivalent to d.appendleft(d.pop()), and rotating one step to the left is equivalent to d.append(d.popleft()).

1
2
3
4
5
6
>>> d.rotate(1)                      # right rotation
>>> d
deque(['k', 'b', 'a', 'j'])
>>> d.rotate(-1)                     # left rotation
>>> d
deque(['b', 'a', 'j', 'k'])

Deque objects also provide one read-only attribute:

  • maxlen

8.3.4. defaultdict objects

  • class collections.defaultdict([default_factory[, ...]])

Using list as the default_factory, it is easy to group a sequence of key-value pairs into a dictionary of lists:

1
2
3
4
5
6
7
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
...     d[k].append(v)
...
>>> sorted(d.items())
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]

Setting the default_factory to int makes the defaultdict useful for counting (like a bag or multiset in other languages):

1
2
3
4
5
6
7
>>> s = 'mississippi'
>>> d = defaultdict(int)
>>> for k in s:
...     d[k] += 1
...
>>> sorted(d.items())
[('i', 4), ('m', 1), ('p', 2), ('s', 4)]

A faster and more flexible way to create constant functions is to use a lambda function which can supply any constant value

1
2
3
4
5
6
>>> def constant_factory(value):
...     return lambda: value
>>> d = defaultdict(constant_factory('<missing>'))
>>> d.update(name='John', action='ran')
>>> '%(name)s %(action)s to %(object)s' % d
'John ran to <missing>'

Setting the default_factory to set makes the defaultdict useful for building a dictionary of sets:

1
2
3
4
5
6
7
>>> s = [('red', 1), ('blue', 2), ('red', 3), ('blue', 4), ('red', 1), ('blue', 4)]
>>> d = defaultdict(set)
>>> for k, v in s:
...     d[k].add(v)
...
>>> sorted(d.items())
[('blue', {2, 4}), ('red', {1, 3})]

8.3.5. namedtuple() Factory Function for Tuples with Named Fields

  • collections.namedtuple(typename, field_names, *, verbose=False, rename=False, module=None)

Named tuples assign meaning to each position in a tuple and allow for more readable, self-documenting code.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
>>> # Basic example
>>> Point = namedtuple('Point', ['x', 'y'])
>>> p = Point(11, y=22)     # instantiate with positional or keyword arguments
>>> p[0] + p[1]             # indexable like the plain tuple (11, 22)
33
>>> x, y = p                # unpack like a regular tuple
>>> x, y
(11, 22)
>>> p.x + p.y               # fields also accessible by name
33
>>> p                       # readable __repr__ with a name=value style
Point(x=11, y=22)

Named tuples are especially useful for assigning field names to result tuples returned by the csv or sqlite3 modules:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')

import csv
# somenamedtuple._make(iterable) makes a new instance
for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))):
    print(emp.name, emp.title)

import sqlite3
conn = sqlite3.connect('/companydata')
cursor = conn.cursor()
cursor.execute('SELECT name, age, title, department, paygrade FROM employees')
for emp in map(EmployeeRecord._make, cursor.fetchall()):
    print(emp.name, emp.title)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
>>> class Point(namedtuple('Point', ['x', 'y'])):
...     __slots__ = ()
...     @property
...     def hypot(self):
...         return (self.x ** 2 + self.y ** 2) ** 0.5
...     def __str__(self):
...         return 'Point: x=%6.3f  y=%6.3f  hypot=%6.3f' % (self.x, self.y, self.hypot)

>>> for p in Point(3, 4), Point(14, 5/7):
...     print(p)
Point: x= 3.000  y= 4.000  hypot= 5.000
Point: x=14.000  y= 0.714  hypot=14.018

8.3.6. OrderedDict objects

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
>>> # regular unsorted dictionary
>>> d = {'banana': 3, 'apple': 4, 'pear': 1, 'orange': 2}

>>> # dictionary sorted by key
>>> OrderedDict(sorted(d.items(), key=lambda t: t[0]))
OrderedDict([('apple', 4), ('banana', 3), ('orange', 2), ('pear', 1)])

>>> # dictionary sorted by value
>>> OrderedDict(sorted(d.items(), key=lambda t: t[1]))
OrderedDict([('pear', 1), ('orange', 2), ('banana', 3), ('apple', 4)])

>>> # dictionary sorted by length of the key string
>>> OrderedDict(sorted(d.items(), key=lambda t: len(t[0])))
OrderedDict([('pear', 1), ('apple', 4), ('orange', 2), ('banana', 3)])

8.3.7. UserDict objects

8.3.8. UserList objects

8.3.9. UserString objects

8.4. collections.abc — Abstract Base Classes for Containers

8.5. heapq — Heap queue algorithm

8.6. bisect — Array bisection algorithm

8.7. array — Efficient arrays of numeric values

8.8. weakref — Weak references

8.9. types — Dynamic type creation and names for built-in types

8.10. copy — Shallow and deep copy operations

  • copy.copy(x)
  • copy.deepcopy(x)

8.11. pprint — Data pretty printer

8.12. reprlib — Alternate repr() implementation

8.13. enum — Support for enumerations

9. Numeric and Mathematical Modules

9.1. numbers — Numeric abstract base classes

9.2. math — Mathematical functions

9.3. cmath — Mathematical functions for complex numbers

9.4. decimal — Decimal fixed point and floating point arithmetic

9.5. fractions — Rational numbers

9.6. random — Generate pseudo-random numbers

9.6.1. Bookkeeping functions

  • random.seed(a=None, version=2)

  • random.getstate()

    Return an object capturing the current internal state of the generator. 

  • random.setstate(state)

    state should have been obtained from a previous call to getstate(), and setstate() restores the internal state of the generator to what it was at the time getstate() was called.

  • random.getrandbits(k)

9.6.2. Functions for integers

  • random.randrange(stop)

    1
    2
    
    >>> randrange(10)        # Integer from 0 to 9 inclusive
    7
    
  • random.randrange(start, stop[, step])

    1
    2
    
    >>> randrange(0, 101, 2)      # Even integer from 0 to 100 inclusive
    26
    
  • random.randint(a, b) Alias for randrange(a, b+1)

9.6.3. Functions for sequences

  • random.choice(seq)

    1
    2
    3
    
    # Single random element from a sequence
    >>> choice(['win', 'lose', 'draw'])      
    'draw'
    
  • random.choices(population, weights=None, *, cum_weights=None, k=1)

    Return a k sized list of elements chosen from the population with replacement

    1
    2
    3
    
    # Six roulette wheel spins (weighted sampling with replacement)
    >>> choices(['red', 'black', 'green'], [18, 18, 2], k=6)  
    ['red', 'green', 'black', 'black', 'red', 'black'] 
    
  • random.shuffle(x[, random])

    Shuffle the sequence x in place.

    1
    2
    3
    4
    
    >>> origin = [1, 2, 3, 4, 5, 6]
    >>> random.shuffle(origin)       # Shuffle a list
    >>> origin
    [4, 1, 5, 6, 3, 2]
    
  • random.sample(population, k)

    Return a k length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement.

    1
    2
    3
    
    # Three samples without replacement
    >>> random.sample([1, 2, 3, 4, 5, 6], k=3)
    [2, 3, 1
    

9.6.4. Real-valued distributions

  • random.random()

    1
    2
    
    >>> random()                  # Random float:  0.0 <= x < 1.0
    0.37444887175646646
    
  • random.uniform(a, b)

    1
    2
    
    >>> uniform(2.5, 10.0)       # Random float:  2.5 <= x < 10.0
    3.1800146073117523
    
  • random.triangular(low, high, mode)

  • random.betavariate(alpha, beta)

  • random.expovariate(lambd)

    1
    2
    3
    
    # Interval between arrivals averaging 5 seconds
    >>> expovariate(1 / 5)    
    5.148957571865031
    
  • random.gammavariate(alpha, beta)

  • random.gauss(mu, sigma)

  • random.lognormvariate(mu, sigma)

  • random.normalvariate(mu, sigma)

  • random.vonmisesvariate(mu, kappa)

  • random.paretovariate(alpha)

  • random.weibullvariate(alpha, beta)

9.7. statistics — Mathematical statistics functions

10. Functional Programming Modules

10.1. itertools — Functions creating iterators for efficient looping

Infinite iterators:

  • itertools.count(start=0, step=1)

    1
    
    count(10) --> 10 11 12 13 14 ...
    
  • itertools.cycle(iterable)

    1
    
    cycle('ABCD') --> A B C D A B C D ...
    
  • itertools.repeat(object[, times])

    1
    
    repeat(10, 3) --> 10 10 10
    

Iterators terminating on the shortest input sequence:

  • itertools.accumulate(iterable[, func])

    1
    
    accumulate([1,2,3,4,5]) --> 1 3 6 10 15
    
  • itertools.chain(*iterables)

    • classmethod chain.from_iterable(iterable)
    1
    2
    
    chain('ABC', 'DEF') --> A B C D E F
    chain.from_iterable(['ABC', 'DEF']) --> A B C D E F
    
  • itertools.compress(data, selectors)

    1
    
    compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
    
  • itertools.dropwhile(predicate, iterable)

    1
    
    dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
    
  • itertools.filterfalse(predicate, iterable)

    1
    
    filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
    
  • itertools.groupby(iterable, key=None)

  • itertools.islice(iterable, stop)

  • itertools.islice(iterable, start, stop[, step])

    1
    
    islice('ABCDEFG', 2, None) --> C D E F G
    
  • itertools.starmap(function, iterable)

    1
    
    starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
    
  • itertools.takewhile(predicate, iterable)

    1
    2
    
    # until pred fails
    takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
    
  • itertools.tee(iterable, n=2)

  • itertools.zip_longest(*iterables, fillvalue=None)

    1
    
    zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
    

Combinatoric iterators:

  • itertools.product(*iterables, repeat=1)

    1
    2
    
    product('ABCD', repeat=2)
    --> AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD
    
  • itertools.permutations(iterable, r=None)

    1
    2
    
    permutations('ABCD', 2)
    --> AB AC AD BA BC BD CA CB CD DA DB DC
    
  • itertools.combinations(iterable, r)

    1
    2
    
    combinations('ABCD', 2)
    --> AB AC AD BC BD CD
    
  • itertools.combinations_with_replacement(iterable, r)

    1
    2
    
    combinations_with_replacement('ABCD', 2)
    --> AA AB AC AD BB BC BD CC CD DD
    

10.2. functools — Higher-order functions and operations on callable objects

  • functools.cmp_to_key(func)

    Transform an old-style comparison function to a key function.

    A comparison function is any callable that accept two arguments, compares them, and returns a negative number for less-than, zero for equality, or a positive number for greater-than.

    1
    
    sorted(iterable, key=cmp_to_key(locale.strcoll))
    
  • @functools.lru_cache(maxsize=128, typed=False)

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    
    @lru_cache(maxsize=None)
    def fib(n):
        if n < 2:
            return n
        return fib(n-1) + fib(n-2)
    
    >>> [fib(n) for n in range(16)]
    [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
    
    >>> fib.cache_info()
    CacheInfo(hits=28, misses=16, maxsize=None, currsize=16)
    
  • @functools.total_ordering

    Given a class defining one or more rich comparison ordering methods, this class decorator supplies the rest. 

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    
    @total_ordering
    class Student:
        def _is_valid_operand(self, other):
            return (hasattr(other, "lastname") and
                    hasattr(other, "firstname"))
        def __eq__(self, other):
            if not self._is_valid_operand(other):
                return NotImplemented
            return ((self.lastname.lower(), self.firstname.lower()) ==
                    (other.lastname.lower(), other.firstname.lower()))
        def __lt__(self, other):
            if not self._is_valid_operand(other):
                return NotImplemented
            return ((self.lastname.lower(), self.firstname.lower()) <
                    (other.lastname.lower(), other.firstname.lower()))
    
  • functools.partial(func, *args, **keywords)

    1
    2
    3
    4
    5
    6
    
    >>> def three_sum(a, b, c):
    ...     return a + b + c
    ... 
    >>> add_two = partial(three_sum, 2, 0)
    >>> add_two(3)
    5
    
  • class functools.partialmethod(func, *args, **keywords)

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    
    >>> class Cell(object):
    ...     def __init__(self):
    ...         self._alive = False
    ...     @property
    ...     def alive(self):
    ...         return self._alive
    ...     def set_state(self, state):
    ...         self._alive = bool(state)
    ...     set_alive = partialmethod(set_state, True)
    ...     set_dead = partialmethod(set_state, False)
    ...
    >>> c = Cell()
    >>> c.alive
    False
    >>> c.set_alive()
    >>> c.alive
    True
    
  • functools.reduce(function, iterable[, initializer])

    1
    2
    
    >>> reduce(lambda x, y: x+y, [1, 2, 3, 4, 5])
    ((((1+2)+3)+4)+5)
    
  • @functools.singledispatch

    Transform a function into a single-dispatch generic function.

    1
    2
    3
    4
    5
    6
    
    >>> from functools import singledispatch
    >>> @singledispatch
    ... def fun(arg, verbose=False):
    ...     if verbose:
    ...         print("Let me just say,", end=" ")
    ...     print(arg)
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    
    >>> @fun.register(int)
    ... def _(arg, verbose=False):
    ...     if verbose:
    ...         print("Strength in numbers, eh?", end=" ")
    ...     print(arg)
    ...
    >>> @fun.register(list)
    ... def _(arg, verbose=False):
    ...     if verbose:
    ...         print("Enumerate this:")
    ...     for i, elem in enumerate(arg):
    ...         print(i, elem)
    
    1
    2
    3
    4
    
    >>> def nothing(arg, verbose=False):
    ...     print("Nothing.")
    ...
    >>> fun.register(type(None), nothing)
    
    1
    2
    3
    4
    5
    6
    
    >>> @fun.register(float)
    ... @fun.register(Decimal)
    ... def fun_num(arg, verbose=False):
    ...     if verbose:
    ...         print("Half of your number:", end=" ")
    ...     print(arg / 2)
    
    1
    2
    3
    4
    5
    6
    7
    8
    
    >>> fun.registry.keys()
    dict_keys([<class 'NoneType'>, <class 'int'>, <class 'object'>,
              <class 'decimal.Decimal'>, <class 'list'>,
              <class 'float'>])
    >>> fun.registry[float]
    <function fun_num at 0x1035a2840>
    >>> fun.registry[object]
    <function fun at 0x103fe0000>
    
  • functools.update_wrapper(wrapper, wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)

  • @functools.wraps(wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    >>> from functools import wraps
    >>> def my_decorator(f):
    ...     @wraps(f)
    ...     def wrapper(*args, **kwds):
    ...         print('Calling decorated function')
    ...         return f(*args, **kwds)
    ...     return wrapper
    ...
    >>> @my_decorator
    ... def example():
    ...     """Docstring"""
    ...     print('Called example function')
    ...
    >>> example()
    Calling decorated function
    Called example function
    >>> example.__name__
    'example'
    >>> example.__doc__
    'Docstring'
    

10.3. operator — Standard operators as functions

operator.lt(a, b) operator.mul(a, b)
operator.le(a, b) operator.matmul(a, b)
operator.eq(a, b) operator.neg(obj)
operator.ne(a, b) operator.or_(a, b)
operator.ge(a, b) operator.pos(obj)
operator.gt(a, b) operator.pow(a, b)
operator.not_(obj) operator.pow(a, b)
operator.truth(obj) operator.rshift(a, b)
operator.is_(a, b) operator.sub(a, b)
operator.is_not(a, b) operator.truediv(a, b)
operator.abs(obj) operator.xor(a, b)
operator.add(a, b) operator.concat(a, b)
operator.and_(a, b) operator.contains(a, b)
operator.floordiv(a, b) operator.countOf(a, b)
operator.index(a) operator.delitem(a, b)
operator.inv(obj) operator.getitem(a, b)
operator.invert(obj) operator.indexOf(a, b)
operator.lshift(a, b) operator.setitem(a, b, c)
operator.mod(a, b) operator.length_hint(obj, default=0)
operator.attrgetter(attr) operator.itemgetter(item)

11. File and Directory Access

11.1. pathlib — Object-oriented filesystem paths

11.2. os.path — Common pathname manipulations

11.3. fileinput — Iterate over lines from multiple input streams

11.4. stat — Interpreting stat() results

11.5. filecmp — File and Directory Comparisons

11.6. tempfile — Generate temporary files and directories

11.7. glob — Unix style pathname pattern expansion

11.8. fnmatch — Unix filename pattern matching

11.9. linecache — Random access to text lines

11.10. shutil — High-level file operations

11.11. macpath — Mac OS 9 path manipulation functions

12. Data Persistence

12.1. pickle — Python object serialization

12.2. copyreg — Register pickle support functions

12.3. shelve — Python object persistence

12.4. marshal — Internal Python object serialization

12.5. dbm — Interfaces to Unix “databases”

12.6. sqlite3 — DB-API 2.0 interface for SQLite databases

13. Data Compression and Archiving

13.1. zlib — Compression compatible with gzip

13.2. gzip — Support for gzip files

13.3. bz2 — Support for bzip2 compression

13.4. lzma — Compression using the LZMA algorithm

13.5. zipfile — Work with ZIP archives

13.6. tarfile — Read and write tar archive files

14. File Formats

14.1. csv — CSV File Reading and Writing

14.2. configparser — Configuration file parser

14.3. netrc — netrc file processing

14.4. xdrlib — Encode and decode XDR data

14.5. plistlib — Generate and parse Mac OS X .plist files

15. Cryptographic Services

15.1. hashlib — Secure hashes and message digests

15.2. hashlib — BLAKE2 hash functions

15.3. Module

15.4. Examples

15.5. Credits

15.6. hmac — Keyed-Hashing for Message Authentication

15.7. secrets — Generate secure random numbers for managing secrets

16. Generic Operating System Services

16.1. os — Miscellaneous operating system interfaces

16.2. io — Core tools for working with streams

16.3. time — Time access and conversions

16.4. argparse — Parser for command-line options, arguments and sub-commands

16.5. getopt — C-style parser for command line options

16.6. logging — Logging facility for Python

16.7. logging.config — Logging configuration

16.8. logging.handlers — Logging handlers

16.9. getpass — Portable password input

16.10. curses — Terminal handling for character-cell displays

16.11. curses.textpad — Text input widget for curses programs

16.12. curses.ascii — Utilities for ASCII characters

16.13. curses.panel — A panel stack extension for curses

16.14. platform — Access to underlying platform’s identifying data

16.15. errno — Standard errno system symbols

16.16. ctypes — A foreign function library for Python

17. Concurrent Execution

17.1. threading — Thread-based parallelism

17.2. multiprocessing — Process-based parallelism

17.3. The concurrent package

17.4. concurrent.futures — Launching parallel tasks

17.5. subprocess — Subprocess management

17.6. sched — Event scheduler

17.7. queue — A synchronized queue class

17.8. dummy_threading — Drop-in replacement for the threading module

17.9. _thread — Low-level threading API

17.10. _dummy_thread — Drop-in replacement for the _thread module

18. Interprocess Communication and Networking

18.1. socket — Low-level networking interface

18.2. ssl — TLS/SSL wrapper for socket objects

18.3. select — Waiting for I/O completion

18.4. selectors — High-level I/O multiplexing

18.5. asyncio — Asynchronous I/O, event loop, coroutines and tasks

18.6. asyncore — Asynchronous socket handler

18.7. asynchat — Asynchronous socket command/response handler

18.8. signal — Set handlers for asynchronous events

18.9. mmap — Memory-mapped file support

19. Internet Data Handling

19.1. email — An email and MIME handling package

19.2. json — JSON encoder and decoder

19.3. mailcap — Mailcap file handling

19.4. mailbox — Manipulate mailboxes in various formats

19.5. mimetypes — Map filenames to MIME types

19.6. base64 — Base16, Base32, Base64, Base85 Data Encodings

19.7. binhex — Encode and decode binhex4 files

19.8. binascii — Convert between binary and ASCII

19.9. quopri — Encode and decode MIME quoted-printable data

19.10. uu — Encode and decode uuencode files

20. Structured Markup Processing Tools

20.1. html — HyperText Markup Language support

20.2. html.parser — Simple HTML and XHTML parser

20.3. html.entities — Definitions of HTML general entities

20.4. XML Processing Modules

20.5. xml.etree.ElementTree — The ElementTree XML API

20.6. xml.dom — The Document Object Model API

20.7. xml.dom.minidom — Minimal DOM implementation

20.8. xml.dom.pulldom — Support for building partial DOM trees

20.9. xml.sax — Support for SAX2 parsers

20.10. xml.sax.handler — Base classes for SAX handlers

20.11. xml.sax.saxutils — SAX Utilities

20.12. xml.sax.xmlreader — Interface for XML parsers

20.13. xml.parsers.expat — Fast XML parsing using Expat

21. Internet Protocols and Support

21.1. webbrowser — Convenient Web-browser controller

21.2. cgi — Common Gateway Interface support

21.3. cgitb — Traceback manager for CGI scripts

21.4. wsgiref — WSGI Utilities and Reference Implementation

21.5. urllib — URL handling modules

21.6. urllib.request — Extensible library for opening URLs

21.7. urllib.response — Response classes used by urllib

21.8. urllib.parse — Parse URLs into components

21.9. urllib.error — Exception classes raised by urllib.request

21.10. urllib.robotparser — Parser for robots.txt

21.11. http — HTTP modules

21.12. http.client — HTTP protocol client

21.13. ftplib — FTP protocol client

21.14. poplib — POP3 protocol client

21.15. imaplib — IMAP4 protocol client

21.16. nntplib — NNTP protocol client

21.17. smtplib — SMTP protocol client

21.18. smtpd — SMTP Server

21.19. telnetlib — Telnet client

21.20. uuid — UUID objects according to RFC 4122

21.21. socketserver — A framework for network servers

21.22. http.server — HTTP servers

21.23. http.cookies — HTTP state management

21.25. xmlrpc — XMLRPC server and client modules

21.26. xmlrpc.client — XML-RPC client access

21.27. xmlrpc.server — Basic XML-RPC servers

21.28. ipaddress — IPv4/IPv6 manipulation library

22. Multimedia Services

22.1. audioop — Manipulate raw audio data

22.2. aifc — Read and write AIFF and AIFC files

22.3. sunau — Read and write Sun AU files

22.4. wave — Read and write WAV files

22.5. chunk — Read IFF chunked data

22.6. colorsys — Conversions between color systems

22.7. imghdr — Determine the type of an image

22.8. sndhdr — Determine type of sound file

22.9. ossaudiodev — Access to OSS-compatible audio devices

23. Internationalization

23.1. gettext — Multilingual internationalization services

23.2. locale — Internationalization services

24. Program Frameworks

24.1. turtle — Turtle graphics

24.2. cmd — Support for line-oriented command interpreters

24.3. shlex — Simple lexical analysis

  1. Graphical User Interfaces with Tk

25.1. tkinter — Python interface to Tcl/Tk

25.2. tkinter.ttk — Tk themed widgets

25.3. tkinter.tix — Extension widgets for Tk

25.4. tkinter.scrolledtext — Scrolled Text Widget

25.5. IDLE

25.6. Other Graphical User Interface Packages

26. Development Tools

26.1. typing — Support for type hints

26.2. pydoc — Documentation generator and online help system

26.3. doctest — Test interactive Python examples

26.4. unittest — Unit testing framework

26.5. unittest.mock — mock object library

26.6. unittest.mock — getting started

26.7. 2to3 - Automated Python 2 to 3 code translation

26.8. test — Regression tests package for Python

26.9. test.support — Utilities for the Python test suite

27. Debugging and Profiling

27.1. bdb — Debugger framework

27.2. faulthandler — Dump the Python traceback

27.3. pdb — The Python Debugger

27.4. The Python Profilers

27.5. timeit — Measure execution time of small code snippets

27.6. trace — Trace or track Python statement execution

27.7. tracemalloc — Trace memory allocations

28. Software Packaging and Distribution

28.1. distutils — Building and installing Python modules

28.2. ensurepip — Bootstrapping the pip installer

28.3. venv — Creation of virtual environments

28.4. zipapp — Manage executable python zip archives

29. Python Runtime Services

29.1. sys — System-specific parameters and functions

29.2. sysconfig — Provide access to Python’s configuration information

29.3. builtins — Built-in objects

29.4. main — Top-level script environment

29.5. warnings — Warning control

29.6. contextlib — Utilities for with-statement contexts

29.7. abc — Abstract Base Classes

29.8. atexit — Exit handlers

29.9. traceback — Print or retrieve a stack traceback

29.10. future — Future statement definitions

29.11. gc — Garbage Collector interface

29.12. inspect — Inspect live objects

29.13. site — Site-specific configuration hook

29.14. fpectl — Floating point exception control

30. Custom Python Interpreters

30.1. code — Interpreter base classes

30.2. codeop — Compile Python code

31. Importing Modules

31.1. zipimport — Import modules from Zip archives

31.2. pkgutil — Package extension utility

31.3. modulefinder — Find modules used by a script

31.4. runpy — Locating and executing Python modules

31.5. importlib — The implementation of import

32. Python Language Services

32.1. parser — Access Python parse trees

32.2. ast — Abstract Syntax Trees

32.3. symtable — Access to the compiler’s symbol tables

32.4. symbol — Constants used with Python parse trees

32.5. token — Constants used with Python parse trees

32.6. keyword — Testing for Python keywords

32.7. tokenize — Tokenizer for Python source

32.8. tabnanny — Detection of ambiguous indentation

32.9. pyclbr — Python class browser support

32.10. py_compile — Compile Python source files

32.11. compileall — Byte-compile Python libraries

32.12. dis — Disassembler for Python bytecode

32.13. pickletools — Tools for pickle developers

33. Miscellaneous Services

33.1. formatter — Generic output formatting

34. MS Windows Specific Services

34.1. msilib — Read and write Microsoft Installer files

34.2. msvcrt — Useful routines from the MS VC++ runtime

34.3. winreg — Windows registry access

34.4. winsound — Sound-playing interface for Windows

35. Unix Specific Services

35.1. posix — The most common POSIX system calls

35.2. pwd — The password database

35.3. spwd — The shadow password database

35.4. grp — The group database

35.5. crypt — Function to check Unix passwords

35.6. termios — POSIX style tty control

35.7. tty — Terminal control functions

35.8. pty — Pseudo-terminal utilities

35.9. fcntl — The fcntl and ioctl system calls

35.10. pipes — Interface to shell pipelines

35.11. resource — Resource usage information

35.12. nis — Interface to Sun’s NIS (Yellow Pages)

35.13. syslog — Unix syslog library routines

36. Superseded Modules

36.1. optparse — Parser for command line options

36.2. imp — Access the import internals

37. Undocumented Modules

37.1. Platform specific modules