Beginning Python - From Novice to Professional

Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional

16.01.2014 Views

APPENDIX B ■ PYTHON REFERENCE 561 Table B-3. Some Important Built-in Functions (Continued) Function min(object1, [object2, ...]) oct(number) open(filename[, mode[, bufsize]]) ord(char) If object1 is a nonempty sequence, the smallest element is returned; otherwise, the smallest of the supplied arguments (object1, object2, . . .) is returned. Converts an integer number to an octal string. An alias for file (use open, not file, when opening files). Returns the ASCII value of a single character (a string or Unicode string of length 1). pow(x, y[, z]) Returns x to the power of y, optionally modulo z. range([start, ]stop[, step]) raw_input([prompt]) reduce(function, sequence[, initializer]) reload(module) repr(object) reversed(sequence) round(float[, n]) setattr(object, name, value) sorted(iterable[, cmp][, key][, reverse]) str(object) sum(seq[, start]) tuple([sequence]) type(object) unichr(number) unicode(object[, encoding[, errors]]) Description Returns a numeric range (as a list) with the given start (inclusive, default 0), stop (exclusive), and step (default 1). Returns data input by the user as a string, optionally using a given prompt. Applies the given function cumulatively to the items of the sequence, using the cumulative result as the first argument and the items as the second argument, optionally with a start value (initializer). Reloads an already loaded module and returns it. Returns a string representation of the object, often usable as an argument to eval. Returns a reverse iterator over the sequence. Rounds off the given float to n digits after the decimal point (default zero). Sets the named attribute of the given object to the given value. Returns a new sorted list from the items in iterable. Optional parameters are the same as for the list method sort. Returns a nicely formatted string representation of the given object. Returns the sum of a sequence of numbers, added to the optional parameter start (default 0). Constructs a tuple, optionally with the same items as the supplied sequence. Returns the type of the given object. The Unicode version of chr. Returns a Unicode encoding of the given object, possibly with a given encoding, and a given mode for handling errors (either 'strict', 'replace', or 'ignore', 'strict' being the default).

562 APPENDIX B ■ PYTHON REFERENCE Table B-3. Some Important Built-in Functions (Continued) Function vars([object]) xrange([start, ]stop[, step]) zip(sequence1, ...) Description Returns a dictionary representing the local scope, or a dictionary corresponding to the attributes of the given object (do not modify the returned dictionary). Similar to range, but the returned object uses less memory, and should only be used for iteration. Returns a list of tuples, where each tuple contains an item from each of the supplied sequences. The returned list has the same length as the shortest of the supplied sequences. Table B-4. List Methods Method list.append(object) list.count(object) list.extend(sequence) list.index(object) Description Equivalent to list[len(list):len(list)] = [object]. Returns the number of i’s for which list[i] == object. Equivalent to list[len(list):len(list)] = sequence. Returns the smallest i for which list[i] == object (or raises a ValueError if no such i exists). list.insert(index, object) Equivalent to list[index:index] = [object] if index >= 0; if index < 0, object is prepended to the list. list.pop([index]) list.remove(object) list.reverse() list.sort([cmp][, key] [, reverse]) Removes and returns the item with the given index (default –1). Equivalent to del list[list.index(object)]. Reverses the items of list in place. Sorts the items of list in place (stable sorting). Can be customized by supplying a comparison function, cmp; a key function, key, which will create the keys for the sorting); and a reverse flag (a Boolean value). Table B-5. Dictionary Methods Method dict.clear() dict.copy() dict.fromkeys(seq[, val]) dict.get(key[, default]) Description Removes all the items of dict. Returns a copy of dict. Returns a dictionary with keys from seq and values set to val (default None). May be called directly on dictionary type, dict, as a class method. Returns dict[key] if it exists, otherwise it returns the given default value (default None).

562 APPENDIX B ■ PYTHON REFERENCE<br />

Table B-3. Some Important Built-in Functions (Continued)<br />

Function<br />

vars([object])<br />

xrange([start, ]s<strong>to</strong>p[, step])<br />

zip(sequence1, ...)<br />

Description<br />

Returns a dictionary representing the local scope, or<br />

a dictionary corresponding <strong>to</strong> the attributes of the<br />

given object (do not modify the returned dictionary).<br />

Similar <strong>to</strong> range, but the returned object uses less<br />

memory, and should only be used for iteration.<br />

Returns a list of tuples, where each tuple contains an<br />

item from each of the supplied sequences. The returned<br />

list has the same length as the shortest of the supplied<br />

sequences.<br />

Table B-4. List Methods<br />

Method<br />

list.append(object)<br />

list.count(object)<br />

list.extend(sequence)<br />

list.index(object)<br />

Description<br />

Equivalent <strong>to</strong> list[len(list):len(list)] = [object].<br />

Returns the number of i’s for which list[i] == object.<br />

Equivalent <strong>to</strong> list[len(list):len(list)] = sequence.<br />

Returns the smallest i for which list[i] == object<br />

(or raises a ValueError if no such i exists).<br />

list.insert(index, object) Equivalent <strong>to</strong> list[index:index] = [object] if index >= 0;<br />

if index < 0, object is prepended <strong>to</strong> the list.<br />

list.pop([index])<br />

list.remove(object)<br />

list.reverse()<br />

list.sort([cmp][, key]<br />

[, reverse])<br />

Removes and returns the item with the given index<br />

(default –1).<br />

Equivalent <strong>to</strong> del list[list.index(object)].<br />

Reverses the items of list in place.<br />

Sorts the items of list in place (stable sorting). Can be<br />

cus<strong>to</strong>mized by supplying a comparison function, cmp; a key<br />

function, key, which will create the keys for the sorting); and<br />

a reverse flag (a Boolean value).<br />

Table B-5. Dictionary Methods<br />

Method<br />

dict.clear()<br />

dict.copy()<br />

dict.fromkeys(seq[, val])<br />

dict.get(key[, default])<br />

Description<br />

Removes all the items of dict.<br />

Returns a copy of dict.<br />

Returns a dictionary with keys from seq and values set<br />

<strong>to</strong> val (default None). May be called directly on dictionary<br />

type, dict, as a class method.<br />

Returns dict[key] if it exists, otherwise it returns the<br />

given default value (default None).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!