Python has a number of built-in functions,
which are an integral part of the Python interpreter. We can't look at all
of them because many are related to features of the language that we
haven't addressed yet.

One of the built-in mathematical functions will have to wait for
complete coverage until we've introduced the more complex data types,
specifically tuples, in Chapter 13, Tuples. The divmod (x,
y ) function returns a tuple object with
the quotient and remainder in division.

Built-In Math Functions

The bulk of the math functions are in a separate module, called
math, which we will cover in the section called “The math Module”. The formal definitions of mathematical
built-in functions are provided below.

abs (number) →
number

Return the absolute value of the argument,
|x|.

pow (x,
y, [z]) →
number

Raise x to the y
power. If z is present, this is done modulo
z,
x^{
y
}
% z.

round (number,
[ndigits]) → float

Round number to
ndigits beyond the decimal point.

cmp (x,
y) → integer

Compare x and y,
returning a number. If the number is less than 0, then
x<y; if the number is
zero, then x == y; if
the number is positive, then
x>y.

The round (number,
[ndigits] ) function rounds a number to
the nearest whole number. If the n parameter is given, this is the
number of decimal places to round to. If n is positive, this is decimal
places to the right of the decimal point. If n is negative, this is the
number of places to the left of the decimal point. Examples:
round(678.456,2) yields 678.46;
round(678.456,-1) yields 680.

The cmp(x,
y) function is handy for the
comparisons used when sorting objects into order. For example,
cmp(2,35) yields -1, telling us that the first
value is less than the second value.

Create a hexadecimal string representation of
number. A leading '0x' is placed on the
string as a reminder that this is hexadecimal.
hex(684) yields the string
'0x2ac'.

oct (number) →
string

Create a octal string representation of
number. A leading '0' is placed on the string
as a reminder that this is octal not decimal.
oct(509) yields the string
'0775'.

int (string,
[base]) → integer

Generates an integer from the string x.
If base is supplied, x
must be in the given base. If base is
omitted, x must be decimal.

str (object) →
string

Generate a string representation of the given object. This
is the a "readable" version of the value.

repr (object) →
string

Generate a string representation of the given object.
Generally, this is the a Python expression that can reconstruct
the value; it may be rather long and complex.

The int function has two forms. The
int (x) form converts a
decimal string, x, to an integer. For example
int('25') is 25. The int
(x, b)
form converts a string, x, in base
b to an integer. For example
int('010101',2) yields 21. int('321',4) is 57,
int('2ac',16) is 684.

The str(x) and
repr(x) functions convert any
Python object to a string. The
str(x) version is typically
more readable, where the
repr(x) version is an
internalized representation. For most garden-variety numeric values,
there is no difference. For the more complex data types, however, the
resultsof repr and str can be
very different. For classes you write (see Chapter 21, Classes), your class definition must provide these
string representation functions.

Collection Functions

These are two built-in functions which operate on a collection of
data elements.

max (sequence) →
value

Return the largest value in
sequence.

min (sequence) →
value

Return the smallest value in
sequence.

The max and min
functions accept any number of values and return the largest or smallest
of the values. max(1,2,3) yields 3, min(1,2,3)
yields 1.

Published under the terms of the Open Publication License