2. 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.
Built-in Functions |
||||
|---|---|---|---|---|
In addition, there are other four built-in functions that are no longer
considered essential: apply(), buffer(), coerce(), and
intern(). They are documented in the Non-essential Built-in Functions
section.
-
abs(x)¶ Return the absolute value of a number. The argument may be a plain or long integer or a floating point number. If the argument is a complex number, its magnitude is returned.
-
all(iterable)¶ Return
Trueif all elements of the iterable are true (or if the iterable is empty). Equivalent to:def all(iterable): for element in iterable: if not element: return False return True
New in version 2.5.
-
any(iterable)¶ Return
Trueif any element of the iterable is true. If the iterable is empty, returnFalse. Equivalent to:def any(iterable): for element in iterable: if element: return True return False
New in version 2.5.
-
basestring()¶ This abstract type is the superclass for
strandunicode. It cannot be called or instantiated, but it can be used to test whether an object is an instance ofstrorunicode.isinstance(obj, basestring)is equivalent toisinstance(obj, (str, unicode)).New in version 2.3.
-
bin(x)¶ Convert an integer number to a binary string. The result is a valid Python expression. If x is not a Python
intobject, it has to define an__index__()method that returns an integer.New in version 2.6.
-
class
bool([x])¶ Return a Boolean value, i.e. one of
TrueorFalse. x is converted using the standard truth testing procedure. If x is false or omitted, this returnsFalse; otherwise it returnsTrue.boolis also a class, which is a subclass ofint. Classboolcannot be subclassed further. Its only instances areFalseandTrue.New in version 2.2.1.
Changed in version 2.3: If no argument is given, this function returns
False.
-
class
bytearray([source[, encoding[, errors]]])¶ Return a new array of bytes. The
bytearrayclass is a mutable sequence of integers in the range 0 <= x < 256. It has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that thestrtype has, see String Methods.The optional source parameter can be used to initialize the array in a few different ways:
If it is unicode, you must also give the encoding (and optionally, errors) parameters;
bytearray()then converts the unicode to bytes usingunicode.encode().If it is an integer, the array will have that size and will be initialized with null bytes.
If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array.
If it is an iterable, it must be an iterable of integers in the range
0 <= x < 256, which are used as the initial contents of the array.
Without an argument, an array of size 0 is created.
New in version 2.6.
-
callable(object)¶ Return
Trueif the object argument appears callable,Falseif not. If this returns true, it is still possible that a call fails, but if it is false, calling object will never succeed. Note that classes are callable (calling a class returns a new instance); class instances are callable if they have a__call__()method.
-
chr(i)¶ Return a string of one character whose ASCII code is the integer i. For example,
chr(97)returns the string'a'. This is the inverse oford(). The argument must be in the range [0..255], inclusive;ValueErrorwill be raised if i is outside that range. See also
