Built-in Types for Representing Data
There are approximately a dozen built-in data types that are used to represent most of the data used in programs. These are grouped into a few major categories as shown in Table 3.1. The Type Name column in the table lists the name or expression that you can use to check for that type using isinstance() and other type-related functions. Certain types are only available in Python 2 and have been indicated as such (in Python 3, they have been deprecated or merged into one of the other types).
Table 3.1 Built-In Types for Data Representation
Type Category |
Type Name |
Description |
None |
type(None) |
The null object None |
Numbers |
int |
Integer |
long |
Arbitrary-precision integer (Python 2 only) |
|
float |
Floating point |
|
complex |
Complex number |
|
bool |
Boolean (True or False) |
|
Sequences |
str |
Character string |
unicode |
Unicode character string (Python 2 only) |
|
list |
List |
|
tuple |
Tuple |
|
xrange |
A range of integers created by xrange() (In Python 3, it is called range.) |
|
Mapping |
dict |
Dictionary |
Sets |
set |
Mutable set |
frozenset |
Immutable set |
The None Type
The None type denotes a null object (an object with no value). Python provides exactly one null object, which is written as None in a program. This object is returned by functions that don’t explicitly return a value. None is frequently used as the default value of optional arguments, so that the function can detect whether the caller has actually passed a value for that argument. None has no attributes and evaluates to False in Boolean expressions.
Numeric Types
Python uses five numeric types: Booleans, integers, long integers, floating-point numbers, and complex numbers. Except for Booleans, all numeric objects are signed. All numeric types are immutable.
Booleans are represented by two values: True and False. The names True and False are respectively mapped to the numerical values of 1 and 0.
Integers represent whole numbers in the range of –2147483648 to 2147483647 (the range may be larger on some machines). Long integers represent whole numbers of unlimited range (limited only by available memory). Although there are two integer types, Python tries to make the distinction seamless (in fact, in Python 3, the two types have been unified into a single integer type). Thus, although you will sometimes see references to long integers in existing Python code, this is mostly an implementation detail that can be ignored—just use the integer type for all integer operations. The one exception is in code that performs explicit type checking for integer values. In Python 2, the expression isinstance(x, int) will return False if x is an integer that has been promoted to a long.
Floating-point numbers are represented using the native double-precision (64-bit) representation of floating-point numbers on the machine. Normally this is IEEE 754, which provides approximately 17 digits of precision and an exponent in the range of –308 to 308. This is the same as the double type in C. Python doesn’t support 32-bit single-precision floating-point numbers. If precise control over the space and precision of numbers is an issue in your program, consider using the numpy extension (which can be found at http://numpy.sourceforge.net).
Complex numbers are represented as a pair of floating-point numbers. The real and imaginary parts of a complex number z are available in z.real and z.imag. The method z.conjugate() calculates the complex conjugate of z (the conjugate of a+bj is a-bj).
Numeric types have a number of properties and methods that are meant to simplify operations involving mixed arithmetic. For simplified compatibility with rational numbers (found in the fractions module), integers have the properties x.numerator and x.denominator. An integer or floating-point number y has the properties y.real and y.imag as well as the method y.conjugate() for compatibility with complex numbers. A floating-point number y can be converted into a pair of integers representing a fraction using y.as_integer_ratio(). The method y.is_integer() tests if a floating-point number y represents an integer value. Methods y.hex() and y.fromhex() can be used to work with floating-point numbers using their low-level binary representation.
Several additional numeric types are defined in library modules. The decimal module provides support for generalized base-10 decimal arithmetic. The fractions module adds a rational number type. These modules are covered in Chapter 14, “Mathematics.”
Sequence Types
Sequences represent ordered sets of objects indexed by non-negative integers and include strings, lists, and tuples. Strings are sequences of characters, and lists and tuples are sequences of arbitrary Python objects. Strings and tuples are immutable; lists allow insertion, deletion, and substitution of elements. All sequences support iteration.
Operations Common to All Sequences
Table 3.2 shows the operators and methods that you can apply to all sequence types. Element i of sequence s is selected using the indexing operator s[i], and subsequences are selected using the slicing operator s[i:j] or extended slicing operator s[i:j:stride] (these operations are described in Chapter 4). The length of any sequence is returned using the built-in len(s) function. You can find the minimum and maximum values of a sequence by using the built-in min(s) and max(s) functions. However, these functions only work for sequences in which the elements can be ordered (typically numbers and strings). sum(s) sums items in s but only works for numeric data.
Table 3.2 Operations and Methods Applicable to All Sequences
Item |
Description |
s[i] |
Returns element i of a sequence |
s[i:j] |
Returns a slice |
s[i:j:stride] |
Returns an extended slice |
len(s) |
Number of elements in s |
min(s) |
Minimum value in s |
max(s) |
Maximum value in s |
sum(s [,initial]) |
Sum of items in s |
all(s) |
Checks whether all items in s are True. |
any(s) |
Checks whether any item in s is True. |
Table 3.3 shows the additional operators that can be applied to mutable sequences such as lists.
Table 3.3 Operations Applicable to Mutable Sequences
Item |
Description |
s[i] = v |
Item assignment |
s[i:j] = t |
Slice assignment |
s[i:j:stride] = t |
Extended slice assignment |
del s[i] |
Item deletion |
del s[i:j] |
Slice deletion |
del s[i:j:stride] |
Extended slice deletion |
Lists
Lists support the methods shown in Table 3.4. The built-in function list(s) converts any iterable type to a list. If s is already a list, this function constructs a new list that’s a shallow copy of s. The s.append(x) method appends a new element, x, to the end of the list. The s.index(x) method searches the list for the first occurrence of x. If no such element is found, a ValueError exception is raised. Similarly, the s.remove(x) method removes the first occurrence of x from the list or raises ValueError if no such item exists. The s.extend(t) method extends the list s by appending the elements in sequence t.
Table 3.4 List Methods
Method |
Description |
list(s) |
Converts s to a list. |
s.append(x) |
Appends a new element, x, to the end of s. |
s.extend(t) |
Appends a new list, t, to the end of s. |
s.count(x) |
Counts occurrences of x in s. |
s.index(x [,start [,stop]]) |
Returns the smallest i where s[i]==x. start and stop optionally specify the starting and ending index for the search. |
s.insert(i,x) |
Inserts x at index i. |
s.pop([i]) |
Returns the element i and removes it from the list. If i is omitted, the last element is returned. |
s.remove(x) |
Searches for x and removes it from s. |
s.reverse() |
Reverses items of s in place. |
s.sort([key [, reverse]]) |
Sorts items of s in place. key is a key function. reverse is a flag that sorts the list in reverse order. key and reverse should always be specified as keyword arguments. |
The s.sort() method sorts the elements of a list and optionally accepts a key function and reverse flag, both of which must be specified as keyword arguments. The key function is a function that is applied to each element prior to comparison during sorting. If given, this function should take a single item as input and return the value that will be used to perform the comparison while sorting. Specifying a key function is useful if you want to perform special kinds of sorting operations such as sorting a list of strings, but with case insensitivity. The s.reverse() method reverses the order of the items in the list. Both the sort() and reverse() methods operate on the list elements in place and return None.
Strings
Python 2 provides two string object types. Byte strings are sequences of bytes containing 8-bit data. They may contain binary data and embedded NULL bytes. Unicode strings are sequences of unencoded Unicode characters, which are internally represented by 16-bit integers. This allows for 65,536 unique character values. Although the Unicode standard supports up to 1 million unique character values, these extra characters are not supported by Python by default. Instead, they are encoded as a special two-character (4-byte) sequence known as a surrogate pair—the interpretation of which is up to the application. As an optional feature, Python may be built to store Unicode characters using 32-bit integers. When enabled, this allows Python to represent the entire range of Unicode values from U+000000 to U+110000. All Unicode-related functions are adjusted accordingly.
Strings support the methods shown in Table 3.5. Although these methods operate on string instances, none of these methods actually modifies the underlying string data. Thus, methods such as s.capitalize(), s.center(), and s.expandtabs() always return a new string as opposed to modifying the string s. Character tests such as s.isalnum() and s.isupper() return True or False if all the characters in the string s satisfy the test. Furthermore, these tests always return False if the length of the string is zero.
Table 3.5 String Methods
Method |
Description |
s.capitalize() |
Capitalizes the first character. |
s.center(width [, pad]) |
Centers the string in a field of length width. pad is a padding character. |
s.count(sub [,start [,end]]) |
Counts occurrences of the specified substring sub. |
s.decode([encoding [,errors]]) |
Decodes a string and returns a Unicode string (byte strings only). |
s.encode([encoding [,errors]]) |
Returns an encoded version of the string (unicode strings only). |
s.endswith(suffix [,start [,end]]) |
Checks the end of the string for a suffix. |
s.expandtabs([tabsize]) |
Replaces tabs with spaces. |
s.find(sub [, start [,end]]) |
Finds the first occurrence of the specified substring sub or returns −1. |
s.format(*args, **kwargs) |
Formats s. |
s.index(sub [, start [,end]]) |
Finds the first occurrence of the specified substring sub or raises an error. |
s.isalnum() |
Checks whether all characters are alphanumeric. |
s.isalpha() |
Checks whether all characters are alphabetic. |
s.isdigit() |
Checks whether all characters are digits. |
s.islower() |
Checks whether all characters are lowercase. |
s.isspace() |
Checks whether all characters are whitespace. |
s.istitle() |
Checks whether the string is a title-cased string (first letter of each word capitalized). |
s.isupper() |
Checks whether all characters are uppercase. |
s.join(t) |
Joins the strings in sequence t with s as a separator. |
s.ljust(width [, fill]) |
Left-aligns s in a string of size width. |
s.lower() |
Converts to lowercase. |
s.lstrip([chrs]) |
Removes leading whitespace or characters supplied in chrs. |
s.partition(sep) |
Partitions a string based on a separator string sep. Returns a tuple (head,sep,tail) or (s, "","") if sep isn’t found. |
s.replace(old, new [,maxreplace]) |
Replaces a substring. |
s.rfind(sub [,start [,end]]) |
Finds the last occurrence of a substring. |
s.rindex(sub [,start [,end]]) |
Finds the last occurrence or raises an error. |
s.rjust(width [, fill]) |
Right-aligns s in a string of length width. |
s.rpartition(sep) |
Partitions s based on a separator sep, but searches from the end of the string. |
s.rsplit([sep [,maxsplit]]) |
Splits a string from the end of the string using sep as a delimiter. maxsplit is the maximum number of splits to perform. If maxsplit is omitted, the result is identical to the split() method. |
s.rstrip([chrs]) |
Removes trailing whitespace or characters supplied in chrs. |
s.split([sep [,maxsplit]]) |
Splits a string using sep as a delimiter. maxsplit is the maximum number of splits to perform. |
s.splitlines([keepends]) |
Splits a string into a list of lines. If keepends is 1, trailing newlines are preserved. |
s.startswith(prefix [,start [,end]]) |
Checks whether a string starts with prefix. |
s.strip([chrs]) |
Removes leading and trailing whitespace or characters supplied in chrs. |
s.swapcase() |
Converts uppercase to lowercase, and vice versa. |
s.title() |
Returns a title-cased version of the string. |
s.translate(table [,deletechars]) |
Translates a string using a character translation table table, removing characters in deletechars. |
s.upper() |
Converts a string to uppercase. |
s.zfill(width) |
Pads a string with zeros on the left up to the specified width. |
The s.find(), s.index(), s.rfind(), and s.rindex() methods are used to search s for a substring. All these functions return an integer index to the substring in s. In addition, the find() method returns -1 if the substring isn’t found, whereas the index() method raises a ValueError exception. The s.replace() method is used to replace a substring with replacement text. It is important to emphasize that all of these methods only work with simple substrings. Regular expression pattern matching and searching is handled by functions in the re library module.
The s.split() and s.rsplit() methods split a string into a list of fields separated by a delimiter. The s.partition() and s.rpartition() methods search for a separator substring and partition s into three parts corresponding to text before the separator, the separator itself, and text after the separator.
Many of the string methods accept optional start and end parameters, which are integer values specifying the starting and ending indices in s. In most cases, these values may be given negative values, in which case the index is taken from the end of the string.
The s.translate() method is used to perform advanced character substitutions such as quickly stripping all control characters out of a string. As an argument, it accepts a translation table containing a one-to-one mapping of characters in the original string to characters in the result. For 8-bit strings, the translation table is a 256-character string. For Unicode, the translation table can be any sequence object s where s[n] returns an integer character code or Unicode character corresponding to the Unicode character with integer value n.
The s.encode() and s.decode() methods are used to transform string data to and from a specified character encoding. As input, these accept an encoding name such as 'ascii', 'utf-8', or 'utf-16'. These methods are most commonly used to convert Unicode strings into a data encoding suitable for I/O operations and are described further in Chapter 9, “Input and Output.” Be aware that in Python 3, the encode() method is only available on strings, and the decode() method is only available on the bytes datatype.
The s.format() method is used to perform string formatting. As arguments, it accepts any combination of positional and keyword arguments. Placeholders in s denoted by {item} are replaced by the appropriate argument. Positional arguments can be referenced using placeholders such as {0} and {1}. Keyword arguments are referenced using a placeholder with a name such as {name}. Here is an example:
>>> a = "Your name is {0} and your age is {age}" >>> a.format("Mike", age=40) 'Your name is Mike and your age is 40' >>>
Within the special format strings, the {item} placeholders can also include simple index and attribute lookup. A placeholder of {item[n]} where n is a number performs a sequence lookup on item. A placeholder of {item[key]} where key is a non-numeric string performs a dictionary lookup of item["key"]. A placeholder of {item.attr} refers to attribute attr of item. Further details on the format() method can be found in the “String Formatting” section of Chapter 4.
xrange() Objects
The built-in function xrange([i,]j [,stride]) creates an object that represents a range of integers k such that i <= k < j. The first index, i, and the stride are optional and have default values of 0 and 1, respectively. An xrange object calculates its values whenever it’s accessed and although an xrange object looks like a sequence, it is actually somewhat limited. For example, none of the standard slicing operations are supported. This limits the utility of xrange to only a few applications such as iterating in simple loops.
It should be noted that in Python 3, xrange() has been renamed to range(). However, it operates in exactly the same manner as described here.
Mapping Types
A mapping object represents an arbitrary collection of objects that are indexed by another collection of nearly arbitrary key values. Unlike a sequence, a mapping object is unordered and can be indexed by numbers, strings, and other objects. Mappings are mutable.
Dictionaries are the only built-in mapping type and are Python’s version of a hash table or associative array. You can use any immutable object as a dictionary key value (strings, numbers, tuples, and so on). Lists, dictionaries, and tuples containing mutable objects cannot be used as keys (the dictionary type requires key values to remain constant).
To select an item in a mapping object, use the key index operator m[k], where k is a key value. If the key is not found, a KeyError exception is raised. The len(m) function returns the number of items contained in a mapping object. Table 3.6 lists the methods and operations.
Table 3.6 Methods and Operations for Dictionaries
Item |
Description |
len(m) |
Returns the number of items in m. |
m[k] |
Returns the item of m with key k. |
m[k]=x |
Sets m[k] to x. |
del m[k] |
Removes m[k] from m. |
k in m |
Returns True if k is a key in m. |
m.clear() |
Removes all items from m. |
m.copy() |
Makes a copy of m. |
m.fromkeys(s [,value]) |
Create a new dictionary with keys from sequence s and values all set to value. |
m.get(k [,v]) |
Returns m[k] if found; otherwise, returns v. |
m.has_key(k) |
Returns True if m has key k; otherwise, returns False. (Deprecated, use the in operator instead. Python 2 only) |
m.items() |
Returns a sequence of (key,value) pairs. |
m.keys() |
Returns a sequence of key values. |
m.pop(k [,default]) |
Returns m[k] if found and removes it from m; otherwise, returns default if supplied or raises KeyError if not. |
m.popitem() |
Removes a random (key,value) pair from m and returns it as a tuple. |
m.setdefault(k [, v]) |
Returns m[k] if found; otherwise, returns v and sets m[k] = v. |
m.update(b) |
Adds all objects from b to m. |
m.values() |
Returns a sequence of all values in m. |
Most of the methods in Table 3.6 are used to manipulate or retrieve the contents of a dictionary. The m.clear() method removes all items. The m.update(b) method updates the current mapping object by inserting all the (key,value) pairs found in the mapping object b. The m.get(k [,v]) method retrieves an object but allows for an optional default value, v, that’s returned if no such key exists. The m.setdefault(k [,v]) method is similar to m.get(), except that in addition to returning v if no object exists, it sets m[k] = v. If v is omitted, it defaults to None. The m.pop() method returns an item from a dictionary and removes it at the same time. The m.popitem() method is used to iteratively destroy the contents of a dictionary.
The m.copy() method makes a shallow copy of the items contained in a mapping object and places them in a new mapping object. The m.fromkeys(s [,value]) method creates a new mapping with keys all taken from a sequence s. The type of the resulting mapping will be the same as m. The value associated with all of these keys is set to None unless an alternative value is given with the optional value parameter. The fromkeys() method is defined as a class method, so an alternative way to invoke it would be to use the class name such as dict.fromkeys().
The m.items() method returns a sequence containing (key,value) pairs. The m.keys() method returns a sequence with all the key values, and the m.values() method returns a sequence with all the values. For these methods, you should assume that the only safe operation that can be performed on the result is iteration. In Python 2 the result is a list, but in Python 3 the result is an iterator that iterates over the current contents of the mapping. If you write code that simply assumes it is an iterator, it will be generally compatible with both versions of Python. If you need to store the result of these methods as data, make a copy by storing it in a list. For example, items = list(m.items()). If you simply want a list of all keys, use keys = list(m).
Set Types
A set is an unordered collection of unique items. Unlike sequences, sets provide no indexing or slicing operations. They are also unlike dictionaries in that there are no key values associated with the objects. The items placed into a set must be immutable. Two different set types are available: set is a mutable set, and frozenset is an immutable set. Both kinds of sets are created using a pair of built-in functions:
s = set([1,5,10,15]) f = frozenset(['a',37,'hello'])
Both set() and frozenset() populate the set by iterating over the supplied argument. Both kinds of sets provide the methods outlined in Table 3.7.
Table 3.7 Methods and Operations for Set Types
Item |
Description |
len(s) |
Returns the number of items in s. |
s.copy() |
Makes a copy of s. |
s.difference(t) |
Set difference. Returns all the items in s, but not in t. |
s.intersection(t) |
Intersection. Returns all the items that are both in s and in t. |
s.isdisjoint(t) |
Returns True if s and t have no items in common. |
s.issubset(t) |
Returns True if s is a subset of t. |
s.issuperset(t) |
Returns True if s is a superset of t. |
s.symmetric_difference(t) |
Symmetric difference. Returns all the items that are in s or t, but not in both sets. |
s.union(t) |
Union. Returns all items in s or t. |
The s.difference(t), s.intersection(t), s.symmetric_difference(t), and s.union(t) methods provide the standard mathematical operations on sets. The returned value has the same type as s (set or frozenset). The parameter t can be any Python object that supports iteration. This includes sets, lists, tuples, and strings. These set operations are also available as mathematical operators, as described further in Chapter 4.
Mutable sets (set) additionally provide the methods outlined in Table 3.8.
Table 3.8 Methods for Mutable Set Types
Item |
Description |
s.add(item) |
Adds item to s. Has no effect if item is already in s. |
s.clear() |
Removes all items from s. |
s.difference_update(t) |
Removes all the items from s that are also in t. |
s.discard(item) |
Removes item from s. If item is not a member of s, nothing happens. |
s.intersection_update(t) |
Computes the intersection of s and t and leaves the result in s. |
s.pop() |
Returns an arbitrary set element and removes it from s. |
s.remove(item) |
Removes item from s. If item is not a member, KeyError is raised. |
s.symmetric_difference_update(t) |
Computes the symmetric difference of s and t and leaves the result in s. |
s.update(t) |
Adds all the items in t to s. t may be another set, a sequence, or any object that supports iteration. |
All these operations modify the set s in place. The parameter t can be any object that supports iteration.