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
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.
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
MODULE.FUNCTION(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()
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')
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 |
See also
Checks¶
Create some number variables (integers, floating point numbers and complex numbers). Experiment a little with what happens when you perform operations with them, even across types.