Numbers¶
Python’s four number types are integers, floating point numbers, complex numbers and Boolean numbers:
Type |
Examples |
---|---|
Integers |
|
Floats |
|
Complex numbers |
|
Boolean numbers |
|
They can be manipulated with the arithmetic operators:
Operator |
Description |
---|---|
|
Addition |
|
Subtraction |
|
Multiplication |
|
Division [1] |
|
Exponentiation |
|
Modulus |
Note
Integers can be unlimited in size, limited only by the available memory.
Examples:
>>> 8 + 3 - 5 * 3
-4
>>> 8 / 3
2.6666666666666665
>>> 8 // 3
2
>>> x = 4.2 ** 3.4
>>> x
131.53689544409096
>>> 9e7 * -5e-3
-450000.0
>>> -5e-3 ** 3
-1.2500000000000002e-07
See also
Julia Evans: Examples of floating point problems
David Goldberg: What Every Computer Scientist Should Know About Floating-Point Arithmetic
Complex numbers¶
Complex numbers consist of a real part and an imaginary part, which is given the suffix
j
in Python.
>>> 7 + 2j
(7+2j)
Note
Python expresses the resulting complex number in parentheses to indicate that the output represents the value of a single object:
>>> (5+3j) ** (3+5j)
(-7.04464115622119-11.276062812695923j)
>>> x = (5+3j) * (6+8j)
>>> x
(6+58j)
>>> x.real
6.0
>>> x.imag
58.0
Complex numbers consist of a real part and an imaginary part with the suffix
j
. In the preceding code, the variable x
is assigned to a complex
number. You can get its „real“ part with the attribute notation x.real
and
the „imaginary“ part with x.imag
.
Built-in numerical functions¶
Several built-in functions can work with numbers:
abs()
returns the absolute value of a number. Here, as argument can be an integer, a floating point number or an object that implements
__abs__()
. With complex numbers as arguments, their absolute value is returned.divmod()
takes two (non-complex) numbers as arguments and returns a pair of numbers consisting of their quotient and the remainder if integer division is used.
float
returns a floating point number formed from a number or string
x
.hex()
converts an integer number to a lowercase hexadecimal string with the prefix
0x
.int
returns an integer object constructed from a number or string
x
, or0
if no arguments are given.max()
returns the largest element in an iterable or the largest of two or more arguments.
min()
returns the smallest element in an iterable or the smallest of two or more arguments.
oct()
converts an integer number to an octal string with the prefix
0o
. The result is a valid Python expression. Ifx
is not a Pythonint()
object, it must define an__index__()
method that returns an integer.pow()
returns base as a power of exp.
round()
returns a number rounded to ndigits after the decimal point. If ndigits is omitted or is None, the nearest integer to the input is returned.
Boolean values¶
Boolean values are used in the following examples:
>>> x = False
>>> x
False
>>> not x
True
>>> y = True * 2
>>> y
2
Apart from their representation as True
and False
, Boolean values
behave like the numbers 1
(True
) and 0
(False
).
Advanced numerical functions¶
More advanced numerical functions such as trigonometry, as well as some useful
constants, are not built into Python, but are provided in a standard module
called math. Module will
be explained in more detail later. For now, suffice it to say that you need to
make the maths functions available in this section by importing math
:
import math
Built-in functions are always available and are called using standard function
call syntax. In the following code, round
is called with a float as the
input argument.
>>> round(2.5)
2
With ceil
from the standard library math
and the attribute notation
MODUL.FUNKTION(ARGUMENT)
is rounded up:
>>> math.ceil(2.5)
3
The math
module provides, among other things
the number theoretic and representation functions
math.ceil()
,math.modf()
,math.frexp()
andmath.ldexp()
,the power and logarithmic functions
math.exp()
,math.log()
,math.log10()
,math.pow()
andmath.sqrt()
,the trigonometric functions
math.acos()
,math.asin()
,math.atan()
,math.atan2()
,math.ceil()
,math.cos()
,math.hypot()
andmath.sin()
,the hyperbolic functions
math.cosh()
,math.sinh()
andmath.tanh()
Advanced functions for complex numbers¶
The functions in the math module are not
applicable to complex numbers; one of the reasons for this is probably that the
square root of -1
is supposed to produce an error. Therefore, similar
functions for complex numbers have been provided in the cmath module:
cmath.acos()
, cmath.acosh()
, cmath.asin()
, cmath.asinh()
, cmath.atan()
, cmath.atanh()
, cmath.cos()
, cmath.cosh()
, python3:cmath.e()
, cmath.exp()
, cmath.log()
, cmath.log10()
, python3:cmath.pi()
, cmath.sin()
, cmath.sinh()
, cmath.sqrt()
, cmath.tan()
, cmath.tanh()
.
To make it clear in the code that these functions are special functions for
complex numbers, and to avoid name conflicts with the more normal equivalents,
it is recommended to simply import the module to explicitly refer to the
cmath
package when using the function, for example:
>>> import cmath
>>> cmath.sqrt(-2)
1.4142135623730951j
Warning
Now it becomes clearer why we do not recommend importing all functions of a
module with from MODULE import *
. If you would import the module
math
first and then the module cmath
, the functions in cmath
would have priority over those of math
. Also, when understanding the
code, it is much more tedious to find out the source of the functions used.
Rounding half to even¶
Usually Python calculates floating point numbers according to the IEEE 754 standard, rounding down numbers in
the middle half of the time and rounding up in the other half to avoid
statistical drift in longer calculations. Decimal
and ROUND_HALF_UP
from the decimal module are therefore needed
for rounding half to even:
>>> import decimal
>>> num = decimal.Decimal("2.5")
>>> rounded = num.quantize(decimal.Decimal("0"), rounding = decimal.ROUND_HALF_UP)
>>> rounded
Decimal('3')
Numerical calculations¶
The standard Python installation is not well suited for intensive numerical calculations due to speed limitations. But the powerful Python extension NumPy provide highly efficient implementations of many advanced numerical operations. The focus is on array operations, including multi-dimensional matrices and advanced functions such as the fast Fourier transform.
Built-in modules for numbers¶
The Python standard library contains a number of built-in modules that you can use to manage numbers:
Module |
Description |
---|---|
for numeric abstract base classes |
|
for mathematical functions for real and complex numbers |
|
for decimal fixed-point and floating-point arithmetic |
|
for functions for calculating mathematical statistics |
|
for rational numbers |
|
for generating pseudo-random numbers and selections and for shuffling sequences |
|
for functions that create iterators for efficient loops |
|
for higher-order functions and operations on callable objects |
|
for standard operators as functions |