In this chapter, you will learn how to use Python to work with numbers. You’ve already seen some arithmetic examples, but after reading this chapter, you’ll have a better understanding of the different ways you can represent numbers in Python, of how to perform mathematical computations, and of efficient ways of working with large numerical data sets.
Numerical code lies at the heart of technical software, and is used widely in science, engineering, finance, and related fields. Almost any substantial program does some nontrivial numerical computation, so it pays to be familiar with some of the contents of this chapter even if you are not working in one of these fields.
For instance, if you are writing a script to analyze web logs, you might want to compute statistics on the rate of hits on your web server; if you are writing a program with a graphical user interface, you might need math functions to compute the coordinates of the graphics in your GUI.
Numbers in Python
A number, like any object in Python, has a type. Python has four basic numerical types. Two of these, int and long, represent integers, and float represents floating-point numbers. The fourth numeric type, which is covered later in this chapter, represents complex floating-point numbers.
Integers
You’ve already seen the simplest integer type, int. If you write an ordinary number in your program like 42, called a literal number, Python creates an int object for it:
>>> x = 42
>>> type(x)
<type ‘int’>
You didn’t have to construct the int explicitly, but you could if you want, like this:
>>> x = int(42)
You can also use the int constructor to convert other types, such as strings or other numerical types, to
integers:
>>> x = int(“17”)
>>> y = int(4.8)
>>> print x, y, x - y
17 4 13
In the first line, Python converts a string representing a number to the number itself; you can’t do math
with “17” (a string), but you can with 17 (an integer). In the second line, Python converted the floatingpoint value 4.8 to the integer 4 by truncating it—chopping off the part after the decimal point to make it an integer.
When you convert a string to an int, Python assumes the number is represented in base 10. You can specify another base as the second argument. For instance, if you pass 16, the number is assumed to be
hexadecimal:
>>> hex_number = “a1”
>>> print int(hex_number, 16)
161
You can specify hexadecimal literals by prefixing the number with 0x. For example, hexadecimal 0xa1 is equivalent to decimal 161. Similarly, literals starting with just a 0 are assumed to be octal (base 8), so octal 0105 is equivalent to decimal 69. These conventions are used in many other programming languages, too.
Long Integers
What’s the largest number Python can store in an int? Python uses at least 32 bits to represent integers,
which means that you can store numbers at least as large as 231–1 and negative numbers as small as-231.
If you need to store a larger number, Python provides the long type, which represents arbitrarily large integers.
For example, long before the search engine Google existed, mathematicians defined a googol, a one followed by 100 zeros. To represent this number in Python, you could type out the hundred zeros, or you can save yourself the trouble by using the exponentiation operator, **:
>>> googol = 10 ** 100
>>> print googol
10000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000
This is an example of a long object:
>>> type(googol)
<type ‘long’>
Note that when you computed the value of googol, you used only int literals—namely, 10 and 100.
Python converted the result to a long automatically because it didn’t fit in an int.
If you enter a literal that is too large for an int, Python uses a long automatically:
>>> type(12345678900)
<type ‘long’>
You can also construct a long object for a number that would fit in an int. Either call the long constructor
explicitly, as in long(42), or append an L to the literal, as in 42L.