Professional Documents
Culture Documents
Numeric Types: Built-In Numeric Tools
Numeric Types: Built-In Numeric Tools
The oct function converts decimal to octal, hex to hexadecimal, and bin to binary. To go the other way, the built-in int function converts a string
of digits to an integer, and an optional second argument lets you specify the numeric base:
>>> int('64'), int('100', 8), int('40', 16), int('1000000', 2)
(64, 64, 64, 64)
>>> int('0x40', 16), int('0b1000000', 2) # Literals okay too
(64, 64)
Sets
>>> x = set('abcde')
>>> y = set('bdxyz')
>>> x
set(['a', 'c', 'b', 'e', 'd'])
>>> 'e' in x # Membership
True
>>> x – y # Difference
set(['a', 'c', 'e'])
>>> x | y # Union
set(['a', 'c', 'b', 'e', 'd', 'y', 'x', 'z'])
>>> x & y # Intersection
set(['b', 'd'])
>>> x ^ y # Symmetric difference (XOR)
set(['a', 'c', 'e', 'y', 'x', 'z'])
>>> x > y, x < y # Superset, subset
(False, False)
>>> z = x.intersection(y) # Same as x & y
>>> z
set(['b', 'd'])
>>> z.add('SPAM') # Insert one item
>>> z
set(['b', 'd', 'SPAM'])
>>> z.update(set(['X', 'Y'])) # Merge: in-place union
>>> z
set(['Y', 'X', 'b', 'd', 'SPAM'])
>>> z.remove('b') # Delete one item
>>> z
set(['Y', 'X', 'd', 'SPAM'])
As iterable containers, sets can also be used in operations such as len, for loops, and list comprehensions. Because they are unordered, though,
they don’t support sequence operations like indexing and slicing:
>>> for item in set('abc'): print(item * 3)
...
aaa
ccc
bbb
Set operations can be coded manually in Python with other types, like lists and dictionaries.
>>> S
{1.23}
>>> S.add([1, 2, 3]) # Only mutable objects work in a set
TypeError: unhashable type: 'list'
>>> S.add({'a':1})
TypeError: unhashable type: 'dict'
>>> S.add((1, 2, 3))
>>> S # No list or dict, but tuple okay
{1.23, (1, 2, 3)}
>>> S | {(4, 5, 6), (1, 2, 3)} # Union: same as S.union(...)
{1.23, (4, 5, 6), (1, 2, 3)}
>>> (1, 2, 3) in S # Membership: by complete values
True
>>> (1, 4, 3) in S
False
Sets can be used to filter duplicates out of other collections. Simply convert the collection to a set, and then convert it back again (because sets
are iterable, they work in the list call here):
>>> L = [1, 2, 1, 3, 2, 4, 5]
>>> set(L)
{1, 2, 3, 4, 5}
>>> L = list(set(L)) # Remove duplicates
>>> L
[1, 2, 3, 4, 5]
Numeric Extensions
Finally, although Python core numeric types offer plenty of power for most applications, there is a large library of third-party open source
extensions available to address more focused needs. Because numeric programming is a popular domain for Python, you’ll find a wealth of
advanced tools.
For example, if you need to do serious number crunching, an optional extension for Python called NumPy (Numeric Python) provides advanced
numeric programming tools, such as a matrix data type, vector processing, and sophisticated computation libraries. Hardcore scientific
programming groups at places like Los Alamos and NASA use Python with NumPy to implement the sorts of tasks they previously coded in C++,
FORTRAN, or Matlab. The combination of Python and NumPy is often compared to a free, more flexible version of Matlab—you get NumPy’s
performance, plus the Python language and its libraries.
Because it’s so advanced, we won’t talk further about NumPy in this book. You can find additional support for advanced numeric programming
in Python, including graphics and plotting tools, statistics libraries, and the popular SciPy package at Python’s PyPI site, or by searching the Web.
Also note that NumPy is currently an optional extension; it doesn’t come with Python and must be installed separately.