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:
- os: For interacting with the operating system, such as file and directory manipulation.
- sys: Useful for system-specific parameters and functions.
- math: Provides mathematical functions and constants.
- datetime: For working with dates and times.
- collections: Contains data structures like lists, dictionaries, and sets.
- json: To work with JSON data.
- csv: For reading and writing CSV files.
- urllib: For making HTTP requests.
- re: Regular expressions for pattern matching.
- random: For generating random numbers.
- sqlite3: To work with SQLite databases.
- logging: Used for event logging in your applications.
- argparse: For parsing command-line arguments.
- 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]) |
-
@classmethodA class method receives the class as implicit first argument, just like an instance method receives the instance.
1 2 3class C: @classmethod def f(cls, arg1, arg2, ...): ...It can be called either on the class (such as
C.f()) or on an instance (such asC().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 3with 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 16class 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 age1 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)) -
@staticmethodA static method does not receive an implicit first argument.
1 2 3class 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++.
@classmethodfor 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 usingitertools.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.
- super can be used to refer to parent classes without naming them explicitly with single inheritance.
- 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 assuper(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) Trueitertools.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:
NoneandFalse. - 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 infinity: 1//2 is 0, (-1)//2 is -1, 1//(-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
StopIterationexception.
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.
|
|
|
|
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
strobjects, you can build a list and use str.join() at the end or else write to anio.StringIOinstance and retrieve its value when complete - if concatenating
bytesobjects, you can similarly use bytes.join() orio.BytesIO, or you can do in-place concatenation with abytearrayobject.bytearrayobjects are mutable and have an efficient overallocation mechanism - if concatenating
tupleobjects, extend a list instead - for other types,
investigatethe 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=None, reverse=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(object, encoding=‘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 tolower() -
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
mappingis adictsubclass. -
str.index(sub[, start[, end]])Like
find(), but raiseValueErrorwhen 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
-1on failure. -
str.rindex(sub[, start[, end]])Like
rfind()but raisesValueErrorwhen 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 tolen(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'
|
|
Since bytes objects are sequences of integers (akin to a tuple), for a bytes object b, b[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]]])
|
|
Since bytearray objects are sequences of integers (akin to a list), for a bytearray object b, b[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.
|
|
-
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
inoperator: -
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)
|
|
-
static bytes.maketrans(from, to) -
static bytearray.maketrans(from, to)
|
|
-
bytes.partition(sep) -
bytearray.partition(sep)
|
|
-
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 raisesValueErrorwhen 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.
|
|
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])
|
|
The original sequence is returned if width is less than or equal to len(s).
bytes.ljust(width[, fillbyte])bytearray.ljust(width[, fillbyte])
|
|
For bytes objects, the original sequence is returned if width is less than or equal to len(s).
bytes.lstrip([chars])bytearray.lstrip([chars])
|
|
If omitted or None, the chars argument defaults to removing ASCII whitespace.
bytes.rjust(width[, fillbyte])bytearray.rjust(width[, fillbyte])
|
|
bytes.rsplit(sep=None, maxsplit=-1)bytearray.rsplit(sep=None, maxsplit=-1)
|
|
bytes.rstrip([chars])bytearray.rstrip([chars])
|
|
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.
|
|
If sep is not specified or is None, runs of consecutive ASCII whitespace are regarded as a single separator
|
|
bytes.strip([chars])bytearray.strip([chars])
|
|
bytes.capitalize()bytearray.capitalize()
|
|
bytes.expandtabs(tabsize=8)bytearray.expandtabs(tabsize=8)
|
|
bytes.isalnum()bytearray.isalnum()
|
|
bytes.isalpha()bytearray.isalpha()
|
|
bytes.isdigit()bytearray.isdigit()
|
|
bytes.islower()bytearray.islower()
|
|
bytes.isspace()bytearray.isspace()
|
|
bytes.istitle()bytearray.istitle()
|
|
bytes.isupper()bytearray.isupper()
|
|
bytes.lower()bytearray.lower()
|
|
bytes.splitlines(keepends=False)bytearray.splitlines(keepends=False)
|
|
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:
|
|
bytes.swapcase()bytearray.swapcase()
|
|
bytes.title()bytearray.title()
|
|
|
|
A workaround for apostrophes can be constructed using regular expressions:
|
|
bytes.upper()bytearray.upper()
|
|
bytes.zfill(width)bytearray.zfill(width)
|
|
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
Trueif the set has no elements in common with other. -
issubset(other) -
set <= otherTest whether every element in the set is in other.
-
set < otherTest whether the set is a proper subset of other, that is,
set <=other and set != other. -
issuperset(other) -
set >= otherTest whether every element in other is in the set.
-
set > otherTest 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 ^ otherReturn 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 ^= otherUpdate the set, keeping only elements found in either set, but not in both.
-
add(elem) -
remove(elem)RaisesKeyErrorif 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
KeyErrorif the set is empty. -
clear()
4.10. Mapping Types — dict
class dict(**kwarg)
class dict(mapping, **kwarg)
class dict(iterable, **kwarg)
|
|
len(d)
d[key]
-
d[key] = valueRaises aKeyErrorif key is not in the map.__missing__() -
del d[key]Raises aKeyErrorif 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
KeyErroris 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
AssertionErrorRaised when an
assertstatement fails. -
exception
AttributeErrorRaised when an attribute reference or assignment fails.
-
exception
EOFErrorRaised when the input() function hits an end-of-file condition (EOF) without reading any data.
-
exception
ImportErrorRaised when the
importstatement has troubles trying to load a module. Also raised when the “from list” infrom ... importhas a name that cannot be found. -
exception
IndexErrorRaised when a sequence subscript is out of range.
-
exception
KeyErrorRaised when a mapping (dictionary) key is not found in the set of existing keys.
-
exception
KeyboardInterruptRaised when the user hits the interrupt key (normally
Control-CorDelete). -
exception
NotImplementedErrorIn 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
|
|
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:
|
|
Accessing arguments by name:
|
|
Accessing arguments’ attributes:
|
|
Accessing arguments’ items:
|
|
Replacing %s and %r:
|
|
Aligning the text and specifying a width:
|
|
Replacing %+f, %-f, and % f and specifying a sign:
|
|
Using the comma as a thousands separator:
|
|
Expressing a percentage:
|
|
Using type-specific formatting:
|
|
Nesting arguments and more complex examples:
|
|
6.1.4. Template strings
|
|
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 2prog = re.compile(pattern) result = prog.match(string)is equivalent to
1result = re.match(pattern, string) -
re.A -
re.ASCII -
re.DEBUG -
re.I -
re.IGNORECASE -
re.L -
re.LOCALE -
re.M -
re.MULTILINEWhen 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.DOTALLwithout this flag,
'.'will match anything except a newline. -
re.X -
re.VERBOSE1 2 3a = re.compile(r"""\d + # the integral part \. # the decimal point \d * # some fractional digits""", re.X)equal to
1b = 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 withsearch()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
|
|
-
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.
|
|
|
|
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.dateAttributes:
year,month, andday. -
class datetime.timeAttributes:
hour,minute,second,microsecond, andtzinfo. -
class datetime.datetimeAttributes:
year,month, andday,hour,minute,second,microsecond, andtzinfo. -
class datetime.timedeltaA duration expressing the difference between two
date,time, ordatetimeinstances to microsecond resolution. -
class datetime.tzinfoThese are used by the
datetimeandtimeclasses 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):
dayssecondsmicrosecondsInstance 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.yeardate.monthdate.day
Supported operations:
date2 = date1 + timedeltadate2 = date1 - timedeltatimedelta = date1 - date2date1 < 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 2date(2003, 12, 29).isocalendar() == (2004, 1, 1) date(2004, 1, 4).isocalendar() == (2004, 1, 7) -
date.isoformat()1date(2002, 12, 4).isoformat() == '2002-12-04' -
date.__str__()For a date d,
str(d)is equivalent tod.isoformat(). -
date.ctime()1date(2002, 12, 4).ctime() == 'Wed Dec 4 00:00:00 2002'1d.ctime() == time.ctime(time.mktime(d.timetuple())) -
date.strftime(format) -
date.__format__(format)
|
|
|
|
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.yeardatetime.monthdatetime.daydatetime.hourdatetime.minutedatetime.seconddatetime.microseconddatetime.tzinfodatetime.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.'
|
|
8.1.5. time Objects
|
|
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.2. calendar — General calendar-related functions
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.
|
|
|
|
|
|
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
|
|
Counter objects return a zero count for missing items instead of raising a KeyError
|
|
|
|
|
|
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)
|
|
appendleft(x)
|
|
clear()
|
|
-
copy() -
count(x) -
extend(iterable)
|
|
extendleft(iterable)
|
|
index(x[, start[, stop]])
|
|
insert(i, x)
|
|
pop()
|
|
popleft()
|
|
remove(value)
|
|
reverse()
|
|
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()).
|
|
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:
|
|
Setting the default_factory to int makes the defaultdict useful for counting (like a bag or multiset in other languages):
|
|
A faster and more flexible way to create constant functions is to use a lambda function which can supply any constant value
|
|
Setting the default_factory to set makes the defaultdict useful for building a dictionary of sets:
|
|
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.
|
|
Named tuples are especially useful for assigning field names to result tuples returned by the csv or sqlite3 modules:
|
|
|
|
8.3.6. OrderedDict objects
|
|
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(), andsetstate()restores the internal state of the generator to what it was at the timegetstate()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 forrandrange(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)1count(10) --> 10 11 12 13 14 ... -
itertools.cycle(iterable)1cycle('ABCD') --> A B C D A B C D ... -
itertools.repeat(object[, times])1repeat(10, 3) --> 10 10 10
Iterators terminating on the shortest input sequence:
-
itertools.accumulate(iterable[, func])1accumulate([1,2,3,4,5]) --> 1 3 6 10 15 -
itertools.chain(*iterables)classmethod chain.from_iterable(iterable)
1 2chain('ABC', 'DEF') --> A B C D E F chain.from_iterable(['ABC', 'DEF']) --> A B C D E F -
itertools.compress(data, selectors)1compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F -
itertools.dropwhile(predicate, iterable)1dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1 -
itertools.filterfalse(predicate, iterable)1filterfalse(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])1islice('ABCDEFG', 2, None) --> C D E F G -
itertools.starmap(function, iterable)1starmap(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)1zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
Combinatoric iterators:
-
itertools.product(*iterables, repeat=1)1 2product('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 2permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC -
itertools.combinations(iterable, r)1 2combinations('ABCD', 2) --> AB AC AD BC BD CD -
itertools.combinations_with_replacement(iterable, r)1 2combinations_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.
1sorted(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_orderingGiven 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.singledispatchTransform 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.24. http.cookiejar — Cookie handling for HTTP clients
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
- Graphical User Interfaces with Tk