Built-in Functions

The Python interpreter has a number of functions built into it that are always available. They are listed here in alphabetical order.


\begin{funcdesc}{abs}{x}
Return the absolute value of a number. The argument may be a plain
or long integer or a floating point number.
\end{funcdesc}


\begin{funcdesc}{apply}{function\, args}
The \var{function} argument must be a c...
...rgs})}, since in that case there is always
exactly one argument.)
\end{funcdesc}


\begin{funcdesc}{chr}{i}
Return a string of one character whose \ASCII{} code i...
...e{ord()}. The argument must be in the range [0..255],
inclusive.
\end{funcdesc}


\begin{funcdesc}{cmp}{x\, y}
Compare the two objects \var{x} and \var{y} and re...
...} == \var{y}} and strictly positive if
\code{\var{x} > \var{y}}.
\end{funcdesc}


\begin{funcdesc}{coerce}{x\, y}
Return a tuple consisting of the two numeric ar...
...mon type, using the same rules as used by arithmetic
operations.
\end{funcdesc}


\begin{funcdesc}{compile}{string\, filename\, kind}
Compile the \var{string} in...
...tements, or \code{'eval'}
if it consists of a single expression.
\end{funcdesc}


\begin{funcdesc}{dir}{}
Without arguments, return the list of names in the curr...
...es', 'path', 'stderr', 'stdin', 'stdout']
>>>\end{verbatim}\ecode
\end{funcdesc}


\begin{funcdesc}{divmod}{a\, b}
Take two numbers as arguments and return a pair...
...as
\code{(math.floor(\var{a} / \var{b}), \var{a} \%{} \var{b})}.
\end{funcdesc}


\begin{funcdesc}{eval}{s\, globals\, locals}
The arguments are a string and two...
...on of statements is supported by the
\code{exec} statement.
\par
\end{funcdesc}


\begin{funcdesc}{filter}{function\, list}
Construct a list from those elements o...
...lements of \var{list} that are false (zero or empty) are
removed.
\end{funcdesc}


\begin{funcdesc}{float}{x}
Convert a number to floating point. The argument may be a plain or
long integer or a floating point number.
\end{funcdesc}


\begin{funcdesc}{getattr}{object\, name}
The arguments are an object and a stri...
..., '\var{foobar}')} is equivalent to
\code{\var{x}.\var{foobar}}.
\end{funcdesc}


\begin{funcdesc}{hasattr}{object\, name}
The arguments are an object and a stri...
...bject, name)} and
seeing whether it raises an exception or not.)
\end{funcdesc}


\begin{funcdesc}{hash}{object}
Return the hash value of the object (if it has o...
...ash value (even if they are of different types, e.g.
1 and 1.0).
\end{funcdesc}


\begin{funcdesc}{hex}{x}
Convert a number to a hexadecimal string. The result is a valid
Python expression.
\end{funcdesc}


\begin{funcdesc}{id}{object}
Return the \lq identity' of an object. This is an int...
...alue.) (Implementation note: this is the address of the
object.)
\end{funcdesc}


\begin{funcdesc}{input}{prompt}
Almost equivalent to \code{eval(raw_input(\var{...
...ay be broken over multiple lines using the
backslash convention.
\end{funcdesc}


\begin{funcdesc}{int}{x}
Convert a number to a plain integer. The argument may be a plain or
long integer or a floating point number.
\end{funcdesc}


\begin{funcdesc}{len}{s}
Return the length (the number of items) of an object. ...
... be a sequence (string, tuple or list) or a mapping (dictionary).
\end{funcdesc}


\begin{funcdesc}{long}{x}
Convert a number to a long integer. The argument may be a plain or
long integer or a floating point number.
\end{funcdesc}


\begin{funcdesc}{map}{function\, list\, ...}
Apply \var{function} to every item ...
...guments may be
any kind of sequence; the result is always a list.
\end{funcdesc}


\begin{funcdesc}{max}{s}
Return the largest item of a non-empty sequence (string, tuple or
list).
\end{funcdesc}


\begin{funcdesc}{min}{s}
Return the smallest item of a non-empty sequence (string, tuple or
list).
\end{funcdesc}


\begin{funcdesc}{oct}{x}
Convert a number to an octal string. The result is a valid Python
expression.
\end{funcdesc}


\begin{funcdesc}{open}{filename\, mode\, bufsize}
Return a new file object (des...
...r tty devices and fully buffered for other
files.%
\footnotemark
\end{funcdesc}


\begin{funcdesc}{ord}{c}
Return the \ASCII{} value of a string of one character...
...urns the integer \code{97}. This is the inverse of
\code{chr()}.
\end{funcdesc}


\begin{funcdesc}{pow}{x\, y}
Return \var{x} to the power \var{y}. The arguments...
...tion raises an exception; e.g., \code{pow(2, -1)} is not allowed.
\end{funcdesc}


\begin{funcdesc}{range}{start\, end\, step}
This is a versatile function to cre...
...8, -9]
>>> range(0)
[]
>>> range(1, 0)
[]
>>>\end{verbatim}\ecode
\end{funcdesc}


\begin{funcdesc}{raw_input}{prompt}
The string argument is optional; if present...
...cus
>>> s
'Monty Python\'s Flying Circus'
>>>\end{verbatim}\ecode
\end{funcdesc}


\begin{funcdesc}{reduce}{function\, list\, initializer}
Apply the binary \var{fu...
...\var{list}. The \var{list} arguments may be any kind of
sequence.
\end{funcdesc}


\begin{funcdesc}{reload}{module}
Re-parse and re-initialize an already imported...
...ized module object available) before you can
\code{reload()} it.
\end{funcdesc}


\begin{funcdesc}{repr}{object}
Return a string containing a printable representa...
...yield an object with the same value
when passed to \code{eval()}.
\end{funcdesc}


\begin{funcdesc}{round}{x\, n}
Return the floating point value \var{x} rounded ...
...round(0.5)} is \code{1.0} and \code{round(-0.5)} is \code{-1.0}).
\end{funcdesc}


\begin{funcdesc}{setattr}{object\, name\, value}
This is the counterpart of \co...
...bar}', 123)} is equivalent to
\code{\var{x}.\var{foobar} = 123}.
\end{funcdesc}


\begin{funcdesc}{str}{object}
Return a string containing a nicely printable repr...
...table to \code{eval()};
its goal is to return a printable string.
\end{funcdesc}


\begin{funcdesc}{type}{object}
Return the type of an \var{object}. The return va...
...f type(x) == type(''): print 'It is a string'\end{verbatim}\ecode
\end{funcdesc}


\begin{funcdesc}{vars}{}
Without arguments, return a dictionary corresponding to...
... on the
corresponding symbol table are undefined.%
\footnotemark
\end{funcdesc}


\begin{funcdesc}{xrange}{start\, end\, step}
This function is very similar to \c...
...sed (e.g. when the loop is usually
terminated with \code{break}).
\end{funcdesc}