The following lightly describes selected, standard Python functions which require no
imports to use. See the standard Python documentation for more technical descriptions and
details of all arguments. Optional arguments are specified in italics.
of a number, i.e. its
Result is [3, 2, 1, 0, 1, 2, 3].
iterable) have true values.
Gives True if they do and
False otherwise. This does
not work on multi-
dimensional NumPy arrays.
any(vals)
Determines whether any of
the items in a collection (or
other iterable object) have
true values. Gives True if so
and False otherwise. This
does not work on multi-
dimensional NumPy arrays.
any([True, False, True]) #
True
any([False, None, 0.0]) #
False
bin(val)
Creates a string representing
the binary version of a
number; 0 and 1 characters
prefixed with ‘0b’. Available
from Python 2.6 onwards.
x = 341
print(bin(x))
Result is ‘0b101010101’.
bool(val)
Determines whether a value
is logically true or false, i.e.
converts a value into a
Boolean object.
bool(0.0) # False
bool(3.0) # True
bool(‘abc’) # True
callable(obj)
Determines whether an object
is callable, i.e. can be
invoked like a function.
Giving True if so and False
otherwise.
y = 1.47
x = abs # The inbuilt
function
print(callable(y),
callable(x)) # False, True
chr(code)
Gives the ASCII character
string for an integer code
number. Performs the inverse
operation to ord().
i = ord(‘A’)
print(chr(i+1))
Result is ‘B’.
cmp(val1, val2)
Compares two objects and
gives back 1 if the first is
larger, −1 if the second is
larger or 0 if they are equal.
Not available in Python 3.
cmp(1,2) # -1
cmp(2,2) # 0
cmp(2,1) # 1
complex(real,
imag)
Creates a complex number
using separate real and
imaginary components.
x = complex(1,-1.4142)
Result is (1-1.4142j); 1-
1.4142i.
dict(
vals)
Creates a dictionary from a
specified object, e.g. to copy
another dictionary or to
convert a list of
keyword:value pairs.
x = [(1,1),(2,4),(3,9),(4,16)]
y = dict(x)
Result is {1:1, 2:4, 3:9,
4:16}.
dir(
obj)
Gives a list of variable names
that are present within the
current program scope, or if
object is specified the names
associated with that object as
attributes.
print(dir())
print(dir(”))
First line shows imports and
declarations. Second line
shows attributes of string
types.
divmod(val1,
val2)
Divide x by y giving the
integer floor and remainder
as separate values.
[See Mathematics section]
enumerate(vals,
start)
Generates sequential pairs of
(number, item) from a
collection or other iterable
object, e.g. extracting objects
and their indices from a list.
Creates an iterator object.
Takes a start number as a
second argument.
letters = ‘GCAT’
enum =
list(enumerate(letters))
Result is [(0,‘G’), (1,‘C’),
(2,‘A’), (3,‘T’)].
eval(exprn, locals,
globals)
Evaluates a string as an
expression in Python syntax,
i.e. to allow the dynamic
generation of expressions.
Allows optional dictionary
arguments to specify global
and local scope variable
names.
text = ‘x*7+5*y’
x = 4.7
y = 8.1
eval(text)
Result is 73.4.
execfile(fileName,
Do'stlaringiz bilan baham: