Python 32
Python 32
Python 32
2 quick reference
John W. Shipman
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
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
List of Tables
3.1. Python's built-in types ........................................................................................................... 5 5.1. String escape sequences ....................................................................................................... 19 8.1. Python operator precedence ................................................................................................. 47
vi
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
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
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/
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
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).
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).
float
Python types
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).
bytes
bytearray
list tuple
None
Python types
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
Python types
Python types
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).
Numeric types
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
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
All other values are considered True. To convert any value to a Boolean, see Section 1.3, bool(): Convert to bool type (p. 51).
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).
Numeric types
11
12
Numeric types
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
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'
Additionally, these methods are supported on any value s that is one of the sequence types.
s.count(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
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
>>> "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
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'
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]
16
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
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.
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
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))
19
ASCII name
SP NL CR HT FF VT
English name space newline carriage return horizontal tab form feed vertical tab
http://en.wikipedia.org/wiki/ASCII
20
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---'
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/////'
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.
21
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 , 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'
5 6
http://docs.python.org/py3k/library/codecs.html#standard-encodings http://en.wikipedia.org/wiki/ASCII
22
'replace'
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'( )' '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
If given, specifies the starting position, counting from zero, of the string to be tested.
end
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.
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
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).
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
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
25
26
>>> '\xa0'.isprintable() False >>> 'abc def $*^'.isprintable() True >>> ''.isprintable() True
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
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'
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'
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
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.'
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')
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'
For example:
>>> 'banana'.find('a') 1 >>> 'banana'.rfind('a') 5 >>> 'banana'.rfind('slug') -1
30
>>> '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
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
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')
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.']
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()
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
>>> '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."]
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() ['']
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
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'
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.
34
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'
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'
If you need to include actual { and } characters in the result, double them, like this:
35
>>> "There are {0} members in set {{a}}.".format(15) 'There are 15 members in set {a}.'
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.
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
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?'
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"
37
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.
This option controls whether an arithmetic sign is displayed. There are three possible values:
38
+ -
Always display a sign: + for positive, - for negative. Display - only for negative values. Display one space for positive values, - for negative.
(one space)
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.
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
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%'
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
43
44
45
46
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
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.
http://en.wikipedia.org/wiki/Duck_typing
48
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).
2 3
http://en.wikipedia.org/wiki/ASCII http://docs.python.org/py3k/library/locale.html
49
5. Comprehensions
50
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."
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
2. Advanced functions
Built-in functions
53
54
Built-in functions
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).
Python statements
55
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.
56
Python statements
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.
Python statements
57
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
58
Python statements
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]
Python statements
59
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
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.
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
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).
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
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)
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'}
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.
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
... 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
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.
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
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(...): ...
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.
69
70
Exceptions
71
72
Exceptions
The method either returns the corresponding item or raises an appropriate exception: IndexError for sequences or KeyError for mappings.
3. Static methods
73
74
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/
75
76