Pretty much all programs need to do numeric calculations. Python has several ways of representing numbers, and an assortment of operators to operate on numbers.
To do numeric calculations in Python, you can write
expressions that look more or less like algebraic
expressions in many other common languages. The
+” operator is addition;
-” is subtraction; use
*” to multiply; and use
/” to divide. Here are
>>> 99 + 1 100 >>> 1 - 99 -98 >>> 7 * 5 35 >>> 81 / 9 9
The examples in this document will often use a lot of extra space between the parts of the expression, just to make things easier to read. However, these spaces are not required:
>>> 99+1 100 >>> 1-99 -98
When an expression contains more than one operation, Python defines the usual order of operations, so that higher-precedence operations like multiplication and division are done before addition and subtraction. In this example, even though the multiplication comes after the addition, it is done first.
>>> 2 + 3 * 4 14
If you want to override the usual precedence of Python operators, use parentheses:
>>> (2+3)*4 20
Here's a result you may not expect:
>>> 1 / 5 0
You might expect a result of 0.2, not zero. However, Python has different kinds of numbers. Any number without a decimal point is considered an integer, a whole number. If any of the numbers involved contain a decimal point, the computation is done using floating point type:
>>> 1.0 / 4.0 0.25 >>> 1.0 / 5.0 0.20000000000000001
That second example above may also surprise you. Mathematically, one-fifth is exactly 0.2. However, in Python (as in pretty much all other contemporary programming languages), many real numbers cannot be represented exactly. The representation of 1.0/5.0 has a slight error in the seventeenth decimal place. This behavior may be slightly annoying, but in conversational mode, Python doesn't know how much precision you want, so you get a ridiculous amount of precision, and this shows up the fact that some values are approximations.
You can use Python's
>>> print 1.0/5.0 0.2
It's okay to mix integer and floating point numbers in the same expression. Any integer values are coerced to their floating point equivalents.
>>> print 1.0/5 0.2 >>> print 1/5.0 0.2
Later we will learn about Python's format operator, which allows you to specify exactly how much precision to use when displaying numbers. For now, let's move on to some more of the operators on numbers.
%” operator between two
numbers gives you the modulo.
That is, the expression “
” returns the remainder when
is divided by
>>> 13 % 5 3 >>> 5.72 % 0.5 0.21999999999999975 >>> print 5.72 % 0.5 0.22
Exponentiation is expressed as “
>>> 2 ** 8 256 >>> 2 ** 30 1073741824 >>> 2.0 ** 0.5 1.4142135623730951 >>> 10.0 ** 5.2 158489.31924611141 >>> 2.0 ** 100 1.2676506002282294e+30
That last number,
is an example of exponential or
scientific notation. This
number is read as “1.26765... times ten to the
30th power”. Similarly, a number like
1.66e-24 is read as “1.66 times ten to
the minus 24th power”.
So far we have seen examples of the integer type, which
int in Python, and the
floating-point type, called the
in Python. Values in
int type must be
between -2,147,483,648 and 2,147,483,647 (inclusive).
There is another type called
can represent much larger integer values. Python
automatically switches to this type whenever an
expression has values larger than about two billion. You
will see letter “
at the end of such values, but they act just like regular
>>> 2 ** 50 1125899906842624L >>> 2 ** 100 1267650600228229401496703205376L >>> 2 ** 1000 107150860718626732094842504906000181056140481170553360744375038837035105112 493612249319837881569585812759467291755314682518714528569231404359845775746 985748039345677748242309854210746050623711418779541821530464749835819412673 987675591655439460770629145711964776865421676604298316526243868372056680693 76L