Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python 32

Download as pdf or txt
Download as pdf or txt
You are on page 1of 82

Python 3.

2 quick reference

John W. Shipman

Python 3.2 quick reference


by John W. Shipman Abstract A reference guide to most of the common features of the Python programming language, version 3.2. This publication is available in Web form and also as a PDF document . Please forward any comments to tccdoc@nmt.edu. This an unfinished, unreleased, draft version.
1 2

1 2

http://www.nmt.edu/tcc/help/pubs/lang/python32/ http://www.nmt.edu/tcc/help/pubs/lang/python32/python32.pdf

Table of Contents
1. Python 3.2: Introduction ........................................................................................................... 1 1. Python 2.x and 3.x ............................................................................................................ 1 2. Starting Python ................................................................................................................ 1 2.1. Using Python in Windows ..................................................................................... 2 2.2. Using Python in Linux ........................................................................................... 2 2. The syntax of Python code ........................................................................................................ 3 1. Line syntax ...................................................................................................................... 3 2. Names and keywords ...................................................................................................... 3 3. Python types ........................................................................................................................... 5 1. Type None: The special placeholder value .......................................................................... 6 4. Numeric types ......................................................................................................................... 9 1. Type int: Whole numbers ................................................................................................ 9 2. Type float: Approximated real numbers ........................................................................ 10 3. The bool type: Truth values ............................................................................................ 10 4. Type complex: Imaginary numbers ................................................................................. 11 5. Sequence types (ordered sets) ................................................................................................. 13 1. Mutability and immutability ........................................................................................... 13 2. Common operations on sequence types ........................................................................... 14 3. Type str: Strings of text characters .................................................................................. 16 3.1. It's a Unicode world now ..................................................................................... 17 3.2. String constants ................................................................................................... 18 3.3. Definition of whitespace ................................................................................... 20 3.4. Methods on class str ........................................................................................... 20 3.5. The string .format() method .............................................................................. 35 4. Type bytes: Immutable sequences of 8-bit integers .......................................................... 42 5. Type bytearray: Mutable sequences of 8-bit integers ....................................................... 42 6. Type list: Mutable sequences of arbitrary objects ........................................................... 42 7. Type tuple: Immutable sequences of arbitrary objects ...................................................... 42 8. Type range: A range of values ........................................................................................ 42 6. The set types: set and frozenset ........................................................................................... 43 7. Type dict: Dictionaries (mappings) ........................................................................................ 45 8. Operators and expressions ...................................................................................................... 47 1. What is a predicate? ....................................................................................................... 48 2. What is an iterable? ........................................................................................................ 48 3. Duck typing, or: what is an interface? .............................................................................. 48 4. What is the locale? .......................................................................................................... 49 5. Comprehensions ............................................................................................................ 50 9. Built-in functions ................................................................................................................... 51 1. Basic built-in functions ................................................................................................... 51 1.1. abs(): Absolute value .......................................................................................... 51 1.2. ascii(): Convert to 8-bit ASCII ........................................................................... 51 1.3. bool(): Convert to bool type ............................................................................... 51 1.4. complex(): Convert to complex type .................................................................... 51 1.5. input(): Read a string from standard input .......................................................... 51 1.6. int(): Convert to int type ................................................................................... 51 1.7. iter(): Return an iterator for a given sequence ..................................................... 52 1.8. len(): How many elements in a sequence? ........................................................... 52 1.9. max(): What is the largest element of a sequence? .................................................. 52 1.10. min(): What is the smallest element of a sequence? .............................................. 52 1.11. open(): Open a file ............................................................................................ 52 1.12. ord(): What is the code point of this character? ................................................... 52

New Mexico Tech Computer Center

Python 3.2 quick reference

iii

1.13. repr(): Printable representation ......................................................................... 53 1.14. str(): Convert to str type ................................................................................. 53 2. Advanced functions ....................................................................................................... 53 10. Python statements ................................................................................................................ 55 1. Simple statements .......................................................................................................... 55 1.1. The expression statement ..................................................................................... 56 1.2. The assignment statement: name = expression ...................................................... 56 1.3. The assert statement .......................................................................................... 56 1.4. The del statement ................................................................................................ 56 1.5. The import statement .......................................................................................... 56 1.6. The global statement .......................................................................................... 56 1.7. The nonlocal statement ....................................................................................... 56 1.8. The pass statement .............................................................................................. 56 1.9. The raise statement ............................................................................................ 56 1.10. The return statement ......................................................................................... 56 2. Compound statements .................................................................................................... 56 2.1. Python's block structure ....................................................................................... 56 2.2. The break statement: Exit a for or while loop ...................................................... 58 2.3. The continue statement: Jump to the next cycle of a for or while .......................... 58 2.4. The for statement: Iteration over a sequence ......................................................... 58 2.5. The if statement ................................................................................................. 60 2.6. The tryexcept construct .................................................................................. 60 2.7. The with statement .............................................................................................. 60 2.8. The yield statement: Generate one result of a generator ........................................ 60 11. def(): Defining your own functions ...................................................................................... 61 1. The function definition ................................................................................................... 61 2. Calling a function ........................................................................................................... 63 3. A function's local namespace .......................................................................................... 66 4. Iterators: Values that can produce a sequence of values .................................................... 66 5. Generators: Functions that can produce a sequence of values ............................................ 67 6. Decorators ..................................................................................................................... 68 12. Exceptions ........................................................................................................................... 71 13. Classes: invent your own types .............................................................................................. 73 1. Defining a class .............................................................................................................. 73 2. Special methods ............................................................................................................. 73 2.1. .__call__(): What to do when someone calls an instance ..................................... 73 2.2. __getitem__(): Get one item from a sequence or mapping .................................... 73 2.3. .__iter__(): Create an iterator ........................................................................... 73 3. Static methods ............................................................................................................... 73 14. The conversion path from 2.x to 3.x ........................................................................................ 75

iv

Python 3.2 quick reference

New Mexico Tech Computer Center

List of Tables
3.1. Python's built-in types ........................................................................................................... 5 5.1. String escape sequences ....................................................................................................... 19 8.1. Python operator precedence ................................................................................................. 47

New Mexico Tech Computer Center

Python 3.2 quick reference

vi

Python 3.2 quick reference

New Mexico Tech Computer Center

Chapter 1. Python 3.2: A fine general-purpose programming language


The Python programming language is a recent, general-purpose, higher-level programming language. It is available for free and runs on pretty much every current platform. This document is a reference guide, 1 a tutorial. If you are new to Python programming, see the tunot torial written by Guido van Rossum , the inventor of Python. Not every feature of Python is covered here. If you are interested in exploring some of the3more remote 2 corners of the language, refer to the official standard library and language reference documents. Bookmark the standard library right away if you haven't already; you will find it most useful. The language reference is formal and technical and will be of interest mainly to specialists.

1. Python 2.x and 3.x


Since the language's inception in the early 1990s, the maintainers have been careful to add new features in a way that minimized the number of cases where old code would not function under the new release. The 3.0 version was the first to violate this rule. A number of language features that were compatible through the 2.7 release have been taken out in the 3.x versions. However, this is unlikely to happen again; it is mainly a one-time cleanup and simplification of the language. Since 3.0, the Python language is actually smaller and more elegant. Furthermore, the upgrade path from 2.7 (the last major 2.x release) to the 3.x versions is straightforward and to a large extent automated.

Important
There is no hurry to convert old 2.x programs to 3.x! The 2.7 release will certainly be maintained for many years. Your decision about when to convert may depend on the porting of any third-party library modules you use. In any case, the conversion process is discussed in Chapter 14, The conversion path from 2.x to 3.x (p. 75).

2. Starting Python
You can use Python in two different ways: In calculator or conversational mode, Python will prompt you for input with three greater-than signs (>>>). Type a line and Python will print the result. Here's an example:
>>> 2+2 4

1 2 3

http://docs.python.org/py3k/tutorial/ http://docs.python.org/py3k/library/ http://docs.python.org/py3k/reference/

New Mexico Tech Computer Center

Python 3.2: Introduction

>>> 1.0 / 7.0 0.14285714285714285

You can also use Python to write a program, sometimes called a script. How you start Python depends on your platform. To install Python on your own workstation, refer to the Python downloads page . On a Tech Computer Center Windows workstation, see Section 2.1, Using Python in Windows (p. 2). On a TCC Linux workstation, see Section 2.2, Using Python in Linux (p. 2).
4

2.1. Using Python in Windows


Under Windows, we recommend the IDLE integrated development environment for Python work. If you are using Python at the NM Tech Computer Center (TCC), you can get conversational mode from Start All Programs Python 3.2 IDLE (Python GUI). You will see the usual >>> interactive prompt. You can use conversational mode in this window. To construct a Python script, use File New Window. Write your script in this window, then save it with File Save As...; make sure your file name ends with .py. Then use Run Run Module (or F5) to run your script. The output will appear in the conversational-mode window. You may also run a Python script by double-clicking on it, provided that its name ends with .py.

2.2. Using Python in Linux


To enter conversational mode on a Linux system, type this command:
python3

Type Control-D to terminate the session. If you write a Python script named filename.py, you can execute it using the command
python3 filename.py arguments...

Under Unix, you can also make a script self-executing by placing this line at the top:
#!/usr/bin/env python3

You must also tell Linux that the file is executable by using the command chmod +x filename. For example, if your script is called hello.py, you would type this command:
chmod +x hello.py

http://python.org/download/

Python 3.2: Introduction

New Mexico Tech Computer Center

Chapter 2. The syntax of Python code


1. Line syntax
The comment character is #; comments are terminated by end of line. Long lines may be continued by ending the line with a backslash (\), but this is not necessary if there is at least one open (, [, or {.

2. Names and keywords


Python names (also called identifiers) can be any length and follow these rules: The first or only character must be a letter (uppercase or lowercase) or the underbar character, _. Any additional characters may be letters, underbars, or digits. Examples: coconuts, sirRobin, blanche_hickey_869, __secretWord. Case is significant in Python. The name Robin is not the same name as robin. You can use non-ASCII Unicode characters as Python names. For details, see the the reference docu1 mentation . The names below are keywords, also known as reserved words. They have special meaning in Python and cannot be used as names or identifiers.
False None True and as assert break class continue def del elif else except finally for from global if import in is lambda nonlocal not or pass raise return try while with yield

Certain names starting with the underbar character (_) are reserved. The name _ is available during conversational mode to refer to the result of the previous computation. In a script, it has no special meaning and is not defined. Many names starting and ending with two underbars (__...__) have special meaning in Python. It is best not to use such names for your own code. Names starting with one underbar are not imported by the import statement. Names starting with two underbars are private to the class in which they are declared. This is useful for distinguishing names in a class from names in its parent classes. Such names are not actually hidden; they are mangled in a prescribed way. The mangling prevents conflicts with names in parent classes, while still making them visible to introspective applications such as the Python debugger.

http://docs.python.org/py3k/reference/lexical_analysis.html#identifiers

New Mexico Tech Computer Center

The syntax of Python code

The syntax of Python code

New Mexico Tech Computer Center

Chapter 3. Python types


All the data you manipulate in Python consists of objects and names. An object represents some value. It may be a simple value like the number 17, or a very complex value, like an object that describes the results of every cricket game ever played between England and Australia. Every object has a type. Python has a rich set of built-in types. You can also invent your own types using Python's object-oriented programming features. The type of an object determines what operations can be performed on it. For example, an object of Python's int (integer) type holds a whole number, and you can use operators like + and - to add and subtract objects of that type. A Python name is like a luggage tag: it is a handle you use to manipulate the values contained in an object. For the rules Python uses for names, see Section 2, Names and keywords (p. 3).

Important
Unlike many current languages such as C and C++, a Python name is not associated with a type. A name is associated with an object, and that object has a type. The association between a name and an object is called a binding. For example, after executing this statement,
x = 17

we say that the name x is bound to the object 17, which has type int. There is no reason that name x can't be associated with an integer at one point and a completely different type later in the execution of a script. (It is, however, bad practice. Programs are more clear when each name is used for only one purpose.) Here is a list of Python's built-in primitive types. To learn how to invent your own types, see Chapter 13, Classes: invent your own types (p. 73). For a discussion of the distinction between mutable and immutable types, see Section 1, Mutability and immutability (p. 13).

Table 3.1. Python's built-in types


Type name Values
bool

Examples

The two Boolean values True and False. See Sec- True, False tion 3, The bool type: Truth values (p. 10). Integers of any size, limited only by the available 42, -3, memory. See Section 1, Type int: Whole num- 12345678901234567890123456789 bers (p. 9). Floating-point numbers; see Section 2, Type float: 3.14159, -1.0, 6.0235e23 Approximated real numbers (p. 10).

Chapter 4, Numeric types (p. 9)


int

float

New Mexico Tech Computer Center

Python types

Type name Values


complex

Examples

Complex numbers. If the idea of computing with the (3.2+4.9j), (0+3.42e-3j) square root of -1 bothers you, just ignore this type, otherwise see Section 4, Type complex: Imaginary numbers (p. 11). Strings (sequences of zero or more Unicode charac- 'Sir Robin', "xyz", "I'd've", ters); see Section 3, Type str: Strings of text charac- '\u262e\u262f' ters (p. 16). Strings can be empty: write these as "" or ''. Immutable sequences of zero or more positive in- b'git', bytes([14, 202, 6]) tegers in the range [0, 255]. See Section 4, Type bytes: Immutable sequences of 8-bit integers (p. 42). Mutable sequences of zero or more positive integers bytearray(), bytein the range [0, 255]. See Section 5, Type bytearray: array(b'Bletchley') Mutable sequences of 8-bit integers (p. 42). A mutable sequence of values; see Section 6, Type ['dot', 'dash']; [] list: Mutable sequences of arbitrary objects (p. 42). An immutable sequence of values; see Section 7, ('dot', 'dash'); (); Type tuple: Immutable sequences of arbitrary ob- ("singleton",) jects (p. 42). An unordered, immutable set of zero or more distinct set(), set('red', 'yellow', values. 'green') An unordered, mutable set of zero or more distinct frozenset([3, 5, 7]), values. frozenset() Use dict values (dictionaries) to structure data as {'go':1, 'stop':0}, {} look-up tables; see Chapter 7, Type dict: Dictionaries (mappings) (p. 45). A special, unique value that may be used where a None value is required but there is no obvious value. See Section 1, Type None: The special placeholder value (p. 6).

Chapter 5, Sequence types (ordered sets) (p. 13)


str

bytes

bytearray

list tuple

Chapter 6, The set types: set and frozenset (p. 43)


set frozenset dict

None

1. Type None: The special placeholder value


Python has a unique value called None. This special null value can be used as a placeholder, or to signify that some value is unknown. This value has its own type and is the only instance of that type. In conversational mode, any expression that evaluates to None is not printed. However, if a value of None is converted to a string, the result is the string 'None'; this may happen, for example, in a print statement.
>>> x = None >>> x >>> print(x) None

Python types

New Mexico Tech Computer Center

The value None is returned from any function that executes a return statement with no value, or any function after it executes its last line if that last line is not a return statement.
>>> def useless(): ... print("Useless!") ... >>> useless() Useless! >>> z=useless() Useless! >>> z >>> print(z) None >>> def boatAnchor(): ... pass ... >>> x=boatAnchor() >>> x >>> print(x) None

New Mexico Tech Computer Center

Python types

Python types

New Mexico Tech Computer Center

Chapter 4. Numeric types


Python has these built-in types for representing numbers. Section 1, Type int: Whole numbers (p. 9). Section 2, Type float: Approximated real numbers (p. 10). Section 4, Type complex: Imaginary numbers (p. 11).

1. Type int: Whole numbers


A Python object of type int represents an integer, that is, a signed whole number. The range of int values is limited only by the available memory. To write an int constant, you may use several different formats. A zero is written as just 0. To write an integer in decimal (base 10), the first digit must not be zero. Examples: 17, 100000000000000. To write an integer in octal (base 8), precede it with 0o. Examples: 0o177, 0o37.

Note
In Python 2.x versions, any number starting with a 0 followed by other digits was considered octal. This convention is no longer allowed in Python 3.x.
>>> 0o37 31 >>> 0o177 127 >>> 00004 File "<stdin>", line 1 00004 ^ SyntaxError: invalid token

To write an integer in hexadecimal (base 16), precede it with 0x or 0X. Examples: 0x7f, 0X1000. To write an integer in binary (base 12), precede it with 0b or 0B. Examples: 0b1001, 0B111110011101. To produce a negative number, use the unary - operator before the number. Note that this is an operator and not part of the constant.
>>> 100 - -5 105 >>> 100 - (-5) 105

To convert a string value to an int, see Section 1.6, int(): Convert to int type (p. 51).

New Mexico Tech Computer Center

Numeric types

2. Type float: Approximated real numbers


Values of this type represent real numbers, with the usual limitations of IEEE-754 floating point type: it cannot represent very large or very small numbers, and the precision is limited to only about 15 digits. 1 For complete details on the IEEE-754 standard and its limitations, see the Wikipedia article . To write a float constant, use at least one digit, plus either a decimal point or an exponent or both. The decimal point, if present, must be preceded or followed by one or more digits. Examples:
3.14 0.0 1. .1

Just for the sake of legibility, we recommend that you use at least one digit on both sides of the decimal point. Depending on the context, a reader might wonder whether a number such as .1 is one-tenth, or the number one preceded by an errant particle of pepper on the paper or monitor. The exponent, if present, consists of either e or E, optionally followed by a + or - sign, followed by at least one digit. The resulting value is equal to the part before the exponent, times ten to the 2 power of the exponent, that is, scientific notation . For example, Avogadro's Number gives the number of atoms of carbon in 12 grams of carbon , and 23 is written as 6.022141810 . In Python that would be 6.0221418e23.
12

3. The bool type: Truth values


A value of bool type represents a Boolean (true or false) value. There are only two values, written in Python as True and False. Internally, True is represented as 1 and False as 0, and they can be used in numeric expressions as those values. Here's an example. In Python, the expression a < b compares two values a and b, and returns True if a is less than b, False is a is greater than or equal to b.
>>> 2 < 3 True >>> 3 < 2 False >>> True+4 5 >>> False * False 0

These values are considered False wherever true/false values are expected, such as in an if statement: The bool value False. Any numeric zero: the int value 0, the float value 0.0, the long value 0L, or the complex value 0.0j. Any empty sequence: the str value '', the unicode value u'', the empty list value [], or the empty tuple value (). Any empty mapping, such as the empty dict (dictionary) value {}. The special value None.
1 2

http://en.wikipedia.org/wiki/IEEE_754-2008 http://en.wikipedia.org/wiki/Scientific_notation

10

Numeric types

New Mexico Tech Computer Center

All other values are considered True. To convert any value to a Boolean, see Section 1.3, bool(): Convert to bool type (p. 51).

4. Type complex: Imaginary numbers


Mathematically, a complex number is a number of the form A+Bi where i is the imaginary number, equal to the square root of -1. Complex numbers are quite commonly used in electrical engineering. In that field, however, because the symbol i is used to represent current, they use the symbol j for the square root of -1. Python adheres to this convention: a number followed by j is treated as an imaginary number. Python displays complex numbers in parentheses when they have a nonzero real part.
>>> 5j 5j >>> 1+2.56j (1+2.56j) >>> (1+2.56j)*(-1-3.44j) (7.8064-6j)

Unlike Python's other numeric types, complex numbers are a composite quantity made of two parts: the real part and the imaginary part, both of which are represented internally as float values. You can retrieve the two components using attribute references. For a complex number C: C.real is the real part. C.imag is the imaginary part as a float, not as a complex value.
>>> a=(1+2.56j)*(-1-3.44j) >>> a (7.8064-6j) >>> a.real 7.8064 >>> a.imag -6.0

To construct a complex value from two float values, see Section 1.4, complex(): Convert to complex type (p. 51).

New Mexico Tech Computer Center

Numeric types

11

12

Numeric types

New Mexico Tech Computer Center

Chapter 5. Sequence types (ordered sets)


Python's sequence types str, bytes, bytearray, list, tuple and range share a number of common operations (see Section 2, Common operations on sequence types (p. 14)). Objects of each of these types share a common algebraic abstraction: the ordered set. Among the properties of an ordered set are: An ordered set may be empty, or it may contain one or more objects. The contained objects don't all have to be different. For example, the list [1, 2, 2, 2] contains three copies of the int value 2. If an ordered set has more than object, they can be numbered: there is a first one, a second one, and so on. Here are the sections describing Python's sequence types. Section Section Section Section Section Section Section Section 3.1, It's a Unicode world now (p. 17). 3, Type str: Strings of text characters (p. 16). 4, Type bytes: Immutable sequences of 8-bit integers (p. 42). 5, Type bytearray: Mutable sequences of 8-bit integers (p. 42). 6, Type list: Mutable sequences of arbitrary objects (p. 42). 7, Type tuple: Immutable sequences of arbitrary objects (p. 42). 8, Type range: A range of values (p. 42). 2, Common operations on sequence types (p. 14).

1. Mutability and immutability


One important quality of a Python type is whether it is considered mutable or immutable. An object is considered mutable if part of its contained value can be changed. For example, of Python's two universal container types, list objects are mutable, but tuple objects are not.
>>> some_list = [2, 3, 5, 7, 11] >>> some_list[2] = 888 >>> some_list [2, 3, 888, 7, 11] >>> some_tuple = (2, 3, 5, 7, 11) >>> some_tuple[2] = 888 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment

For another example, str values are immutable. You cannot change one character within a string of three characters:
>>> s = "abc" >>> s[1] = "x" Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'str' object does not support item assignment

New Mexico Tech Computer Center

Sequence types (ordered sets)

13

Of Python's two types that represent strings of 8-bit bytes, objects of the bytes type are immutable, but bytearray objects are mutable.
>>> v1 b'aeiou' >>> v2=bytearray(v1) >>> print(v1, v2) b'aeiou' bytearray(b'aeiou') >>> v2.append(ord('y')) >>> v2 bytearray(b'aeiouy') >>> v1.append(ord('y')) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'bytes' object has no attribute 'append'

2. Common operations on sequence types


A number of Python operators, functions, and methods operate on all of the sequence types: str, bytes, bytearray, list, tuple, and range. Functions include: Section Section Section Section 1.7, iter(): Return an iterator for a given sequence (p. 52). 1.8, len(): How many elements in a sequence? (p. 52). 1.10, min(): What is the smallest element of a sequence? (p. 52). 1.9, max(): What is the largest element of a sequence? (p. 52).

Additionally, these methods are supported on any value s that is one of the sequence types.
s.count(k)

Returns the number of elements of s that are equal to k.


>>> [0, 1, 0, 0, 0, 5].count(0) [0, 1, 0, 0, 0, 5].count(0) 4 >>> b'abracadabra'.count(b'a') b'abracadabra'.count(b'a') 5
s.index(k)

Returns the position of the first element of s that is equal to k. If no element matches, this function raises a ValueError exception.
>>> 'abcde'.index('d') 3 >>> 'abcde'.index('x') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found >>> range(18).index(14) 14

These operators apply to sequences.


S1+S2

Concatenationfor two sequences S1 and S2 of the same type, a new sequence containing all the elements from S1 followed by all the elements of S2.

14

Sequence types (ordered sets)

New Mexico Tech Computer Center

>>> "vi" + "car" 'vicar' >>> [1,2,3]+[5,7,11,13]+[15] [1, 2, 3, 5, 7, 11, 13, 15] >>> ('roy', 'g')+('biv',) ('roy', 'g', 'biv')
S*n

For a sequence S and a positive integer n, the result is a new sequence containing all the elements of S repeated n times.
>>> 'worra'*8 'worraworraworraworraworraworraworraworra' >>> [0]*4 [0, 0, 0, 0] >>> (True, False)*5 (True, False, True, False, True, False, True, False, True, False)
x in S

Is any element of a sequence S equal to x? For convenience in searching for substrings, if the sequence to be searched is a string, the x operand can be a multi-character string. In that case, the operation returns True if x is found anywhere in S.
>>> 1 in [2,4,6,0,8,0] False >>> 0 in [2,4,6,0,8,0] True >>> 'a' in 'banana' True >>> 3.0 in (2.5, 3.0, 3.5) True >>> "baz" in "rowrbazzle" True
x not in S

Are all the elements of a sequence S not equal to x?


>>> 'a' not in 'banana' False >>> 'x' not in 'banana' True
S[i]

Subscripting: retrieve the ith element of s, counting from zero. If i is greater than or equal to the number of elements of S, an IndexError exception is raised.
>>> 'Perth'[0] 'P' >>> 'Perth'[1] 'e' >>> 'Perth'[4] 'h' >>> 'Perth'[5] Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: string index out of range >>> ('red', 'yellow', 'green')[2] 'green'

New Mexico Tech Computer Center

Sequence types (ordered sets)

15

S[i:j]

Slicing: For a sequence S and two integers i and j, return a new sequence with copies of the elements of S between positions i and j. The values used in slicing refer to the positions between elements, where position zero is the position before the first element; position 1 is between the first and second element; and so on. You can also specify positions relative to the end of a sequence. Position -1 is the position before the last element; -2 is the position before the second-to-last element; and so on. You can omit the starting position to obtain a slice starting at the beginning. You can omit the ending position to get all the elements through the last. For example, here is a diagram showing three slices of the string 'abcdef'.

>>> 'abcdef'[2:5] 'cde' >>> 'abcdef'[:3] 'abc' >>> 'abcdef'[3:] 'def' >>> (90, 91, 92, 93, 94, 95)[2:5] (92, 93, 94)
S[i:j:k]

You can use a slice expression like this to select every kth element. Examples:
>>> teens = range(13,20) >>> teens [13, 14, 15, 16, 17, 18, 19] >>> teens[::2] [13, 15, 17, 19] >>> teens[1::2] [14, 16, 18] >>> teens[1:5] [14, 15, 16, 17] >>> teens[1:5:2] [14, 16]

3. Type str: Strings of text characters


A value of Python's str type is a sequence of zero or more 32-bit Unicode characters (see Section 3.1, It's a Unicode world now (p. 17)). In addition to the functions described in Section 2, Common operations on sequence types (p. 14), these built-in functions apply to strings: Section 1.5, input(): Read a string from standard input (p. 51). Section 1.12, ord(): What is the code point of this character? (p. 52). Section 1.14, str(): Convert to str type (p. 53).

16

Sequence types (ordered sets)

New Mexico Tech Computer Center

3.1. It's a Unicode world now


For the first several decades of software history, the processing of text in the USA almost everywhere 1 used the 7-bit ASCII code or various 8-bit codes. To this day, most physical input and output uses streams of 8-bit bytes. However, for so many reasons, but especially for internationalization of interfaces, it is time to2abandon this tiny character set and join the wider world. The 32-bit characters of the Unicode standard provide enough characters to last us for many years into the future. In all the Python 3.x versions, there is a strict separation of 32-bit character data and sequences of 8-bit bytes. All character data is represented as type str. An object of this type represents an ordered set of 32bit Unicode characters. Ordered sets of 8-bit types may use either type bytes (for immutable values) or type bytearray (for mutable values; see Section 1, Mutability and immutability (p. 13)). To convert from one to the other, you must specify an encoding, that is, a system for converting between 3 8-bit and 32-bit representations. There are many encoding systems, but in most cases the UTF-8 encoding is a reasonable default, compatible with most current Web practice.

To encode data: The str.encode() method allows you to specify the encoding; see Section 3.4, Methods on class str (p. 20). The built-in bytes() and bytearray() functions allow you to specify an encoding; see Section 4, Type bytes: Immutable sequences of 8-bit integers (p. 42) and Section 5, Type bytearray: Mutable sequences of 8-bit integers (p. 42). When you open a file, you can specify an encoding, and this encoding will be used to translate any 32-bit data that gets written to that file. See Section 1.11, open(): Open a file (p. 52). To decode data: The str() built-in function allows you to specify an encoding that will be used to decode 8-bit data as Unicode. See Section 3, Type str: Strings of text characters (p. 16). The bytes.decode() and bytearray.decode() functions allow you to specify an encoding; see Section 4, Type bytes: Immutable sequences of 8-bit integers (p. 42) and Section 5, Type bytearray: Mutable sequences of 8-bit integers (p. 42). What happens when you read data from a file depends on how the file was opened. If it is character data, you may specify an encoding in the call to the open() function, or use the local site default encoding.

1 2 3

http://en.wikipedia.org/wiki/ASCII http://en.wikipedia.org/wiki/Unicode http://en.wikipedia.org/wiki/UTF-8

New Mexico Tech Computer Center

Sequence types (ordered sets)

17

If you are handling byte data, you may include 'b' in the mode argument to the open() function, and you will get back data of type bytes.

3.2. String constants


There are many ways to write constants of type str. You may enclose them in single-quote characters.
>>> s = 'Semprini' >>> print(s) Semprini

You may enclose them between double-quote characters.


>>> t="Fishy requisite" >>> print(t) Fishy requisite

Strings enclosed in single- or double-quotes may not span multiple lines:


>>> u = "This string is unterminated. File "<stdin>", line 1 u = "This string is unterminated. ^ SyntaxError: EOL while scanning string literal

You can enclose a string inside either three single-quote or three double-quote characters. This form allows you to include line breaks (newline characters) within the string.
>>> req = """I'll have the ... crab, tiger, ... and almond requisite.""" >>> print(req) I'll have the crab, tiger, and almond requisite. >>> req "I'll have the\ncrab, tiger,\nand almond requisite." >>> silly = '''It's not pronounced Raymond Luxury Yacht, ... it's pronounced "Throat-wobbler Mangrove."''' >>> print(silly) It's not pronounced Raymond Luxury Yacht, it's pronounced "Throat-wobbler Mangrove."

Note that in these triply-quoted strings, you may use single-quote and double-quote characters inside them. One convenient feature of Python is that if two string constants are adjacent in your program, they are implicitly concatenated. This allows you to break long strings over multiple lines, and even to intersperse comments among them:
>>> wordPat = ( ... '[a-zA-Z_]' ... "[a-zA-Z_']*") >>> print(wordPat) [a-zA-Z_][a-zA-Z_']* # Any letter or underbar # Zero or more letters, underbars, or apostrophe

18

Sequence types (ordered sets)

New Mexico Tech Computer Center

3.2.1. Escape sequences


Within a string constant, the backslash (\) character has special meaning as an escape character: it modifies the meaning of the following character. Here is a table of the escape sequences allowed in Python string literals.

Table 5.1. String escape sequences


\newline \\ \' \" \a \b \f \n \r \t \v \xhh \uhhhh \Uhhhhhhhh \N{uname} \ooo

A backslash at the end of a line is ignored. Backslash (\) Closing single quote (') Double-quote character (") Attention (ASCII BEL) Backspace (in ASCII, the BS character) Formfeed (ASCII FF) Newline (ASCII LF or linefeed) Carriage return (ASCII CR) Horizontal tab (ASCII HT) Vertical tab (ASCII VT) The character with hexadecimal value hh, e.g., '\xFF'. You must always provide exactly two hexadecimal digits. The character with the Unicode code point value hhhh in hexadecimal. You must provide exactly four hexadecimal digits. The character with the Unicode code point value hhhhhhhh in hexadecimal. You must provide exactly eight hexadecimal digits. The character whose official Unicode code point's full name is uname. The character with octal code ooo, e.g., '\177'. You may use one, two, or three octal digits.

Some examples:
>>> s='pepper\ ... pot' >>> s 'pepperpot' >>> null='\x00' >>> null '\x00' >>> ord(null) 0 >>> ord('\xff') 255 >>> ord('\177') 127 >>> gamma='\u03b3' >>> ord(gamma) 947 >>> gamma2 = '\N{GREEK SMALL LETTER GAMMA}' >>> ord(gamma2) 947 >>> thingy = '\N{MATHEMATICAL SCRIPT CAPITAL Y}' >>> hex(ord(thingy))

New Mexico Tech Computer Center

Sequence types (ordered sets)

19

'0x1d4b4' >>> thingy2 = '\U0001D4B4' >>> thingy == thingy2 True

3.2.2. Literals of type bytes


To write a literal using the 8-bit bytes type, use the same syntax is string literals, but prefix the string with either b or B. Characters within the literal may not exceed ordinal 127.
>>> test3 = b'Pratt' >>> len(test3) 5 >>> type(test3) <class 'bytes'> >>> type(B'Pennine Gang') <class 'bytes'>

3.2.3. Raw strings


If you are using strings that contain a lot of backslash (\) characters, it can be inconvenient to escape them as \\ each time. Python allows you to write raw strings, in which the backslashes are treated as ordinary characters: just prefix the string with either r or R. Examples:
>>> latex = 'M\\"{o}\\"{o}se' >>> print(latex) M\"{o}\"{o}se >>> rawLatex = r'M\"{o}\"{o}se' >>> print(rawLatex) M\"{o}\"{o}se

3.3. Definition of whitespace


In Python, these characters are considered whitespace: Escape sequence
' ' '\n' '\r' '\t' '\f' '\v'

ASCII name
SP NL CR HT FF VT

English name space newline carriage return horizontal tab form feed vertical tab

3.4. Methods on class str


All string values S (instances of type str) support the methods enumerated below.

http://en.wikipedia.org/wiki/ASCII

20

Sequence types (ordered sets)

New Mexico Tech Computer Center

3.4.1. S.capitalize()
Return S with its first character capitalized (if a letter).
>>> 'e e cummings'.capitalize() 'E e cummings' >>> '---abc---'.capitalize() '---abc---'

3.4.2. S.center(): Center a string


Returns a new string containing S, centered in a field of a given length. The general form:
S.center(w[, fill])

The argument w is the desired width. If S is that wide or wider, the method returns S. If len(S) < w, then fill characters are added before and after the value of S so that the result has length w. If the number of fill characters is odd, the extra fill character will placed after the centered value. The fill argument is a single character to be used to pad the value of S to length. The default value is one space. Examples:
>>> 'x'.center(4) ' x ' >>> 'x'.center(11, '/') '/////x/////'

3.4.3. S.count(): Count substring occurrences


Return the number of times a given string occurs in a string S. Here is the general form:
S.count(t[,start[,end]]) t

The string you are looking for within S.


start

If given, this integer defines the starting position of the search, counting from zero.
end

If given, this integer limits the search to strings ending at that position.
>>> 3 >>> 3 >>> 2 >>> 1 >>> 1 'banana'.count('a') 'bananana'.count('na') 'banana'.count('a', 3) 'banana'.count('a', 3, 5) 'banana'.count('ana')

Note in the last example above how this function counts only the number of non-overlapping occurrences of the string.

New Mexico Tech Computer Center

Sequence types (ordered sets)

21

3.4.4. S.encode(): Convert to bytes type


Encodes a Unicode string and returns the encoded form as a value of type bytes. See Section 4, Type bytes: Immutable sequences of 8-bit integers (p. 42), and also be sure you familiarize yourself with Section 3.1, It's a Unicode world now (p. 17), especially if you are familiar with the very different character handling in Python 2.x versions. Here is the general form:
S.encode([encoding[, errors]])

The default encoding is 'utf-8'. Many encodings are supported; for a full list, see the standard library 5 documentation for a current list. Here are some popular values:
'utf-8' 'utf-16' 'utf-32' 'ascii'

The standard UTF-8 encoding. This is the default encoding. The standard UTF-16 encoding. The standard UTF-32 encoding. ASCII , for the American Standard Code for Information Interchange.
6

In the examples below, '\xa0' is the Unicode character &nbsp;, non-breaking space, which has no ASCII equivalent.
>>> s='(\xa0)' s='(\xa0)' >>> s.encode() s.encode() b'(\xc2\xa0)' >>> s.encode('utf-8') s.encode('utf-8') b'(\xc2\xa0)' >>> s.encode('utf-16') s.encode('utf-16') b'\xff\xfe(\x00\xa0\x00)\x00' >>> s.encode('utf-32') s.encode('utf-32') b'\xff\xfe\x00\x00(\x00\x00\x00\xa0\x00\x00\x00)\x00\x00\x00'

The optional errors second argument specifies what to do when a character cannot be encoded.
'strict'

Raise a UnicodeEncodeError exception if any of the characters can't be encoded using the specified encoding. This is the default value of the errors argument.
>>> s = "(\xa0)" >>> s.encode('ascii') Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeEncodeError: 'ascii' codec can't encode character '\xa0' in position 1: ordinal not in range(128) 'ignore'

Omit from the result any characters that can't be encoded.


>>> s = "(\xa0)" >>> s.encode('ascii', 'ignore') b'()'

5 6

http://docs.python.org/py3k/library/codecs.html#standard-encodings http://en.wikipedia.org/wiki/ASCII

22

Sequence types (ordered sets)

New Mexico Tech Computer Center

'replace'

In the result, display as ? any character that can't be encoded.


>>> "(\xa0)".encode('ascii', 'replace') b'(?)' 'xmlcharrefreplace'

For each character that can't be encoded, substitute an XML character entity reference of the form &#N;, where N is the decimal value of the Unicode code point of the character. This rendering is a good choice for displaying Unicode characters in HTML.
>>> '(\xa0)'.encode('ascii', 'xmlcharrefreplace') b'(&#160;)' 'backslashreplace'

In the result, render characters that cannot be encoded using Python's backslash escape convention: '\xNN' for 8-bit code points, '\uNNNN' for 16-bit code points, and '\UNNNNNNNN' for 32-bit code points.
>>> zoot='\xa0\u1234\U00012345';print(len(zoot)) 3 >>> zoot.encode('ascii', 'backslashreplace') b'\\xa0\\u1234\\U00012345'

3.4.5. S.endswith()
A predicate that tests whether S ends with a given string. Here's the general form:
S.endswith(t[,start[,end]]) t

The method checks whether S ends with this string.


start

If given, specifies the starting position, counting from zero, of the string to be tested.
end

If given, specifies the ending position of the string to be tested.


>>> 'bishop'.endswith('shop') True >>> 'bishop'.endswith('bath and wells') False >>> 'bishop'[3:5] 'ho' >>> 'bishop'.endswith('o', 3, 5) True

3.4.6. S.expandtabs()
Returns a copy of S with all tabs replaced by one or more spaces. The general form:
S.expandtabs([tabsize])

Each tab is interpreted as a request to move to the next tab stop. The optional tabsize argument specifies the number of spaces between tab stops; the default is 8.

New Mexico Tech Computer Center

Sequence types (ordered sets)

23

Here is how the function actually works. The characters of S are copied to a new string T one at a time. If the character is a tab, it is replaced by enough tabs so the new length of T is a multiple of the tab size (but always at least one space).
>>> 'X\tY\tZ'.expandtabs() 'X Y Z' >>> 'X\tY\tZ'.expandtabs(4) 'X Y Z' >>> print('+...'*8, '\n', 'a\tbb\tccc\tdddd\teeeee\tf'.expandtabs(4), ... sep='') +...+...+...+...+...+...+...+... a bb ccc dddd eeeee f

3.4.7. S.find(): Search for a substring


The purpose of this method is to find some string t inside S. The general form is:
S.find(t[,start[,end]])

If string t is not found in S, the method returns -1. Otherwise it returns the index of the first position in S that matches t. The optional start and end arguments restrict the search to slice S[start:end].
>>> 1 >>> -1 >>> 4 >>> -1 'banana'.find('an') 'banana'.find('ape') 'banana'.find('n', 3) 'council'.find('c', 1, 4)

If you are testing whether a certain substring is found within a larger string, but you don't care exactly where it starts, see the in and not in operators in Section 2, Common operations on sequence types (p. 14).

3.4.8. S.format()
See Section 3.5, The string .format() method (p. 35).

3.4.9. S.format_map(): Format from a mapping


The purpose of this method is to substitute values from a dictionary into a format string. The general form:
S.format_map(m)

This method works similarly to S.format(**m). The argument is a dictionary or other mapping that provides values to be substituted into a format string S. See Section 3.5, The string .format() method (p. 35).
>>> rounders = {'knight': 'Sir Robin', 'steed': 'Patsy'} >>> speech = "Brave {knight}, take {steed} and let us away!" >>> speech.format_map(rounders) 'Brave Sir Robin, take Patsy and let us away!'

24

Sequence types (ordered sets)

New Mexico Tech Computer Center

3.4.10. S.index(): Find a substring


This method, like .find(), is used to locate a substring within S. The general form:
S.index(t[,start[,end]])

The arguments have the same meaning as in Section 3.4.7, S.find(): Search for a substring (p. 24). However, if t is not found, the method raises a ValueError exception.
>>> 'council'.index('un') 2 >>> 'council'.index('phd') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found

3.4.11. S.isalnum(): Test for alphanumeric


This method is a predicate that tests whether S is nonempty and all its characters are alphanumeric.
>>> ''.isalnum() False >>> 'abc123'.isalnum() True >>> '&*$#&*()abc123'.isalnum() False

3.4.12. S.isalpha(): Test for alphabetic


Predicate that tests whether S is nonempty and all its characters are letters.
>>> 'abc123'.isalpha() False >>> 'MaryRecruiting'.isalpha() True >>> ''.isalpha() False >>> da = '\N{CYRILLIC CAPITAL LETTER DE}\N{CYRILLIC SMALL LETTER A}' >>> len(da) 2 >>> da.isalpha() True

3.4.13. S.isdecimal(): Test for decimal


Similar to Section 3.4.14, S.isdigit(): Test for digits (p. 26), but considers characters in the Unicode category Nd to be digits as well as the usual 0 through 9.
>>> "17".isdecimal() True >>> "0xFF".isdecimal() False >>> s = '\N{ARABIC-INDIC DIGIT FIVE}' >>> s.isdecimal() True

New Mexico Tech Computer Center

Sequence types (ordered sets)

25

3.4.14. S.isdigit(): Test for digits


Predicate that tests whether S is nonempty and all its characters are digits.
>>> 'abc123'.isdigit() False >>> ''.isdigit() False >>> '2415'.isdigit() True

3.4.15. S.isidentifier(): Test for identifier


A predicate that tests whether a S is a valid Python identifier.
>>> "lionTamer".isidentifier() True >>> "spiny_norman".isidentifier() True >>> "_42".isidentifier() True >>> "9_a".isidentifier() False

3.4.16. S.islower(): Test for lowercase


Predicate that tests whether S is nonempty and all its letters are lowercase (non-letter characters are ignored).
>>> ''.islower() False >>> 'abc123'.islower() True >>> 'ABC123'.islower() False

3.4.17. S.isnumeric(): Test for numeric


This method is a predicate that tests whether S is nonempty and contains only characters that are considered numeric characters in Unicode.
>>> '123'.isnumeric() True >>> "123x456".isnumeric() False >>> ''.isnumeric() False >>> '\N{VULGAR FRACTION FIVE SIXTHS}'.isnumeric() True

3.4.18. S.isprintable(): Test for printable


A predicate that tests whether S is either empty or contains only printable characters. In the Unicode specification, a character is considered printable if it is not in classes Other or Separator, except for the space character which is a Separator but is considered printable.

26

Sequence types (ordered sets)

New Mexico Tech Computer Center

>>> '\xa0'.isprintable() False >>> 'abc def $*^'.isprintable() True >>> ''.isprintable() True

3.4.19. S.isspace(): Test for whitespace


Predicate that tests whether S is nonempty and all its characters are whitespace characters. In the example below, '\xa0' is a Unicode non-breaking space, which is considered a space character.
>>> ''.isspace() False >>> ' \t\r\n\xa0'.isspace() True >>> 'killer \t \n rabbit'.isspace() False

3.4.20. S.istitle(): Test for title case


A predicate that tests whether S is nonempty and contains only words in title case. In a title-cased string, uppercase characters may appear only at the beginning of the string or after some character that is not a letter. Lowercase characters may appear only after an uppercase letter.
>>> 'abc def GHI'.istitle() False >>> "Abc Def G Hij".istitle() True >>> ''.istitle() False

3.4.21. S.isupper(): Test for uppercase


Predicate that tests whether S is nonempty and all its letters are uppercase letters (non-letter characters are ignored).
>>> 'abcDEF'.isupper() False >>> '123GHI'.isupper() True >>> ''.isupper() False

3.4.22. S.join(): Concatenate multiple strings


The purpose of this method is to take a sequence of strings and form them into one string by concatenating them all. Here is the general form:
S.join(L)

must be an iterable that produces a sequence of strings. The returned value is a string containing the members of the sequence with copies of the delimiter string S inserted between them.
L

New Mexico Tech Computer Center

Sequence types (ordered sets)

27

One quite common operation is to use the empty string as the delimiter to concatenate the elements of a sequence. Examples:
>>> '/'.join(['never', 'pay', 'plan']) 'never/pay/plan' >>> '(***)'.join ( ('Property', 'of', 'the', 'zoo') ) 'Property(***)of(***)the(***)zoo' >>> ''.join(['anti', 'dis', 'establish', 'ment', 'arian', 'ism']) 'antidisestablishmentarianism'

3.4.23. S.ljust(): Left justification


This method returns the contents of S with extra space characters (or some other characters) added after it if necessary to fill it to a given length. Here is the general form:
S.ljust(w[, fill]) w

The desired result length as an integer.


fill

The character to be used to fill the string to the desired length, if needed. The default fill character is a space. This method will never return a string shorter than S.
>>> "Ni".ljust(4) 'Ni ' >>> "Ni".ljust(15, '!') 'Ni!!!!!!!!!!!!!' >>> "Basingstoke".ljust(2) 'Basingstoke'

3.4.24. S.lower(): Convert to lowercase


The result is S with all uppercase characters replaced by their lowercase equivalent.
>>> "I like SHOUTING!".lower() 'i like shouting!'

3.4.25. S.lstrip(): Remove selected leading characters


Returns S, but with any initial characters removed that are in a given set. Here is the general form:
S.lstrip([what])

The optional argument what is a string specifying the characters that you want to remove. The default value of this argument is a string containing all the characters that are considered whitespace; see Section 3.3, Definition of whitespace (p. 20) for a list of those characters. Compare Section 3.4.34, S.rstrip(): Remove selected trailing characters (p. 32) and Section 3.4.25, S.lstrip(): Remove selected leading characters (p. 28).
>>> ' \t \n Run \t \n away ! \n \t 'Run \t \n away ! \n \t ' >>> "***Done***".lstrip('*') '.lstrip()

28

Sequence types (ordered sets)

New Mexico Tech Computer Center

'Done***' >>> "((*(*))**)(*Dinsdale*)".lstrip ( ")(*" ) 'Dinsdale*)'

3.4.26. str.maketrans(): Make a translation table


This is a static method of type str that builds a translation table to be used by the method described in Section 3.4.41, S.translate(): Character substitutions and deletions (p. 34). Here is the general form:
str.maketrans(x[, y[, z]])

If you provide just one value x, it must be a dictionary or other mapping that defines which input characters are to be translated to which output characters. The keys of this dictionary may be either integers (representing Unicode code points) or one-character strings. The corresponding values may be integer code points, one-character strings, or the special value None. The resulting translation table value, when passed as the argument to the S.translate() method, will cause each occurrence of a character that matches one of the keys of x to be replaced by the value corresponding to that key, or to be deleted if the corresponding value is None. If you provide two or three arguments, the x and y arguments must be strings, both having the same length. The resulting translation table will, when passed as the argument to the S.translate() method, cause every character that occurs in x to be replaced by the character from the corresponding position of y. If you would like certain characters to be removed altogether during translation, provide a third argument z containing the characters to be removed. Examples:
>>> m = {'a': 'x', 'e': 'x', 'i': 'x'} >>> revowel = str.maketrans(m) >>> 'Piranha'.translate(revowel) 'Pxrxnhx' >>> killa = str.maketrans({'a': None}) >>> 'Piranha'.translate(killa) 'Pirnh' >>> bingo = str.maketrans('lLrR', 'rRlL') >>> 'Cornwall Llanfair'.translate(bingo) 'Colnwarr Rranfail' >>> devowel = str.maketrans('h', '$', 'aeiouAEIOU') >>> a26 = "The quick brown fox jumps over the lazy dog." >>> a26.translate(devowel) a26.translate(devowel) 'T$ qck brwn fx jmps vr t$ lzy dg.'

3.4.27. S.partition(): Break a string into pieces at a delimiter


The purpose of this method is to search S for the first occurrence of some delimiter string d, and break S into pieces at that point.
S.partition(d)

New Mexico Tech Computer Center

Sequence types (ordered sets)

29

If S contains at least one occurrence of the string d, the result is a tuple (pre, d, post): pre is the part of S before the delimiter, d is the delimiter, and post is the rest of S after the delimiter. If d does not occur in S, the result is a tuple (S, '', '').
>>> "Daffy English kniggets!".partition(' ') ('Daffy', ' ', 'English kniggets!') >>> "Daffy English kniggets!".partition('/') ('Daffy English kniggets!', '', '') >>> "a*b***c*d".partition("**") ('a*b', '**', '*c*d')

3.4.28. S.replace(): Replace some or all occurrences of a string


This method searches string S for occurrences of some string old, and returns a copy of S with some or all of the non-overlapping occurrences of old replaced by a different string new:
S.replace(old, new[,max])

If you would like to limit the number of replacements, specify that number as the optional max argument. The default is to replace all occurrences. Examples:
>>> 'Frenetic'.replace('e', 'x') 'Frxnxtic' >>> 'Frenetic'.replace('e', '###') 'Fr###n###tic' >>> 'banana'.replace('an', 'erzerk') 'berzerkerzerka' >>> 'banana'.replace('a', 'x', 2) 'bxnxna' >>> 'banana'.replace('ana', '^') 'b^na'

3.4.29. S.rfind(): Find the rightmost occurrence of a substring


This method is similar to Section 3.4.7, S.find(): Search for a substring (p. 24), except that it finds the last (rightmost) occurrence of substring t:
S.rfind(t[,start[,end]])

For example:
>>> 'banana'.find('a') 1 >>> 'banana'.rfind('a') 5 >>> 'banana'.rfind('slug') -1

3.4.30. S.rindex(): Rightmost index


This method is similar to Section 3.4.10, S.index(): Find a substring (p. 25) except that it returns the highest (rightmost) position where string t is found in S. It is also similar to Section 3.4.29, S.rfind(): Find the rightmost occurrence of a substring (p. 30) except that when t is not present in S it will raise a ValueError exception.

30

Sequence types (ordered sets)

New Mexico Tech Computer Center

>>> 'banana'.index('n') 'banana'.index('na') 2 >>> 'banana'.rindex('na') 4 >>> 'banana'.rindex('Notlob') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found

3.4.31. S.rjust(): Right justification


This method returns a copy of S, padded with a specified single fill character to guarantee that the result has at least length w.
S.rjust(w[,fill])

If len(S)<w, copies of fill are appended to the end to produce a string of length w. The default fill character is one space. If len(S)>=w, the method returns S.
'123'.rjust(5) ' 123' >>> '123'.rjust(5,'*') '**123' >>> '123'.rjust(30, 'rar') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: The fill character must be exactly one character long

3.4.32. S.rpartition(): Right partition


This method is similar to Section 3.4.27, S.partition(): Break a string into pieces at a delimiter (p. 29), except that it finds the last occurrence of delimiter d within S:
S.rpartition(d)

If S contains at least one occurrence of the string d, the result is a tuple (pre, d, post): pre is the part of S before the last occurrence of the delimiter, d is the delimiter, and post is the rest of S after the delimiter. If d does not occur in S, the result is a tuple (S, '', '').
>>> "Daffy English kniggets!".rpartition(' ') ('Daffy English', ' ', 'kniggets!') >>> "a*b***c*d".rpartition("**") ('a*b*', '**', 'c*d')

3.4.33. S.rsplit(): Split from the right


This method is quite similar to Section 3.4.35, S.split(): Break a string into pieces at delimiters (p. 32). General form:
S.rsplit([d[,max]])

New Mexico Tech Computer Center

Sequence types (ordered sets)

31

The only difference comes when you specify the max argument, and there are more than that many fields in S. In that case, fields are removed from the end of S rather than from the start.
>>> "I am Zoot's identical twin sister, Dingo.".rsplit(None, 2) ["I am Zoot's identical twin", 'sister,', 'Dingo.']

3.4.34. S.rstrip(): Remove selected trailing characters


This method returns a copy of string S, but with all characters in a specified set of characters removed from the end. Compare Section 3.4.25, S.lstrip(): Remove selected leading characters (p. 28) and Section 3.4.38, S.strip(): Remove leading and trailing characters (p. 33). Here is the general form:
S.rstrip([what])

The default value of what is a string containing all the whitespace characters; see Section 3.3, Definition of whitespace (p. 20). Examples:
>>> ' ' \t \t \n Run \t \n away ! \n \t \n Run \t \n away !' '.rstrip()

3.4.35. S.split(): Break a string into pieces at delimiters


Since the early days of computing, the term record has been used to describe a collection of values, called fields, that are separated by some string called a delimiter. For example, back in the days of FORTRAN and other ancient programming languages, it was common to specify that a data record was a sequence of numbers set off by spaces, like this:
134.15 8.06e-14 13

The purpose of Python's .split() method, then, is to break a record into fields. Here is the general form:
S.split([d[,max]])

Given a record as a string S, this method uses delimiter string d to split S into its component fields, and returns a list of strings containing the fields. The default delimiter is any clump of one or more whitespace characters. You may pass None as the d argument to specify this behavior. Use the optional max argument to specify the maximum number of fields to be removed from the front of S; the default maximum is unlimited, so you will get a list of the fields of S no matter how many there are.
>>> record = " 134.15 8.06e-14 13" >>> record.split() ['134.15', '8.06e-14', '13'] >>> "Block<^>of <^>flats<^>".split('<^>') ['Block', 'of ', 'flats', ''] >>> 'a/b/c/d/e'.split('/') ['a', 'b', 'c', 'd', 'e'] >>> 'a/b/c/d/e'.split('/', 1) ['a', 'b/c/d/e'] >>> 'a/b/c/d/e'.split('/', 2) ['a', 'b', 'c/d/e'] >>> '//c//'.split('/') ['', '', 'c', '', '']

32

Sequence types (ordered sets)

New Mexico Tech Computer Center

>>> 'a, b, "c, d", e'.split(',') ['a', ' b', ' "c', ' d"', ' e'] >>> "I am Zoot's identical twin sister, Dingo.".split(None, 2) ['I', 'am', "Zoot's identical twin sister, Dingo."]

3.4.36. S.splitlines(): Split at line breaks


This method breaks a string S into its component lines. Here is the general form:
S.splitlines([keepends])

The method returns the lines in S as a list of strings. A line is defined as any sequence of characters ending with a line terminator sequence, which may be just a newline (ASCII NL, '\n'), just an ASCII CR ('\r', or both ('\r\n'). By default, the line terminator sequences are removed from the result. If you wish to retain them, pass True as the optional keepends argument.
>>> """Is that ... an ocarina?""".splitlines() ['Is that', 'an ocarina?'] >>> """What is your name? ... Sir Robin of Camelot.""".splitlines(True) ['What is your name?\n', 'Sir Robin of Camelot.'] >>> ''.splitlines() [] >>> '\r\n'.splitlines() ['']

3.4.37. S.startswith(): Check for a prefix


This method is a predicate to test whether S starts with string t. Here is the general form:
S.startswith(t[,start[,end]])

The method returns True if t matches the initial characters of S, False otherwise. The optional start and end may be used to limit the part of S that is tested: start limits the starting position, and end specifies the ending position, of the string to be tested to see if it starts with t.
>>> "bishop".startswith('bish') True >>> "bishop".startswith('The') False >>> chiquita = 'Banana' >>> chiquita[2:5] 'nan' >>> chiquita.startswith('na', 2, 5) True

3.4.38. S.strip(): Remove leading and trailing characters


The purpose of this method is to remove a given set of characters from the beginning and end of a string S and return what is left. Here is the general form:
S.strip([c])

New Mexico Tech Computer Center

Sequence types (ordered sets)

33

The optional argument c is a string containing the characters to be removed. The default value of c is a string containing all the whitespace characters; see Section 3.3, Definition of whitespace (p. 20).
>>> ' \t \n Run \t \n away ! \n \t '.strip() 'Run \t \n away !' >>> blue = "!#$%*?@^" >>> "@@^$^!###%Rowr$bazzle!!?#^@".strip(blue) 'Rowr$bazzle'

3.4.39. S.swapcase(): Toggle the case of letters


This method returns a copy of a string S, except that each uppercase letter is replaced by its lowercase equivalent, and vice versa.
>>> "abcDEF".swapcase() 'ABCdef'

3.4.40. S.title(): Change a string to title-case


The result of this method is a copy of S, except that in each group of letters, the first letter is uppercase and the rest are lowercase.
>>> "oh no you didn't!".title() "Oh No You Didn'T!"

3.4.41. S.translate(): Character substitutions and deletions


This method can be used to substitute certain characters for certain other characters in a string. It can also be used to delete all occurrences of certain characters from a string. Here is the general form:
S.translate(T)

The method returns a copy of S after performing all the substitutions and deletions specified by the mapping T. The easiest way to set up the mapping T is to use the method described in Section 3.4.26, str.maketrans(): Make a translation table (p. 29). Refer to that section for a description of how T works, with examples.

3.4.42. S.upper(): Convert lowercase characters to uppercase


This method returns a copy of S with each lowercase character replaced by its uppercase equivalent.
>>> 'I like shouting'.upper() 'I LIKE SHOUTING' >>> Gamma = '\N{GREEK CAPITAL LETTER GAMMA}' >>> gamma = '\N{GREEK SMALL LETTER GAMMA}' >>> Gamma == gamma.upper() True

3.4.43. S.zfill(): Add zero fill


The purpose of this method is to add zeroes to the left of the digits of a number in order to make the result a specified size w. Here is the general form:

34

Sequence types (ordered sets)

New Mexico Tech Computer Center

S.zfill(w)

If len(S)<w, the result is a copy of S with enough zeroes added to the left of S's digits so that the result has length w. If S starts with a sign character (+ or -), the left zeroes are inserted after the sign character. If len(S)>=w, the method returns S.
>>> '12'.zfill(9) '000000012' >>> '+12'.zfill(8) '+0000012' >>> '-42'.zfill(8) '-0000042'

3.5. The string .format() method


The .format() method of the str type is a convenient way to format text the way you want it. Quite often, we want to embed data values in some explanatory text. For example, if we are displaying the number of nematodes in a hectare, it is a lot more meaningful to display it as "There were 37.9 nematodes per hectare" than just "37.9". So what we need is a way to mix constant text like "nematodes per hectare" with values from elsewhere in your program. Here is the general form:
template.format(p0, p1, ..., k0=v0, k1=v1, ...)

The template is a string with one or more replacement fields embedded in constant text. Each replacement field is enclosed in single braces {...}, and specifies that a value is to be substituted at that position in the format string. The values to be substituted are passed as arguments to the .format() method. The arguments to the .format() method are of two types. The list starts with zero or more positional arguments pi, followed by zero or more keyword arguments of the form ki=vi, where each ki is a name with an associated value vi. Just to give you the general flavor of how this works, here's a simple conversational example. In this example, the replacement field {0} is replaced by the first positional argument (49), and {1} is replaced by the second positional argument, the string "okra".
>>> "We have {0} hectares planted to {1}!".format(49, "okra") 'We have 49 hectares planted to okra!' >>>

In the next example, we supply the values using keyword arguments. The arguments may be supplied in any order. The keyword names must be valid Python names (see Section 2, Names and keywords (p. 3)).
>>> "{monster} has now eaten {city}".format( ... city='Tokyo', monster='Mothra') 'Mothra has now eaten Tokyo'

You may mix references to positional and keyword arguments:


>>> "The {structure} sank {0} times in {1} years.".format( ... 3, 2, structure='castle') 'The castle sank 3 times in 2 years.'

If you need to include actual { and } characters in the result, double them, like this:

New Mexico Tech Computer Center

Sequence types (ordered sets)

35

>>> "There are {0} members in set {{a}}.".format(15) 'There are 15 members in set {a}.'

3.5.1. General form of a replacement field


Here is the general form of a replacement field.

A replacement_field starts with an optional field name or number, optionally followed by a ! and conversion specification, optionally followed by : and a format_spec that specifies the format. Section 3.5.2, The field_name part (p. 36). Section 3.5.3, The conversion part (p. 37). Section 3.5.4, The spec part (p. 38). Section 3.5.5, Formatting a field of variable length (p. 41): This is the trick necessary to format a field whose width is computed during program execution.

3.5.2. The field_name part


Here is the syntax for the field_name part of a replacement field, which specifies the source of the value to be formatted.

A field_name must start with either a number or a name. Numbers (integer in the diagram) refer to positional arguments passed to the .format() method, starting at 0 for the first argument. Names (identifier in the diagram) refer to keyword arguments to .format(). Following this you can append any number of expressions that retrieve parts of the referenced value. If the associated argument has attributes, you can refer to them by using a period (.) followed by the attribute name. For example:
>>> import string >>> string.digits '0123456789' >>> "Our digits are '{s.digits}'.".format(s=string)

36

Sequence types (ordered sets)

New Mexico Tech Computer Center

"Our digits are '0123456789'."

If the associated argument is an iterable, you may extract an element from it by using an integer in square brackets [...]. For example:
>>> signal=['red', 'yellow', 'green'] >>> signal[2] 'green' >>> "The light is {0[2]}!".format(signal) 'The light is green!'

To extract an element from a mapping, use an expression of the form [k] where k is a key value, which may contain any character except ].
>>> paintMap = {'M': 'blue', 'F': 'pink'} >>> "Paint it {map[M]}.".format(map=paintMap) 'Paint it blue.'

In general, you can use any combination of these features. For example:
>>> "The sixth digit is '{s.digits[5]}'".format(s=string) "The sixth digit is '5'"

You may omit all of the numbers that refer to positional arguments, and they will be used in the sequence they occur. For example:
>>> "The date is {}-{}-{}.".format(2012, 5, 1) 'The date is 2012-5-1.'

If you use this convention, you must omit all those numbers. You can, however, omit all the numbers and still use the keyword names feature:
>>> "Can I have {} pounds to {excuse}?".format( ... 50, excuse='mend the shed') 'Can I have 50 pounds to mend the shed?'

3.5.3. The conversion part


Following the name part of a replacement field, you can use one of these forms to force the value to be converted by a standard function:
!a !r !s

Section 1.2, ascii(): Convert to 8-bit ASCII (p. 51). Section 1.13, repr(): Printable representation (p. 53). Section 1.14, str(): Convert to str type (p. 53). This is the default for values of types other than str.

Here's an example:
>>> "{}".format('Don\'t') "Don't"

New Mexico Tech Computer Center

Sequence types (ordered sets)

37

>>> "{!r}".format('Don\'t') '"Don\'t"'

3.5.4. The spec part


After the name and conversion parts of a replacement field, you may use a colon (:) and a format specifier to supply more details about how to format the related value. Here is the general form.

fill

You may specify any fill character except }. This character is used to pad a short value to the specified length. It may be specified only in combination with an align character.
align

Specifies how to align values that are not long enough to occupy the specified length. There are four values:
< Left-justify the value. This is the default alignment for string values. > Right-justify the value. This is the default alignment for numbers. ^ Center the value. = For numbers using a sign specifier, add the padding between the sign and the rest of the value.

Here are some examples of the use of fill and align.


>>> "{:>8}".format(13) ' 13' >>> "{:>8}".format('abc') ' abc' >>> "{:*>8}".format('abc') '*****abc' >>> "{:*<8}".format('abc') 'abc*****' >>> "{:>5d}".format(14) ' 14' >>> "{:#>5d}".format(14) '###14' >>> "{:<6}".format('Git') 'Git ' >>> "{:*<6}".format('Git') 'Git***' >>> "{:=^8}".format('Git') '==Git===' >>> "{:*=-9d}".format(-3) '-*******3'
sign

This option controls whether an arithmetic sign is displayed. There are three possible values:

38

Sequence types (ordered sets)

New Mexico Tech Computer Center

+ -

Always display a sign: + for positive, - for negative. Display - only for negative values. Display one space for positive values, - for negative.

(one space)

Here are some examples of use of the sign options.


>>> '{} {}'.format(17, -17) '17 -17' >>> '{:5} {:5}'.format(17, -17) ' 17 -17' >>> '{:<5} {:<5}'.format(17, -17) '17 -17 ' >>> '{:@<5} {:@<5}'.format(17, -17) '17@@@ -17@@' >>> '{:@>5} {:@>5}'.format(17, -17) '@@@17 @@-17' >>> '{:@^5} {:@^5}'.format(17, -17) '@17@@ @-17@' >>> '{:@^+5} {:@^+5}'.format(17, -17) '@+17@ @-17@' >>> '{:@^-5} {:@^-5}'.format(17, -17) '@17@@ @-17@' >>> '{:@^ 5} {:@^ 5}'.format(17, -17) '@ 17@ @-17@' #

This option selects the alternate form of output for some types. When formatting integers as binary, octal, or hexadecimal, the alternate form adds 0b, 0o, or 0x before the value, to show the radix explicitly.
>>> "{:4x}".format(255) ' ff' >>> "{:#4x}".format(255) '0xff' >>> "{:9b}".format(62) ' 111110' >>> "{:#9b}".format(62) ' 0b111110' >>> "{:<#9b}".format(62) '0b111110 '

When formatting float, complex, or Decimal values, the # option forces the result to contain a decimal point, even if it is a whole number.
>>> "{:5.0f}".format(36) ' 36' >>> "{:#5.0f}".format(36) ' 36.' >>> from decimal import Decimal >>> w=Decimal(36) >>> "{:g}".format(w) '36' >>> "{:#g}".format(w) '36.' 0

To fill the field with left zeroes, place a 0 at this position in your replacement field.

New Mexico Tech Computer Center

Sequence types (ordered sets)

39

>>> "{:5d}".format(36) ' 36' >>> "{:05d}".format(36) '00036' >>> "{:021.15f}".format(1.0/7.0) '00000.142857142857143'
width

Place a number at this position to specify the total width of the displayed value.
>>> "Beware 'Beware the >>> "Beware 'Beware the >>> "Beware 'Beware the , the {}!".format('Penguin') Penguin!' the {:11}!".format('Penguin') Penguin !' the {:>11}!".format('Penguin') Penguin!'

Place a comma at this position in your replacement field to display commas between groups of three digits in whole numbers.
>>> "{:,d}".format(12345678901234) '12,345,678,901,234' >>> "{:,f}".format(1234567890123.456789) '1,234,567,890,123.456787' >>> "{:25,f}".format(98765432.10987) ' 98,765,432.109870' >>> "{:25,.2f}".format(98765432.10987) "{:25,.2f}".format(98765432.10987) ' 98,765,432.11' .precision

Use this part to specify the number of digits after the decimal point.
>>> from math import pi >>> "{}".format(pi) '3.141592653589793' >>> "{:.3}".format(pi) '3.14' >>> "{:25,.3f}".format(1234567890123.456789) ' 1,234,567,890,123.457'
type

This code specifies the general type of format used. The default is to convert the value of a string as if using the str() function. Refer to the table below for allowed values.
b c d e E f g G

Format an integer in binary. Given a number, display the character that has that code. Display a number in decimal (base 10). Display a float value using the exponential format. Same as e, but use a capital E in the exponent. Format a number in fixed-point form. General numeric format: use either f or g, whichever is appropriate. Same as g, but uses a capital E in the exponential form.

40

Sequence types (ordered sets)

New Mexico Tech Computer Center

For formatting numbers, this format uses the current local setting to insert separator characters. For example, a number that Americans would show as 1,234.56, Europeans would show it as 1.234,56. Display an integer in octal format. Display an integer in hexadecimal (base 16). Digits greater than 9 are displayed as lowercase characters. Display an integer in hexadecimal (base 16). Digits greater than 9 are displayed as uppercase characters. Display a number as a percentage: its value is multiplied by 100, followed by a % character.

o x X %

Examples:
>>> "{:b}".format(9) '1001' >>> "{:08b}".format(9) '00001001' >>> "{:c}".format(97) 'a' >>> "{:d}".format(0xff) '255' >>> from math import pi >>> "{:e}".format(pi*1e10) '3.141593e+10' >>> "{:E}".format(pi*1e10) '3.141593E+10' >>> "{:f}".format(pi) '3.141593' >>> "{:g}".format(pi) '3.14159' >>> "{:g}".format(pi*1e37) '3.14159e+37' >>> "{:G}".format(pi*1e37) '3.14159E+37' >>> "{:o}".format(255) '377' >>> "{:#o}".format(255) '0o377' >>> "{:x}".format(105199) '19aef' >>> "{:X}".format(105199) '19AEF' >>> "{:<#9X}".format(105199) '0X19AEF ' >>> "{:%}".format(0.6789) '67.890000%' >>> "{:15.3%}".format(0.6789) ' 67.890%'

3.5.5. Formatting a field of variable length


Sometimes you need to format a field using a length that is available only once the program is running. To do this, you can use a number or name in {braces} inside a replacement field at the width position. This item then refers to either a positional or keyword argument to the .format() method as usual. Here's an example. Suppose you want to format a number n using d digits. Here are examples showing this with and without left-zero fill:

New Mexico Tech Computer Center

Sequence types (ordered sets)

41

>>> n = 42 >>> d = 8 >>> "{0:{1}d}".format(42, 8) ' 42' >>> "{0:0{1}d}".format(42, 8) '00000042' >>>

You can, of course, also use keyword arguments to specify the field width. This trick also works for variable precision.
"{count:0{width}d}".format(width=8, count=42) '00000042' >>>

The same technique applies to substituting any of the pieces of a replacement field.
>>> "{:&<14,d}".format(123456) '123,456&&&&&&&' >>> "{1:{0}{2}{3},{4}}".format('&', 123456, '<', 14, 'd') '123,456&&&&&&&' >>> "{:@^14,d}".format(1234567) '@@1,234,567@@@' >>> "{n:{fil}{al}{w},{kind}}".format( ... kind='d', w=14, al='^', fil='@', n=1234567) '@@1,234,567@@@'

4. Type bytes: Immutable sequences of 8-bit integers 5. Type bytearray: Mutable sequences of 8-bit integers 6. Type list: Mutable sequences of arbitrary objects 7. Type tuple: Immutable sequences of arbitrary objects 8. Type range: A range of values

42

Sequence types (ordered sets)

New Mexico Tech Computer Center

Chapter 6. The set types: set and frozenset

New Mexico Tech Computer Center

The set types: set and frozenset

43

44

The set types: set and frozenset

New Mexico Tech Computer Center

Chapter 7. Type dict: Dictionaries (mappings)

New Mexico Tech Computer Center

Type dict: Dictionaries (mappings)

45

46

Type dict: Dictionaries (mappings)

New Mexico Tech Computer Center

Chapter 8. Operators and expressions


Python's operators are shown here from highest precedence to lowest, with a ruled line separating groups of operators with equal precedence:

Table 8.1. Python operator precedence


(E) [E, ...] {key:value, ...} `...`
x.attribute x[...] f(...) x**y

Parenthesized expression or tuple. List. Dictionary or set. Convert to string representation. Attribute reference. Subscript or slice; see Section 2, Common operations on sequence types (p. 14). Call function f.
x to the y power.

-x ~x
x*y x/y, x//y x%y x+y x-y x<<y x>>y x&y x^y x|y x<y, x<=y, x>y, x>=y, x!=y, x==y x in y, x not in y x is y, x is not y

Negation. Bitwise not (one's complement). Multiplication. Division. The // form discards the fraction from the result. For example, 13.9//5.0 returns the value 2.0. Modulo (remainder of x/y). Addition, concatenation. Subtraction.
x shifted left ybits. x shifted right ybits.

Bitwise and. Bitwise exclusive or. Bitwise or. Comparisons. These operators are all predicates; see Section 1, What is a predicate? (p. 48). Test for membership. Test for identity. Boolean not. Boolean and. Boolean or.

not x
x and y x or y

New Mexico Tech Computer Center

Operators and expressions

47

1. What is a predicate?
We use the term predicate to mean any Python function that tests some condition and returns a Boolean value. For example, x < y is a predicate that tests whether x is less than y: 5 < 500 returns True, while 5 >= 500 returns False.

2. What is an iterable?
To iterate over a sequence means to visit each element of the sequence, and do some operation for each element. In Python, we say that a value is an iterable when your program can iterate over it. In short, an iterable is a value that represents a sequence of one more values. All instances of Python's sequence types are iterables. These types may be referred to as container types: a unicode string string is a container for 32-bit characters, and lists and tuples are general-purpose containers that can contain any sequence. One of the most common uses for an iterable is in a for statement, where you want to perform some operation on a sequence of values. For example, if you have a tuple named celsiuses containing Celsius temperatures, and you want to print them with their Fahrenheit equivalents, and you have written a function cToF() that converts Celsius to Fahrenheit, this code does it:
>>> def cToF(c): return c*9.0/5.0 + 32.0 ... >>> celsiuses = (0, 20, 23.6, 100) >>> for celsius in celsiuses: ... print("{0:.1f} C = {1:.1f} F".format(celsius, cToF(celsius))) ... 0.0 C = 32.0 F 20.0 C = 68.0 F 23.6 C = 74.5 F 100.0 C = 212.0 F

However, Python also supports mechanisms for lazy evaluation: a piece of program that acts like a sequence, but produces its contained values one at a time. Keep in mind that the above code works exactly the same if celsiuses is an iterator (see Section 4, Iterators: Values that can produce a sequence of values (p. 66)). You may find many uses for iterators in your programs. For example, celsiuses might be a system that goes off and reads an actual thermometer and returns the readings every ten seconds. In this application, the code above doesn't care where celsiuses gets the values, it cares only about how to convert and print them.

3. Duck typing, or: what is an interface?


When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck. James Whitcomb Riley The term duck typing comes from this quote. In programming terms, this means that the important thing 1 about a value is what it can do, not its type. As the excellent Wikipedia article on duck typing says, Simply stated: provided you can perform the job, we don't care who your parents are.
1

http://en.wikipedia.org/wiki/Duck_typing

48

Operators and expressions

New Mexico Tech Computer Center

One common example of duck typing is in the Python term file-like object. If you open a file for reading using the open() function, the actual type you get back depends on how you open it. But the important thing is not the type but what you can do with it. \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
>>>

Let's suppose that you write a function called numberIt() that takes a readable file as an argument and prints the lines from a file preceded by five-digit line numbers. Here's the function and an example of its use:
>>> def numberIt(f): ... for lineNo, line in enumerate(f): ... print("{0:05d} {1}".format(lineNo, line.rstrip())) ... >>> inFile = open('input') >>> numberIt(inFile) 00000 Kant 00001 Heidegger 00002 Hume

The way you have written the numberIt() function, it works for files, but it also works for any iterable. Thus, when you see the statement that some Python feature works with a file-like object, that means that the object must have an interface like that of the file type; Python doesn't care about the type, just the operations that it supports. In practice, the enumerate() function works with any iterable, so your function will also work with any iterable:
>>> numberIt(['Kant', 'Heidegger', 'Hume']) 00000 Kant 00001 Heidegger 00002 Hume

So in Python when we say that we expect some value to have an interface, we mean that it must provide certain methods or functions, but the actual type of the value is not important. More formally, when we say that a value supports the iterable interface, that value must provide either of the following features: A .__getitem__() method as described in Section 2.2, __getitem__(): Get one item from a sequence or mapping (p. 73). A .__iter__() method as described in Section 2.3, .__iter__(): Create an iterator (p. 73).

4. What is the locale?


In order to accommodate different character encodings, your system may have a locale setting that specifies a preferred character set. In the USA, most systems use the ASCII encoding. Good application code should be written in a way that does not depend on this encoding to deal with cultural issues. For general information on handling locale issues, see the documentation for the locale module .
3 2

2 3

http://en.wikipedia.org/wiki/ASCII http://docs.python.org/py3k/library/locale.html

New Mexico Tech Computer Center

Operators and expressions

49

5. Comprehensions

50

Operators and expressions

New Mexico Tech Computer Center

Chapter 9. Built-in functions


Python has dozens of functions. We have divided them into two general classes: Section 1, Basic built-in functions (p. 51): Functions that most Python programmers need to know about. Section 2, Advanced functions (p. 53): Functions that you might need once you become proficient in the language basics.

1. Basic built-in functions


This section describes some common built-in Python functions, ones that you will likely use most of the time. A number of more exotic functions are discussed in Section 2, Advanced functions (p. 53).

1.1. abs(): Absolute value 1.2. ascii(): Convert to 8-bit ASCII 1.3. bool(): Convert to bool type 1.4. complex(): Convert to complex type 1.5. input(): Read a string from standard input
The purpose of this function is to read input from the standard input stream (the keyboard, by default). Without an argument, it silently awaits input. If you would like to prompt the user for the input, pass the desired prompt string as the argument. The result is returned as a str (string) value, without a trailing newline character. In the example shown just below, the second line is typed by the user.
>>> s = input() This tobacconist is scratched. >>> s 'This tobacconist is scratched.' >>> yesNo = input("Is this the right room for an argument? ") Is this the right room for an argument? I've told you once. >>> yesNo "I've told you once."

1.6. int(): Convert to int type


To convert a number of a different type to int type, or to convert a string of characters that represents a number:

New Mexico Tech Computer Center

Built-in functions

51

int(ns)

where ns is the value to be converted. If ns is a float, the value will be truncated, discarding the fraction. If you want to convert a character string s, expressed in a radix (base) other than 10, to an int, use this form, where b is an integer in the range [2, 36] that specifies the radix. If the string to be converted obeys the Python prefix conventions (octal values start with 0o, hex values with 0x, and binary values with 0b), use zero as the second argument.
int(s, b)

Examples:
>>> int(True) 1 >>> int(False) 0 >>> int(43.89) 43 >>> int("43") 43 >>> int('77', 8) 63 >>> int('7ff', 16) 2047 >>> int('10101', 2) 21 >>> int('037') 37 >>> int('037', 8) 31 >>> int('0o37', 0) 31 >>> int('0x1f', 0) 31

1.7. iter(): Return an iterator for a given sequence 1.8. len(): How many elements in a sequence? 1.9. max(): What is the largest element of a sequence? 1.10. min(): What is the smallest element of a sequence? 1.11. open(): Open a file 1.12. ord(): What is the code point of this character?
The function ord(s) operates on a string s that contains exactly one character. It returns the Unicode code point of that character as type int.

52

Built-in functions

New Mexico Tech Computer Center

>>> ord('@') 64 >>> ord('\xa0') 160

1.13. repr(): Printable representation 1.14. str(): Convert to str type

2. Advanced functions

New Mexico Tech Computer Center

Built-in functions

53

54

Built-in functions

New Mexico Tech Computer Center

Chapter 10. Python statements


Section 1, Simple statements (p. 55) Section 2, Compound statements (p. 56). Here, for your convenience, is a table of all the Python statement types, and the sections where they are described. The first one, the assignment statement, does not have an initial keyword: an assignment statement is a statement of the form variable = expression. Expression Assignment
assert break continue del elif else except finally for from global if import nonlocal pass raise return try with yield

Section 1.1, The expression statement (p. 56) Section 1.2, The assignment statement: name = expression (p. 56). Section 1.3, The assert statement (p. 56). Section 2.2, The break statement: Exit a for or while loop (p. 58). Section 2.3, The continue statement: Jump to the next cycle of a for or while (p. 58). Section 1.4, The del statement (p. 56). Section 2.5, The if statement (p. 60) and Section 2.6, The tryexcept construct (p. 60). Section 2.5, The if statement (p. 60) and Section 2.6, The tryexcept construct (p. 60). Section 2.6, The tryexcept construct (p. 60). Section 2.6, The tryexcept construct (p. 60). Section 2.4, The for statement: Iteration over a sequence (p. 58). Section 1.5, The import statement (p. 56). Section 1.6, The global statement (p. 56). Section 2.5, The if statement (p. 60). Section 1.5, The import statement (p. 56). Section 1.7, The nonlocal statement (p. 56). Section 1.8, The pass statement (p. 56). Section 1.9, The raise statement (p. 56). Section 1.10, The return statement (p. 56). Section 2.6, The tryexcept construct (p. 60). Section 2.7, The with statement (p. 60). Section 2.8, The yield statement: Generate one result of a generator (p. 60).

1. Simple statements
Python statement types are divided into two groups. Simple statements, that are executed sequentially and do not affect the flow of control, are described first. Compound statements, which may affect the sequence of execution, are discussed in Section 2, Compound statements (p. 56).

New Mexico Tech Computer Center

Python statements

55

1.1. The expression statement


A statement may consist of just a single Python expression by itself. For example, to call a function named xParrot with no arguments, this is a valid statement:
xParrot()

1.2. The assignment statement: name = expression 1.3. The assert statement 1.4. The del statement 1.5. The import statement 1.6. The global statement 1.7. The nonlocal statement 1.8. The pass statement 1.9. The raise statement 1.10. The return statement

2. Compound statements
The statements in this section alter the normal sequential execution of a program. They can cause a statement to be executed only under certain circumstances, or execute it repeatedly.

2.1. Python's block structure


One unusual feature of Python is the way that the indentation of your source program organizes it into blocks within blocks within blocks. This is contrary to the way languages like C and Perl organize code blocks by enclosing them in delimiters such as braces { ... }. At the very top level of your program, all statements must be unindentedthey must start in column one. Various Python branching statements like if and for control the execution of one or more subsidiary blocks of lines. A block is defined as a group of adjacent lines that are indented the same amount, but indented further than the controlling line. The amount of indentation of a block is not critical.

56

Python statements

New Mexico Tech Computer Center

You can use either spaces or tab characters for indentation. However, mixing the two is perverse and can make your program hard to maintain. Tab stops are assumed to be every eight columns. Blocks within blocks are simply indented further. Here is an example of some nested blocks:
if i < 0: print("i is negative") else: print("i is nonnegative") if i < 10: print("i has one digit") else: print("i has multiple digits")

If you prefer a more horizontal style, you can always place statements after the colon (:) of a compound statement, and you can place multiple statements on a line by separating them with semicolons (;). Example:
>>> if 2 > 1: print("Math still works"; print "Yay!") ... else: print("Huh?") ... Math still works Yay!

You can't mix the block style with the horizontal style: the consequence of an if or else must either be on the same line or in a block, never both.
>>> if 1: print("True") ... print("Unexpected indent error here.") File "<stdin>", line 2 print("Unexpected indent error here.") ^ IndentationError: unexpected indent >>>

Here is a formal definition of Python's block structure. A suite is the sequence of statements that are executed following the : after an if, else, or other compound statement.

In this diagram, INDENT refers to an increase in the amount of indentation, and DEDENT is where the indentation decreases to its former level. Note that a trailing ; is allowed and ignored on any line.

New Mexico Tech Computer Center

Python statements

57

2.2. The break statement: Exit a for or while loop


The purpose of this statement is to jump out of a for or while loop before the loop would terminate otherwise. Control is transferred to the statement after the last line of the loop. The statement looks like this:
break

Here's an example.
>>> for ... ... ... ... 1 71 13 i in [1, 71, 13, 2, 81, 15]: print(i, end=' ') if (i%2) == 0: break 2

Normally this loop would be executed six times, once for each value in the list, but the break statement gets executed when i is set to an even value.

2.3. The continue statement: Jump to the next cycle of a for or while
Use a continue statement inside a for or while loop when you want to jump directly back to the top of the loop and go around again. If used inside a while loop, the loop's condition expression is evaluated again. If the condition is False, the loop is terminated; if the condition is True, the loop is executed again. Inside a for loop, a continue statement goes back to the top of the loop. If there are any values remaining in the iterable that controls the loop, the loop variable is set to the next value in the iterable, and the loop body is entered. If the continue is executed during the last pass through the loop, control goes to the statement after the end of the loop. Examples:
>>> i = 0 >>> while i < 10: ... print(i, end=' ') ... i += 1 ... if (i%3) != 0: ... continue ... print("num", end=' ') ... 0 1 2 num 3 4 5 num 6 7 8 num 9 >>> for i in range(10): ... print(i, end=' ') ... if (i%4) != 0: ... continue ... print("whee", end=' ') ... 0 whee 1 2 3 4 whee 5 6 7 8 whee 9

2.4. The for statement: Iteration over a sequence


Use a for statement to execute a block of statements repeatedly. Here is the general form. (For the definition of a suite, see Section 2.1, Python's block structure (p. 56).)

58

Python statements

New Mexico Tech Computer Center

for V in S:
B

V is a variable called the induction variable. S is any iterable; see Section 2, What is an iterable? (p. 48). This iterable is called the controlling iterable of the loop. B is a block of statements. The block is executed once for each value in S. During each execution of the block, V is set to the corresponding value of S in turn. Example:
>>> for color in ['black', 'blue', 'transparent']: ... print(color) ... black blue transparent

In general, you can use any number of induction variables. In this case, the members of the controlling iterable must themselves be iterables, which are unpacked into the induction variables in the same way as sequence unpacking as described in Section 1.2, The assignment statement: name = expression (p. 56). Here is an example.
>>> fourDays = ( ('First', 1, 'orangutan librarian'), ... ('Second', 5, 'loaves of dwarf bread'), ... ('Third', 3, 'dried frog pills'), ... ('Fourth', 2, 'sentient luggages') ) >>> for day, number, item in fourDays: ... print("On the {1} day of Hogswatch, my true love gave " ... "to me".format(day)) ... print("{0} {1}".format(number, item)) ... On the First day of Hogswatch, my true love gave to me 1 orangutan librarian On the Second day of Hogswatch, my true love gave to me 5 loaves of dwarf bread On the Third day of Hogswatch, my true love gave to me 3 dried frog pills On the Fourth day of Hogswatch, my true love gave to me 2 sentient luggages

You can change the induction variable inside the loop, but during the next pass through the loop, it will be set to the next element of the controlling iterable normally. Modifying the controlling iterable itself won't change anything; Python makes a copy of it before starting the loop.
>>> for ... ... ... ... Before: Before: Before: Before: >>> L = >>> for ... i in range(4): print("Before:", i, end=' ') i += 1000 print("After:", i) 0 After: 1000 1 After: 1001 2 After: 1002 3 After: 1003 [7, 6, 1912] n in L: L = [44, 55]

New Mexico Tech Computer Center

Python statements

59

... ... 7 6 1912

print(n)

2.5. The if statement 2.6. The tryexcept construct 2.7. The with statement 2.8. The yield statement: Generate one result of a generator

60

Python statements

New Mexico Tech Computer Center

Chapter 11. def(): Defining your own functions


In addition to Python's built-in functions, you may also define your own functions. Section 1, The function definition (p. 61): How to create a user-defined function. Section 2, Calling a function (p. 63): Calling functions. Section 3, A function's local namespace (p. 66): How namespaces work within functions. Section 4, Iterators: Values that can produce a sequence of values (p. 66): A general mechanism for iterating over a sequence of values. Section 5, Generators: Functions that can produce a sequence of values (p. 67): Functions that return a sequence of values one at a time. Section 6, Decorators (p. 68): Decorator expressions.

1. The function definition


Use the def construct to define a functions. Inside a class, use the def construct to define a method. Each definition has three parts: You may supply any number of decorators before the line starting the def. See Section 6, Decorators (p. 68). A statement starting with the def keyword names the function or method you are defining, and describes any parameters that are passed in. The body of the function or method is an indented block. This block is executed when the function is called. Here is a brief interactive example of a function to compute cube roots.
>>> def cubeRoot(x): ... '''Returns the cube root of a number x. ... ''' ... return x**(1.0/3.0) ... >>> cubeRoot(27.1) 3.00369914061521 >>> cubeRoot.__doc__ 'Returns the cube root of a number x.\n '

The above example demonstrates use of a documentation string: if the first indented line after the def is a string constant, that constant is saved as the documentation string for that function, and is available as the .__doc__ attribute of the function. Here is the syntax of the def statement.

The identifier is the name of the function you are defining.

New Mexico Tech Computer Center

def(): Defining your own functions

61

The param_list defines the parameters to the function. It is optional. The -> followed by an expression is optional. It may be used to annotate the expected result type, e.g., def f(k) -> int: . For the definition of suite, see Section 2, Compound statements (p. 56). Here is the syntax for param_list.

Each param consists of an identifier, optionally followed by a colon and an expression that annotates the expected type. This annotation, if given, is not checked; it is by way of documentation. For example, def f(i:int) declares a parameter i and annotates that an int value is expected. Each defparam is a param, optionally followed by an = and a default value for that parameter.

Warning
The default value expression is evaluated when the function is defined. This value is stored away and bound to the corresponding name each time the function is called without a corresponding argument. When the default value expresion is a mutable value, this can lead to undesirable side effects. For example:
>>> ... ... ... >>> [5] >>> [5, def f(x, L=[]): L.append(x) print(L) f(5) f(10) 10]

When the function is defined, a new empty list is created and saved as the default value of the function. The first call adds the value 5 to the saved default value, so when it is called the second time, the default value that is used already has the value 5 in it. To avoid this behavior, use None as the default value, and then add code that detects this case and creates the default value anew each time:
>>> def f2(x, L=None): ... if L is None: ... L=[] ... L.append(x) ... print(L) ... >>> f2(5) [5] >>> f2(10) [10]

62

def(): Defining your own functions

New Mexico Tech Computer Center

If a single * is given, followed by param, that name is bound to a tuple containing any extra positional arguments. See Section 2, Calling a function (p. 63). If a single * appears but is not followed by a param, it signifies that all the following arguments must be specified using the keyword, name=expr syntax. Here is an example. This function has one required positional argument, but it also requires one keyword argument named b.
>>> def f(a, *, b): ... print(a, b) ... >>> f(0, b=4) 0 4 >>> f(0, 4) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: f() takes exactly 1 positional argument (2 given) >>> f(0, 4, b=5) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: f() takes exactly 1 non-keyword positional argument (2 given)

If the parameter list includes ** followed by a param, that name is bound to a dictionary containing all the extra keyword arguments passed in the function call. For example:
>>> def f(*p, **kw): ... print(p, kw) ... >>> f() () {} >>> f(4, 88, name='Clem', clan='Bozo') (4, 88) {'clan': 'Bozo', 'name': 'Clem'}

2. Calling a function
Here is the syntax for a function call.

The primary is the name of the thing you are calling. Callable values include: Built-in functions and methods of builtin types. User-defined functions and methods of user-defined classes. Instances of any user-defined class that defines the special method Section 2.1, .__call__(): What to do when someone calls an instance (p. 73).

New Mexico Tech Computer Center

def(): Defining your own functions

63

The arg_list is the list of arguments you are passing; a trailing comma is ignored if present. You may, instead of the usual argument list, include one comprehension expression (see Section 5, Comprehensions (p. 50)); in that case, the function will be passed one positional argument, which will be a generator.

In the above diagram, pos_args refers to a sequence of positional arguments, and key-args is a sequence of keyword arguments.

The sequence *expr indicates that the associated name will be bound to a tuple containing all extra positional arguments. See the rules for evaluation below. The sequence **expr indicates that the associated name will be bound to a dictionary containing all extra keyword arguments. If you declare additional key-args after the *expr item, those arguments may be passed only as keyword arguments; they may never match a position argument. This is how the arguments actually passed are matched with the parameters to the function. 1. Python creates a list of unfilled slots, one for each declared parameter. 2. The actual positional arguments (pos_args) are placed into those slots in order. 3. For each keyword argument (key-args), if the slot for that name is empty, it is filled with that argument. If the slot was already full, Python raises a TypeError. 4. All unfilled slots are filled with the default value. If any unfilled slots do not have a default value, Python raises a TypeError. 5. If there are more positional arguments than there are slots for them, they are bound to the *expr item is a tuple, if there is one; otherwise Python raises a TypeError. 6. If there are keyword arguments whose names do not match any of the keyword parameters, they are bound to the **expr item as a dictionary, if there is one; otherwise Python raises a TypeError. When you call a function, the argument values you pass to it must obey these rules: There are two kinds of arguments: positional (also called non-default arguments) and keyword (also called default arguments). A positional argument is simply an expression, whose value is passed to the argument.

64

def(): Defining your own functions

New Mexico Tech Computer Center

A keyword argument has this form:


name=expression

All positional arguments in the function call (if any) must precede all keyword arguments (if any).
>>> def wrong(f=1, g): ... print(f, g) ... File "<stdin>", line 1 SyntaxError: non-default argument follows default argument

You must supply at least as many positional arguments as the function expects.
>>> def wantThree(a, b, c): ... print(a,b,c) ... >>> wantThree('nudge', 'nudge', 'nudge') nudge nudge nudge >>> wantThree('nudge') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: wantThree() takes exactly 3 arguments (1 given)

If you supply more positional arguments than the function expects, the extra arguments are matched against keyword arguments in the order of their declaration in the def. Any additional keyword arguments are set to their default values.
>>> def f(a, b, c=1, d='elk'): ... print(a,b,c,d) ... >>> f(99, 111) 99 111 1 elk >>> f(99, 111, 222, 333) 99 111 222 333 >>> f(8, 9, 10, 11, 12, 13) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: f() takes at most 4 arguments (6 given)

You may supply arguments for keyword parameters in any order by using the form k=v, where k is the keyword used in the declaration of that parameter and v is your desired argument.
>>> def blackKeys(fish='Eric', dawn='Stafford', attila='Abdul'): ... print(fish, dawn, attila) ... >>> blackKeys() Eric Stafford Abdul >>> blackKeys(attila='Gamera', fish='Abdul') Abdul Stafford Gamera

If you declare a parameter of the form *name, the caller can provide any number of additional keyword arguments, and the name will be bound to a tuple containing those additional arguments.
>>> def posish(i, j, k, *extras): ... print(i,j,k,extras) ... >>> posish(38, 40, 42)

New Mexico Tech Computer Center

def(): Defining your own functions

65

38 40 42 () >>> posish(44, 46, 48, 51, 57, 88) 44 46 48 (51, 57, 88)

Similarly, you may declare a final parameter of the form **name. If the caller provides any keyword arguments whose names do not match declared keyword arguments, that name will be bound to a dictionary containing the additional keyword arguments as key-value pairs.
>>> ... ... >>> 1 2 >>> 3 4 def extraKeys(a, b=1, *c, **d): print(a, b, c, d) extraKeys(1,2) () {} extraKeys(3,4,6,12, hovercraft='eels', record='scratched') (6, 12) {'record': 'scratched', 'hovercraft': 'eels'}

3. A function's local namespace


Any name that appears in a function's argument list, or any name that is set to a value anywhere in the function, is said to be local to the function. If a local name is the same as a name from outside the function (a so-called global name), references to that name inside the function will refer to the local name, and the global name will be unaffected. Here is an example:
>>> x = 'lobster' >>> y = 'Thermidor' >>> def f(x): ... y = 'crevettes' ... print(x, y) ... >>> f('spam') spam crevettes >>> print(x, y) lobster Thermidor

Keyword parameters have a special characteristic: their names are local to the function, but they are also used to match keyword arguments when the function is called.

4. Iterators: Values that can produce a sequence of values


Closely related to Python's concept of sequences is the concept of an iterator: For a given sequence S, an iterator I is essentially a set of instructions for producing the elements of S as a sequence of zero or more values. To produce an iterator over some sequence S, use this function:
iter(S)

The result of this function is an iterator object that can be used in a for statement.
>>> continents = ('AF', 'AS', 'EU', 'AU', 'AN', 'SA', 'NA') >>> worldWalker = iter(continents) >>> type(worldWalker) <class 'tuple_iterator'> >>> for landMass in worldWalker:

66

def(): Defining your own functions

New Mexico Tech Computer Center

... print("Visit {0}.".format(landMass), end=' ') ... Visit AF. Visit AS. Visit EU. Visit AU. Visit AN. Visit SA. Visit NA.

All iterators have a .next() method that you can call to get the next element in the sequence. This method takes no arguments. It returns the next element in the sequence, if any. When there are no more elements, it raises a StopIteration exception.
>>> trafficSignal = [ 'green', 'yellow', 'red' ] >>> signalCycle = iter(trafficSignal) >>> type(signalCycle) <type 'listiterator'> >>> signalCycle.next() 'green' >>> signalCycle.next() 'yellow' >>> signalCycle.next() 'red' >>> signalCycle.next() Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration

Once an iterator is exhausted, it will continue to raise StopIteration indefinitely. You can also use an iterator as the right-hand operand of the in operator.
>>> signalCycle = iter(trafficSignal) >>> 'red' in signalCycle True

5. Generators: Functions that can produce a sequence of values


Unlike conventional functions that return only a single result, a generator is a function that produces a sequence of zero or more results. Generators are a special case of iterators (see Section 4, Iterators: Values that can produce a sequence of values (p. 66)), so they can be used as the controlling iterable in for statements and the other places where iterators are allowed. In a conventional function, the body of the function is executed until it either executes a return statement, or until it runs out of body statements (which is the equivalent of a return None statement). By contrast, when a generator function is called, its body is executed until it either has another value to produce, or until there are no more values. When a function wishes to return the next generated value, it executes a statement of this form:
yield e

where the e is any Python expression. The difference between yield and return is that when a return is executed, the function is considered finished with its execution, and all its current state diasppears. By contrast, when a function executes a yield statement, execution of the function is expected to resume just after that statement, at the point when the caller of the function needs the next generated value.

New Mexico Tech Computer Center

def(): Defining your own functions

67

A generator signals that there are no more values by executing this statement:
raise StopIteration

For an example of a generator, see Section 2.8, The yield statement: Generate one result of a generator (p. 60). If you are writing a container class (that is, a class whose instances are containers for a set of values), and you want to define an iterator (see Section 2.3, .__iter__(): Create an iterator (p. 73)), that method can be a generator. Here is a small example. The constructor for class Bunch takes a sequence of values and stores them in instance attribute .__stuffList. The iterator method .__iter__() generates the elements of the sequence in order, except it wraps each of them in parentheses:
>>> class Bunch(object): ... def __init__(self, stuffList): ... self.__stuffList = stuffList ... def __iter__(self): ... for thing in self.__stuffList: ... yield "({0})".format(thing) ... raise StopIteration ... >>> mess = Bunch(('lobster Thermidor', 'crevettes', 'Mornay')) >>> for item in mess: ... print(item, end=' ') ... (lobster Thermidor) (crevettes) (Mornay) >>> messWalker = iter(mess) >>> for thing in messWalker: print(thing, end=' ') ... (lobster Thermidor) (crevettes) (Mornay)

6. Decorators
The purpose of a Python decorator is to replace a function or method with a modified version at the time it is defined. For example, the original way to declare a static method was like this:
def someMethod(x, y): ... someMethod = staticmethod(someMethod)

Using Python's decorator syntax, you can get the same effect like this:
@staticmethod def someMethod(x, y): ...

In general, a function or method may be preceded by any number of decorator expressions, and you may also provide arguments to the decorators. If a function f is preceded by a decorator expression of the form @d, it is the equivalent of this code:
def f(...): ... f = d(f)

You may provide a parenthesized argument list after the name of your decorator. A decorator expression d(...) is the equivalent of this code:

68

def(): Defining your own functions

New Mexico Tech Computer Center

def f(...): ... f = d(...)(f)

First, the decorator is called with the argument list you provided. It must return a callable object. That callable is then called with one argument, the decorated function. The name of the decorated function is then bound to the returned value. If you provide multiple decorators, they are applied inside out, in sequence from the last to the first. Here is an example of a function wrapped with two decorators, of which the second has additional arguments:
@f1 @f2('Pewty') def f0(...): ...

This is the equivalent code without using decorators:


def f0(...): ... f0 = f1 ( f2('Pewty') ( f0 ) )

First function f2 is called with one argument, the string 'Pewty'. The return value, which must be callable, is then called with f0 as its argument. The return value from that call is then passed to f1. Name f0 is then bound to the return value from the call to f1.

New Mexico Tech Computer Center

def(): Defining your own functions

69

70

def(): Defining your own functions

New Mexico Tech Computer Center

Chapter 12. Exceptions

New Mexico Tech Computer Center

Exceptions

71

72

Exceptions

New Mexico Tech Computer Center

Chapter 13. Classes: invent your own types


1. Defining a class 2. Special methods
2.1. .__call__(): What to do when someone calls an instance 2.2. __getitem__(): Get one item from a sequence or mapping
If a class defines it, this special method is called whenever a value is retrieved from a sequence or mapping (dictionary-like object) using the syntax v[i], where v is the sequence or mapping and i is a position in a sequence, or a key in a mapping. Here is the calling sequence:
def __getitem__(self, i): ...

The method either returns the corresponding item or raises an appropriate exception: IndexError for sequences or KeyError for mappings.

2.3. .__iter__(): Create an iterator

3. Static methods

New Mexico Tech Computer Center

Classes: invent your own types

73

74

Classes: invent your own types

New Mexico Tech Computer Center

Chapter 14. The conversion path from 2.x to 3.x


At this writing, both Python 2.7 and Python 3.2 are officially maintained implementations. The 3.0 release marked the first release in the development of Python that was incompatible with the old one. If you are using 2.x releases of Python, there is no hurry to convert to the 3.x series. Release 2.7 is guaranteed to be around for many years. Furthermore, there are tools to help you automate much of the conversion process. For a discussion of the changes between 2.7 and 3.2, see What's New in Python . To see what changes must be made in your program to allow automatic conversion to Python 3.x, run Python with this flag:
python -3 yourprogram
1

To convert your program to Python 3.x, first make a copy of the original program, then run this command:
python3-2to3 -w yourprogram

The -w flag replaces yourprogram with the converted 3.x version, and moves the original to yourprogram.bak

http://docs.python.org/py3k/whatsnew/

New Mexico Tech Computer Center

The conversion path from 2.x to 3.x

75

76

The conversion path from 2.x to 3.x

New Mexico Tech Computer Center

You might also like