Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
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).
- Page 541 and 542: 510 CHAPTER 27 ■ PROJECT 8: FILE
- Page 543 and 544: 512 CHAPTER 27 ■ PROJECT 8: FILE
- Page 545 and 546: 514 CHAPTER 27 ■ PROJECT 8: FILE
- Page 547 and 548: 516 CHAPTER 27 ■ PROJECT 8: FILE
- Page 549 and 550: 518 CHAPTER 28 ■ PROJECT 9: FILE
- Page 551 and 552: 520 CHAPTER 28 ■ PROJECT 9: FILE
- Page 553 and 554: 522 CHAPTER 28 ■ PROJECT 9: FILE
- Page 555 and 556: 524 CHAPTER 28 ■ PROJECT 9: FILE
- Page 558 and 559: CHAPTER 29 ■ ■ ■ Project 10:
- Page 560 and 561: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 562 and 563: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 564 and 565: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 566 and 567: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 568 and 569: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 570 and 571: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 572 and 573: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 574 and 575: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 576 and 577: CHAPTER 29 ■ PROJECT 10: DO-IT-YO
- Page 578 and 579: APPENDIX A ■ ■ ■ The Short Ve
- Page 580 and 581: APPENDIX A ■ THE SHORT VERSION 54
- Page 582 and 583: APPENDIX A ■ THE SHORT VERSION 55
- Page 584 and 585: APPENDIX A ■ THE SHORT VERSION 55
- Page 586: APPENDIX A ■ THE SHORT VERSION 55
- Page 589 and 590: 558 APPENDIX B ■ PYTHON REFERENCE
- Page 591: 560 APPENDIX B ■ PYTHON REFERENCE
- Page 595 and 596: 564 APPENDIX B ■ PYTHON REFERENCE
- Page 597 and 598: 566 APPENDIX B ■ PYTHON REFERENCE
- Page 599 and 600: 568 APPENDIX B ■ PYTHON REFERENCE
- Page 601 and 602: 570 APPENDIX B ■ PYTHON REFERENCE
- Page 603 and 604: 572 APPENDIX C ■ ONLINE RESOURCES
- Page 606 and 607: Index ■Symbols - operator 558 !=
- Page 608 and 609: ■INDEX 577 assertEqual method Tes
- Page 610 and 611: ■INDEX 579 cmd module 252, 501 Cm
- Page 612 and 613: ■INDEX 581 get method 74-75 has_k
- Page 614 and 615: ■INDEX 583 ■F Factory class twi
- Page 616 and 617: ■INDEX 585 finding conflicts 197
- Page 618 and 619: ■INDEX 587 initialization 38 nami
- Page 620 and 621: ■INDEX 589 localtime function tim
- Page 622 and 623: ■INDEX 591 nesting blocks 88 Netw
- Page 624 and 625: ■INDEX 593 playful programming 38
- Page 626 and 627: ■INDEX 595 Python C API 365 hand-
- Page 628 and 629: ■INDEX 597 ■S safe_substitute m
- Page 630 and 631: ■INDEX 599 split function re modu
- Page 632 and 633: ■INDEX 601 TestCase class methods
- Page 634 and 635: ■INDEX 603 further exploration 47
- Page 641: forums.apress.com FOR PROFESSIONALS
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).