Introduction to the practice of computing using python 2.7 programming
A Python 2.7 programming tutorial
Table of Contents
1. Introduction . 2
1.1. Starting Python in conversational mode .. 2
2. Python's numeric types 3
2.1. Basic numeric operations .. 3
2.2. The assignment statement . 5
2.3. More mathematical operations 8
3. Character string basics 10
3.1. String literals 10
3.2. Indexing strings .. 12
3.3. String methods 13
3.4. The string format method 18
4. Sequence types .. 20
4.1. Functions and operators for sequences .. 21
4.2. Indexing the positions in a sequence .. 22
4.3. Slicing sequences 23
4.4. Sequence methods . 25
4.5. List methods 25
4.6. The range() function: creating arithmetic progressions 26
4.7. One value can have multiple names 27
5. Dictionaries 29
5.1. Operations on dictionaries .. 29
5.2. Dictionary methods .. 31
5.3. A namespace is like a dictionary .. 33
6. Branching 34
6.1. Conditions and the bool type .. 34
6.2. The if statement 35
6.3. A word about indenting your code . 38
6.4. The for statement: Looping .. 38
6.5. The while statement 40
6.6. Special branch statements: break and continue 40
7. How to write a self-executing Python script .. 41
8. def: Defining functions . 42
8.1. return: Returning values from a function .. 43
8.2. Function argument list features 44
8.3. Keyword arguments . 45
8.4. Extra positional arguments . 46
8.5. Extra keyword arguments .. 46
8.6. Documenting function interfaces . 47
9. Using Python modules .. 47
9.1. Importing items from modules . 48
9.2. Import entire modules . 49
9.3. A module is a namespace 51
9.4. Build your own modules . 51
10. Input and output 52
10.1. Reading files .. 52
10.2. File positioning for random-access devices 54
10.3. Writing files 54
11. Introduction to object-oriented programming . 55
11.1. A brief history of snail racing technology 56
11.2. Scalar variables . 56
11.3. Snail-oriented data structures: Lists .. 57
11.4. Snail-oriented data structures: A list of tuples .. 58
11.5. Abstract data types . 60
11.6. Abstract data types in Python . 62
11.7. class SnailRun: A very small example class .. 62
11.8. Life cycle of an instance . 64
11.9. Special methods: Sorting snail race data .. 66
1. Introduction
This document contains some tutorials for the Python programming language, as of Python version 2.7. These tutorials accompany the free Python classes taught by the New Mexico Tech Computer
3
Center. Another good tutorial is at the Python .
1.1. Starting Python in conversational mode
This tutorial makes heavy use of Python's conversational mode. When you start Python in this way, you will see an initial greeting message, followed by the prompt “>>>”.
• On a TCC workstation in Windows, from the Start menu, select All Programs → Python 2.7 → IDLE (Python GUI). You will see something like this:
3
• For Linux or MacOS, from a shell prompt (such as “$” for the bash shell), type:
When you see the “>>>” prompt, you can type a Python expression, and Python will show you the result of that expression. This makes Python useful as a desk calculator. (That's why we sometimes refer to conversational mode as “calculator mode”.) For example:
Each section of this tutorial introduces a group of related Python features.
2. Python's numeric types
Pretty much all programs need to do numeric calculations. Python has several ways of representing numbers, and an assortment of operators to operate on numbers.
2.1. Basic numeric operations
To do numeric calculations in Python, you can write expressions that look more or less like algebraic expressions in many other common languages. The “+” operator is addition; “-” is subtraction; use “*” to multiply; and use “/” to divide. Here are some examples:
The examples in this document will often use a lot of extra space between the parts of the expression, just to make things easier to read. However, these spaces are not required:
When an expression contains more than one operation, Python defines the usual order of operations, so that higher-precedence operations like multiplication and division are done before addition and subtraction. In this example, even though the multiplication comes after the addition, it is done first.
You might expect a result of 0.2, not zero. However, Python has different kinds of numbers. Any number without a decimal point is considered an integer, a whole number. If any of the numbers involved contain a decimal point, the computation is done using floating point type:
That second example above may also surprise you. Why is the last digit a one? In Python (and in pretty much all other contemporary programming languages), many real numbers cannot be represented exactly. The representation of 1.0/3.0 has a slight error in the seventeenth decimal place. This behavior may be slightly annoying, but in conversational mode, Python doesn't know how much precision you want, so you get a ridiculous amount of precision, and this shows up the fact that some values are approximations. You can use Python's print statement to display values without quite so much precision:
It's okay to mix integer and floating point numbers in the same expression. Any integer values are coerced to their floating point equivalents.
Later we will learn about Python's string format method , which allows you to specify exactly how much precision to use when displaying numbers. For now, let's move on to some more of the operators on numbers.
The “%” operator between two numbers gives you the modulo. That is, the expression “x % y” returns the remainder when x is divided by y.
That last number, 1.2676506002282294e+30, is an example of exponential or scientific notation. This number is read as “1.26765 times ten to the 30th power”. Similarly, a number like 1.66e-24 is read as “1.66 times ten to the minus 24th power”.
So far we have seen examples of the integer type, which is called int in Python, and the floating-point type, called the float type in Python. Python guarantees that int type supports values between 2,147,483,648 and 2,147,483,647 (inclusive).
There is another type called long, that can represent much larger integer values. Python automatically switches to this type whenever an expression has values outside the range of int values. You will see letter “L” appear at the end of such values, but they act just like regular integers.
... ...
2.2. The assignment statement
So far we have worked only with numeric constants and operators. You can attach a name to a value, and that value will stay around for the rest of your conversational Python session.
Python names must start with a letter or the underbar (_) character; the rest of the name may consist of letters, underbars, or digits. Names are case-sensitive: the name Count is a different name than count.
For example, suppose you wanted to answer the question, “how many days is a million seconds?” We can start by attaching the name sec to a value of a million:
A statement of this type is called an assignment statement. To compute the number of minutes in a million seconds, we divide by 60. To convert minutes to hours, we divide by 60 again. To convert hours to days, divide by 24, and that is the final answer.
You can attach more than one name to a value. Use a series of names, separated by equal signs, like this.
Here are the rules for evaluating an assignment statement:
• Each namei is some Python variable name. Variable names must start with either a letter or the underbar (_) character, and the remaining characters must be letters, digits, or underbar characters. Examples: skateKey; _x47; sum_of_all_fears.
• The expression is any Python expression.
• When the statement is evaluated, first the expression is evaluated so that it is a single value. For example, if the expression is “(2+3)*4”, the resulting single value is the integer 20.
Then all the names namei are bound to that value.
What does it mean for a name to be bound to a value? When you are using Python in conversational mode, the names and value you define are stored in an area called the global namespace. This area is like a two-column table, with names on the left and values on the right.
Here is an example. Suppose you start with a brand new Python session, and type this line:
Here is what the global namespace looks like after the execution of this assignment statement.
Global namespace
Name Value
In this diagram, the value appearing on the right shows its type, int (integer), and the value, 5100.
In Python, values have types, but names are not associated with any type. A name can be bound to a value of any type at any time. So, a Python name is like a luggage tag: it identifies a value, and lets you retrieve it later.
Here is another assignment statement, and a diagram showing how the global namespace appears after the statement is executed.
The expression “i + 1” is equivalent to “5100 + 1”, since variable i is bound to the integer 5100. This expression reduces to the integer value 5101, and then the names j and foo are both bound to that value. You might think of this situation as being like one piece of baggage with two tags tied to it.
Let's examine the global namespace after the execution of this assignment statement:
Because foo starts out bound to the integer value 5101, the expression “foo + 1” simplifies to the value 5102. Obviously, foo = foo + 1 doesn't make sense in algebra! However, it is a common way for programmers to add one to a value.
Note that name j is still bound to its old value, 5101.
2.3. More mathematical operations
Python has a number of built-in functions. To call a function in Python, use this general form:
That is, use the function name, followed by an open parenthesis “(”, followed by zero or more arguments separated by commas, followed by a closing parenthesis “)”.
For example, the round function takes one numeric argument, and returns the nearest whole number (as a float number). Examples:
The result of that last case is somewhat arbitrary, since 4.5 is equidistant from 4.0 and 5.0. However, as in most other modern programming languages, the value chosen is the one further from zero. More examples:
For historical reasons, trigonometric and transcendental functions are not built-in to Python. If you want to do calculations of those kinds, you will need to tell Python that you want to use the math package. Type this line:
Once you have done this, you will be able to use a number of mathematical functions. For example, sqrt(x) computes the square root of x:
Importing the math module also adds two predefined variables, pi (as in π) and e, the base of natural logarithms:
Here's an example of a function that takes more than argument. The function atan2(dy , dx) returns the arctangent of a line whose slope is dy/dx.
4
For a complete list of all the facilities in the math module, see the . Here are some more examples; log is the natural logarithm, and log10 is the common logarithm:
Mathematically, cos(π/2) should be zero. However, like pretty much all other modern programming
-17 languages, transcendental functions like this use approximations. 6.12×10 is, after all, pretty close to zero.
Two math functions that you may find useful in certain situations:
• floor(x) returns the largest whole number that is less than or equal to x.
• ceil(x) returns the smallest whole number that is greater than or equal to x.
Note that the floor function always moves toward -∞ (minus infinity), and ceil always moves toward +∞.
3. Character string basics
Python has extensive features for handling strings of characters. There are two types: • A str value is a string of zero or more 8-bit characters. The common characters you see on North
American keyboards all use 8-bit characters. The official name for this character set is , for American Standard Code for Information Interchange.
This character set has one surprising property: all capital letters are considered less than all lowercase letters, so the string "Z" sorts before string "a".
• A unicode value is a string of zero or more 32-bit Unicode characters. The Unicode character set covers just about every written language and almost every special character ever invented.
We'll mainly talk about working with str values, but most unicode operations are similar or identical, except that Unicode literals are preceded with the letter u: for example, "abc" is type str, but u"abc" is type unicode.
3.1. String literals
In Python, you can enclose string constants in either single-quote (' ') or double-quote (" ") characters.
When you display a string value in conversational mode, Python will usually use single-quote characters. Internally, the values are the same regardless of which kind of quotes you use. Note also that the print statement shows only the content of a string, without any quotes around it.
To convert an integer (int type) value i to its string equivalent, use the function “str(i)”:
File "", line 1, in ?
ValueError: invalid literal for int(): 012this ain't no number
The last example above shows what happens when you try to convert a string that isn't a valid number. To convert a string s containing a number in base B, use the form “int(s, B)”:
To obtain the 8-bit integer code contained in a one-character string s, use the function “ord(s)”. The inverse function, to convert an integer i to the character that has code i, use “chr(i)”. The numeric
6 values of each character are defined by the ASCII character set.
In addition to the printable characters with codes in the range from 32 to 127 inclusive, a Python string can contain any of the other unprintable, special characters as well. For example, the null character, whose official name is NUL, is the character whose code is zero. One way to write such a character is to use this form:
Another special character you may need to deal with is the newline character, whose official name is LF (for “line feed”). Use the special escape sequence “\n” to produced this character.
As you can see, when a newline character is displayed in conversational mode, it appears as “\n”, but when you print it, the character that follows it will appear on the next line. The code for this character is 10:
Python has several other of these escape sequences. The term “escape sequence” refers to a convention where a special character, the “escape character”, changes the meaning of the characters after it. Python's escape character is backslash (\).
Input Code Name Meaning
\b 8 BS backspace
\t 9 HT tab
\" 34 " Double quote
\' 39 ' Single quote
\\ 92 \ Backslash
There is another handy way to get a string that contains newline characters: enclose the string within three pairs of quotes, either single or double quotes.
Notice that in Python's conversational mode, when you press Enter at the end of a line, and Python knows that your line is not finished, it displays a “ ” prompt instead of the usual “>>>” prompt.
3.2. Indexing strings
To extract one or more characters from a string value, you have to know how positions in a string are numbered.
Here, for example, is a diagram showing all the positions of the string 'ijklm'.
−5 −4 −3 −2 −1
i | j | k | l | m |
0 1 2 3 4 5
In the diagram above, the numbers show the positions between characters. Position 0 is the position before the first character; position 1 is the position between the first and characters; and so on.
You may also refer to positions relative to the end of a string. Position -1 refers to the position before the last character; -2 is the position before the next-to-last character; and so on.
To extract from a string s the character that occurs just after position n, use an expression of this form:
The last example shows what happens when you specify a position after all the characters in the string.
You can also extract multiple characters from a string; see Section 4.3, “Slicing sequences” (p. 23).
3.3. String methods
Many of the operations on strings are expressed as methods. A method is sort of like a function that lives only inside values of a certain type. To call a method, use this syntax:
where each argi is an argument to the method, just like an argument to a function.
For example, any string value has a method called center that produces a new string with the old value centered, using extra spaces to pad the value out to a given length. This method takes as an argument the desired new length. Here's an example:
The following sections describe some of the more common and useful string methods.
3.3.1. .center(): Center some text
Given some string value s, to produce a new string containing s centered in a string of length n, use this method call:
Note that in the first example we are asking Python to center the string "Ni" in a field of length 5. Clearly we can't center a 2-character string in 5 characters, so Python arbitrarily adds the leftover space character before the old value.
3.3.2. .ljust() and .rjust(): Pad to length on the left or right
Another useful string method left-justifies a value in a field of a given length. The general form:
For any string expression s, this method returns a new string containing the characters from s with enough spaces added after it to make a new string of length n.
Note that the .ljust() method will never return a shorter string. If the length isn't enough, it just returns the original value.
There is a similar method that right-justifies a string value:
This method returns a string with enough spaces added before the value to make a string of length n. As with the .ljust() method, it will never return a string shorter than the original.
3.3.3. .strip(), .lstrip(), and .rstrip(): Remove leading and/or trailing whitespace
Sometimes you want to remove whitespace (spaces, tabs, and newlines) from a string. For a string s, use these methods to remove leading and trailing whitespace:
• s.strip() returns s with any leading or trailing whitespace characters removed.
• s.lstrip() removes only leading whitespace.
• s.rstrip() removes only trailing whitespace.
3.3.4..count(): How many occurrences?
The method s.count(t) searches string s to see how many times string t occurs in it.
Note that this method does not count overlapping occurrences. Although the string "ana" occurs twice in string "banana", the occurrences overlap, so "banana".count("ana") returns only 1.
3.3.5. .find() and .rfind(): Locate a string within a longer string
Use this method to search for a string t within a string s:
If t matches any part of s, the method returns the position where the first match begins; otherwise, it returns -1.
If you need to find the last occurrence of a substring, use the similar method s.rfind(t), which returns the position where the last match starts, or -1 if there is no match.
3.3.6..startswith()and.endswith()
You can check to see if a string s starts with a string t using a method call like this:
The special values True and False are discussed later in Section 6.1, “Conditions and the bool type” (p. 34).
3.3.7. .lower() and .upper(): Change the case of letters
The methods s.lower() and s.upper() are used to convert uppercase characters to lowercase, and vice versa, respectively.
3.3.8. Predicates for testing for kinds of characters
Use the string methods in this section to test whether a string contains certain kinds of characters. Each of these methods is a predicate, that is, it asks a question and returns a value of True or False.
• s.isalpha() tests whether all the characters of s are letters.
• s.isupper() tests whether all the letters of s are uppercase. (It ignores any non-letter characters.)
• s.islower() tests whether all the letters of s are lowercase letters. (This method also ignores nonletter characters.)
• s.isdigit() tests whether all the characters of s are digits.
3.3.9. .split(): Break fields out of a string
The .split() method is used to break a string up into pieces wherever a certain string called the delimiter is found; it returns a list of strings containing the text between the delimiters. For example, suppose you have a string that contains a series of numbers separated by whitespace. A call to the .split() method on that string, with no arguments, returns a list of the parts of the string that are surrounded by whitespace.
3.4. The string format method
One of the commonest string operations is to combine fixed text and variable values into a single string. For example, maybe you have a variable named nBananas that contains the number of bananas, and you want to format a string something like "We have 27 bananas today". Here's how you do it:
In this form:
• S is a format string that specifies the fixed parts of the desired text and also tells where the variable parts are to go and how they are to look.
• The .format() method takes zero or more positional arguments pi followed by and zero or more keyword arguments ki=ei, where each ki is any Python name and each ei is any Python expression.
• The format string contains a mixture of ordinary text and format codes. Each of the format codes is enclosed in braces { }. A format code containing a number refers to the corresponding positional argument, and a format code containing a name refers to the corresponding keyword argument.
Examples:
>>> "We have {0} bananas.".format(27)
'We have 27 bananas.'
>>> "We have {0} cases of {1} today.".format(42, 'peaches')
'We have 42 cases of peaches today.'
>>> "You'll have {count} new {thing}s by {date}".format(
count=27, date="St. Swithin's Day", thing="cooker")
"You'll have 27 new cookers by St. Swithin's Day"
You can control the formatting of an item by using a format code of the form “{N:type}”, where N is the number or name of the argument to the .format() method, and type specifies the details of the formatting.
The type may be a single type code like s for string, d for integer, or f for float.
You may also include a field size just before the type code. With float values, you can also specify a precision after the field size by using a “.” followed by the desired number of digits after the decimal.
Notice in the last example above that it is possible for you to produce any number of spurious digits beyond the precision used to specify the number originally! Beware, because those extra digits are utter garbage.
When you specify a precision, the value is rounded to the nearest value with that precision.
The “e” type code forces exponential notation. You may also wish to use the “g” (for general) type code, which selects either float or exponential notation depending on the value.
By default, strings are left-justified within the field size and numbers are right-justified. You can change this by placing an alignment code just after the “:”: “<” to left-align the field, “^” to center it, and “>” to right-align it.
Normally, short values are padded to length with spaces. You can specify a different padding character by placing it just after the “:”.
If you need to produce any “{” or “}” characters in the result, you must double them within the format code.
>>> "Set {0}: contents {{red, green, blue}}".format('glory')
'Set glory: contents {red, green, blue}'
One thing we sometimes need to is to format something to a size that is not known until the program is running. For example, suppose we want to format a ticket number from a variable named ticket_no, with left zero fill, and the width is given by a variable named how_wide. This would do the job:
Here, where the width is expected, “{w}” appears. Because there is a keyword argument that is effectively w=8, the value "8" is used for the width.
Note
The string .format() method has been available only since Python 2.6. If you are looking at older
code, you may see a different technique using the “%” operator. For example, 'Attila the %s' %
7
'Bun' yields 'Attila the bun'. For an explanation, see the Python library documentation . However, the old format operator is deprecated.
4. Sequence types
Mathematically, a sequence in Python represents an ordered set.
Sequences are an example of container classes: values that contain other values inside them.
#string-formatting-operations
Type name Contains Examples Mutable? unicode 32-bit characters u'abc' u'\u000c' No list Any values [23, "Ruth", 69.8] [] Yes tuple Any values (23, "Ruth", 69.8) () (44,) No
• str and unicode are used to hold text, that is, strings of characters.
• list and tuple are used for sequences of zero or more values of any type. Use a list if the contents of the sequence may change; use a tuple if the contents will not change, and in certain places where tuples are required. For example, the right-hand argument of the string format operator (see Section 3.4, “The string format method” (p. 18)) must be a tuple if you are formatting more than one value.
• To create a list, use an expression of the form
with a list of zero or more values between square brackets, “[…]”.
• To create a tuple, use an expression of the form
with a list of zero or more values enclosed in parentheses, “(…”).
To create a tuple with only one element v, use the special syntax “(v,)”. For example, (43+1,) is a one-element tuple containing the integer 44. The trailing comma is used to distinguish this case from the expression “(43+1)”, which yields the integer 44, not a tuple.
• Mutability: You can't change part of an immutable value. For example, you can't change the first character of a string from 'a' to 'b'. It is, however, easy to build a new string out of pieces of other strings.
Here are some calculator-mode examples. First, we'll create a string named s, a list named L, and a tuple named t:
4.1. Functions and operators for sequences
The built-in function len(S) returns the number of elements in a sequence S.
Function max(S) returns the largest value in a sequence S, and function min(S) returns the smallest value in a sequence S.
To test for set membership, use the “in” operator. For a value v and a sequence S, the expression v in S returns the Boolean value True if there is at least one element of S that equals v; it returns False otherwise.
There is an inverse operator, v not in S, that returns True if v does not equal any element of S, False otherwise.
When the “*” operator occurs between a sequence S and an integer n, you get a new sequence containing n repetitions of the elements of S.
4.2. Indexing the positions in a sequence
Positions in a sequence refer to locations between the values. Positions are numbered from left to right starting at 0. You can also refer to positions in a sequence using negative numbers to count from right to left: position -1 is the position before the last element, position -2 is the position before the next-tolast element, and so on.
Here are all the positions of the string "ijklm".
−5 −4 −3 −2 −1
i | j | k | l | m |
0 1 2 3 4 5
To extract a single element from a sequence, use an expression of the form S[i], where S is a sequence, and i is an integer value that selects the element just after that position.
4.3. Slicing sequences
For a sequence S, and two positions B and E within that sequence, the expression S [ B : E ] produces a new sequence containing the elements of S between those two positions.
Note in the example above that the elements are selected from position 2 to position 4, which does not include the element L[4].
You may omit the starting position to slice elements from at the beginning of the sequence up to the specified position. You may omit the ending position to specify a slice that extends to the end of the sequence. You may even omit both in order to get a copy of the whole sequence.
You can replace part of a list by using a slicing expression on the left-hand side of the “=” in an assignment statement, and providing a list of replacement elements on the right-hand side of the “=”. The elements selected by the slice are deleted and replaced by the elements from the right-hand side.
In slice assignment, it is not necessary that the number of replacement elements is the same as the number of replaced elements. In this example, the second and third elements of L are replaced by the five elements from the list on the right-hand side.
4.4. Sequence methods
To find the position of a value V in a sequence S, use this method:
This method returns the position of the first element of S that equals V. If no elements of S are equal, Python raises a ValueError exception.
4.5. List methods
All list instances have methods for changing the values in the list. These methods work only on lists. They do not work on the other sequence types that are not mutable, that is, the values they contain may not be changed, added, or deleted.
For example, for any list instance L, the .append(v) method appends a new value v to that list.
The method L.remove(V) removes the first element of L that equals V, if there is one. If no elements equal V, the method raises a ValueError exception.
Note that the .sort() method itself does not return a value; it sorts the values of the list in place. A similar method is .reverse(), which reverses the elements in place:
4.6. The range() function: creating arithmetic progressions
The term arithmetic progression refers to a sequence of numbers such that the difference between any two successive elements is the same. Examples: [1, 2, 3, 4, 5]; [10, 20, 30, 40]; [88, 77, 66, 55, 44, 33].
Python's built-in range() function returns a list containing an arithmetic progression. There are three different ways to call this function.
To generate the sequence [0, 1, 2, , n-1], use the form range(n).
Note that the sequence will never include the value of the argument n; it stops one value short.
To generate a sequence [i, i+1, i+2, , n-1], use the form range(i, n):
To generate an arithmetic progression with a difference d between successive values, use the three-argument form range(i, n, d). The resulting sequence will be [i, i+d, i+2*d, ], and will stop before it reaches a value equal to n.
4.7. One value can have multiple names
It is necessary to be careful when modifying mutable values such as lists because there may be more than one name bound to that value. Here is a demonstration.
We start by creating a list of two strings and binding two names to that list.
If we appended a third string to menu1, why does that string also appear in list menu2? The answer lies in the definition of Python's assignment statement: To evaluate an assignment statement of the form
where each Vi is a variable, and expr is some expression, first reduce expr to a single value, then bind each of the names vi to that value.
So let's follow the example one line at a time, and see what the global namespace looks like after each step. First we create a list instance and bind two names to it:
Global namespace
Name Value
Two different names, menu1 and menu2, point to the same list. Next, we create an element-by-element copy of that list and bind the name menu3 to the copy.
Global namespace Name Value
So, when we add a third string to menu1's list, the name menu2 is still bound to that same list.
Global namespace
Name Value
This behavior is seldom a problem in practice, but it is important to keep in mind that two or more names can be bound to the same value.
If you are concerned about modifying a list when other names may be bound to the same list, you can always make a copy using the slicing expression “L[:]”.
5. Dictionaries
Python's dictionary type is useful for many applications involving table lookups. In mathematical terms:
A Python dictionary is a set of zero or more ordered pairs (key, value) such that:
• The value can be any type.
• Each key may occur only once in the dictionary.
• No key may be mutable. In particular, a key may not be a list or dictionary, or a tuple containing a list or dictionary, and so on.
The idea is that you store values in a dictionary associated with some key, so that later you can use that key to retrieve the associated value.
5.1. Operations on dictionaries
The general form used to create a new dictionary in Python looks like this:
To retrieve the value associated with key k from dictionary d, use an expression of this form:
Note that when you try to retrieve the value for which no key exists in the dictionary, Python raises a KeyError exception.
To add or replace the value for a key k in dictionary d, use an assignment statement of this form:
This operation returns True if k is a key in dictionary d, False otherwise.
The construct “k not in d” is the inverse test: it returns True if k is not a key in d, False if it is a key.
5.2. Dictionary methods
A number of useful methods are defined on any Python dictionary. To test whether a key k exists in a dictionary d, use this method:
This is the equivalent of the expression “k in d”: it returns True if the key is in the dictionary, False otherwise.
You can get all the keys and all the values at the same time with this expression, which returns a list of 2-element tuples, in which each tuple has one key and one value as (k, v).
If k is a key in d, this method returns d[k]. However, if k is not a key, the method returns the special value None. The advantage of this method is that if the k is not a key in d, it is not considered an error.
Note that when you are in conversational mode, and you type an expression that results in the value None, nothing is printed. However, the print statement will display the special value None visually as the example above shows.
There is another way to call the .get() method, with two arguments:
In this form, if key k exists, the corresponding value is returned. However, if k is not a key in d, it returns the default value.
Here is another useful dictionary method. This is similar to the two-argument form of the .get() method, but it goes even further: if the key is not found, it stores a default value in the dictionary.
If key k exists in dictionary d, this expression returns the value d[k]. If k is not a key, it creates a new dictionary entry as if you had said “d[k] = default”.
This method adds all the key-value pairs from d2 to d1. For any keys that exist in both dictionaries, the value after this operation will be the value from d2.
>>> colors = { 1: "red", 2: "green", 3: "blue" }
>>> moreColors = { 3: "puce", 4: "taupe", 5: "puce" }
>>> colors.update ( moreColors )
>>> colors
{1: 'red', 2: 'green', 3: 'puce', 4: 'taupe', 5: 'puce'}
Note in the example above that key 3 was in both dictionaries, but after the .update() method call, key 3 is related to the value from moreColors.
5.3. A namespace is like a dictionary
Back in Section 2.2, “The assignment statement” (p. 5), we first encountered the idea of a namespace. When you start up Python in conversational mode, the variables and functions you define live in the “global namespace”.
We will see later on that Python has a number of different namespaces in addition to the global namespace. Keep in mind that namespaces are very similar to dictionaries:
• The names are like the keys of a dictionary: each one is unique.
• The values bound to those names are like the values in a dictionary. They can be any value of any type.
We can even use the same picture for a dictionary that we use to illustrate a namespace. Here is a small dictionary and a picture of it:
6. Branching
By default, statements in Python are executed sequentially. Branching statements are used to break this sequential pattern.
• Sometimes you want to perform certain operations only in some cases. This is called a conditional branch.
• Sometimes you need to perform some operations repeatedly. This is called looping.
Before we look at how Python does conditional branching, we need to look at Python's Boolean type.
6.1. Conditions and the bool type
Boolean algebra is the mathematics of true/false decisions. Python's bool type has only two values: True and False.
A typical use of Boolean algebra is in comparing two values. In Python, the expression x < y is True if x is less than y, False otherwise.
The operator that compares for equality is “==”. (The “=” symbol is not an operator: it is used only in the assignment statement.)
Here are some more examples:
Python has a function cmp(x, y) that compares two values and returns:
• Zero, if x and y are equal.
• A negative number if x < y.
• A positive number if x > y.
The function bool(x) converts any value x to a Boolean value. The values in this list are considered False; any other value is considered True:
• Any numeric zero: 0, 0L, or 0.0.
• Any empty sequence: "" (an empty string), [] (an empty list), () (an empty tuple).
• {} (an empty dictionary).
• The special unique value None.
>>> print bool(0), bool(0L), bool(0.0), bool(''), bool([]), bool(())
False False False False False False
>>> print bool({}), bool(None)
False False
>>> print bool(1), bool(98.6), bool('Ni!'), bool([43, "hike"]) True True True True
6.2. The if statement
The purpose of an if statement is to perform certain actions only in certain cases.
Here is the general form of a simple “one-branch” if statement. In this case, if some condition C is true, we want to execute some sequence of statements, but if C is not true, we don't want to execute those statements.
Here is a picture showing the flow of control through a simple if statement. Old-timers will recognize this as a flowchart.
There can be any number of statements after the if, but they must all be indented, and all indented the same amount. This group of statements is called a block.
When the if statement is executed, the condition C is evaluated, and converted to a bool value (if it isn't already Boolean). If that value is True, the block is executed; if the value is False, the block is skipped.
Here's an example:
Sometimes you want to do some action A when C is true, but perform some different action B when C is false. The general form of this construct is:
As with the single-branch if, the condition C is evaluated and converted to Boolean. If the result is True, block A is executed; if False, block B is executed instead.
Some people prefer a more “horizontal” style of coding, where more items are put on the same line, so as to take up less vertical space. If you prefer, you can put one or more statements on the same line as the if or else, instead of placing them in an indented block. Use a semicolon “;” to separate multiple statements. For example, the above example could be expressed on only two lines:
Sometimes you want to execute only one out of three or four or more blocks, depending on several conditions. For this situation, Python allows you to have any number of “elif clauses” after an if, and before the else clause if there is one. Here is the most general form of a Python if statement:
So, in general, an if statement can have zero or more elif clauses, optionally followed by an else clause. Example:
6.3. A word about indenting your code
One of the most striking innovations of Python is the use of indentation to show the structure of the blocks of code, as in the if statement. Not everyone is thrilled by this feature. However, it is generally good practice to indent subsidiary clauses; it makes the code more readable. Those who argue that they should be allowed to violate this indenting practice are, in the author's opinion, arguing against what is generally regarded as a good practice.
The amount by which you indent each level is a matter of personal preference. You can use a tab character for each level of indention; tab stops are assumed to be every 8th character. Beware mixing tabs with spaces, however; the resulting errors can be difficult to diagnose.
6.4. The for statement: Looping
Use Python's “for” construct to do some repetitive operation for each member of a sequence. Here is the general form:
variable=sequence[0]
block
variable=sequence[1]
block
variable=sequence[−1]
block
• The sequence can be any expression that evaluates to a sequence value, such as a list or tuple. The range() function is often used here to generate a sequence of integers.
• For each value in the sequence in turn, the variable is set to that value, and the block is executed.
As with the if statement, the block consists of one or more statements, indented the same amount relative to the if keyword.
This example prints the cubes of all numbers from 1 through 5.
You may put the body of the loop—that is, the statements that will be executed once for each item in the sequence—on the same line as the “for” if you like. If there are multiple statements in the body, separate them with semicolons.
Here is an another example of iteration over a list of specific values.
6.5. The while statement
Use this statement when you want to perform a block B as long as a condition C is true:
Here is how a while statement is executed.
1. Evaluate C. If the result is true, go to step 2. If it is false, the loop is done, and control passes to the statement after the end of B.
2. Execute block B.
3. Go back to step 1.
Here is an example of a simple while loop.
This construct has the potential to turn into an infinite loop, that is, one that never terminates. Be sure that the body of the loop does something that will eventually make the loop terminate.
6.6. Special branch statements: break and continue
Sometimes you need to exit a for or while loop without waiting for the normal termination. There are two special Python branch statements that do this:
• If you execute a break statement anywhere inside a for or while loop, control passes out of the loop and on to the statement after the end of the loop.
• A continue statement inside a for loop transfers control back to the top of the loop, and the variable is set to the next value from the sequence if there is one. (If the loop was already using the last value of the sequence, the effect of continue is the same as break.) Here are examples of those statements.
In the example above, when the value of i reaches 15, which has a remainder of 0 when divided by 5, the break statement exits the loop.
7. How to write a self-executing Python script
So far we have used Python's conversational mode to demonstrate all the features. Now it's time to learn how to write a complete program.
Your program will live in a file called a script. To create your script, use your favorite text editor (emacs, vi, Notepad, whatever), and just type your Python statements into it.
How you make it executable depends on your operating system.
• On Windows platforms, be sure to give your script file a name that ends in “.py”. If Python is installed, double-clicking on any script with this ending will use Python to run the script.
• Under Linux and MacOS X, the first line of your script must look like this:
The pythonpath tells the operating system where to find Python. This path will usually be “/usr/local/bin/python”, but you can use the “which” shell command to find the path on your computer:
Here is a complete script, set up for a typical Linux installation. This script, powersof2, prints a table n -n showing the values of 2 and 2 for n in the range 1, 2, , 12.
8. def: Defining functions
You can define your own functions in Python with the def statement.
• Python functions can act like mathematical functions such as len(s), which computes the length of
s. In this example, values like s that are passed to the function are called parameters to the function.
• However, more generally, a Python function is just a container for some Python statements that do some task. A function can take any number of parameters, even zero.
Here is the general form of a Python function definition. It consists of a def statement, followed by an indented block called the body of the function.
The parameters that a function expects are called arguments inside the body of the function.
Here's an example of a function that takes no arguments at all, and does nothing but print some text.
To call this function:
• The name of the function is followed by a left parenthesis “(”, a list of zero or more parameter values separated by commas, then a right parenthesis “)”.
• The parameter values are substituted for the corresponding arguments to the function. The value of parameter param0 is substituted for argument arg0; param1 is substituted for arg1 ; and so forth.
Here's a simple example showing argument substitution.
8.1. return: Returning values from a function
So far we have seen some simple functions that take arguments or don't take arguments. How do we define functions like len() that return a value?
Anywhere in the body of your function, you can write a return statement that terminates execution of the function and returns to the statement where it was called.
Here is the general form of this statement:
The expression is evaluated, and its value is returned to the caller.
Here is an example of a function that returns a value:
In this case, the special placeholder value None is returned.
• If Python executes your function body and never encounters a return statement, the effect is the same as a return with no value: the special value None is returned.
Here is another example of a function that returns a value. This function computes the factorial of a positive integer:
The factorial of n, denoted n!, is defined as the product of all the integers from 1 to n inclusive.
For example, 4! = 1×2×3×4 = 24.
We can define the factorial function recursively like this:
• If n is 0 or 1, n! is 1.
• If n is greater than 1, n! = n × (n-1)!.
And here is a recursive Python function that computes the factorial, and a few examples of its use.
8.2. Function argument list features
The general form of a def shown in Section 8, “def: Defining functions” (p. 42) is over-simplified. In general, the argument list of a function is a sequence of four kinds of arguments:
1. If the argument is just a name, it is called a positional argument. There can be any number of positional arguments, including zero.
2. You can supply a default value for the argument by using the form “name=value”. Such arguments are called keyword arguments. See Section 8.3, “Keyword arguments” (p. 45).
A function can have any number of keyword arguments, including zero.
All keyword arguments must follow any positional arguments in the argument list.
3. Sometimes it is convenient to write a function that can accept any number of positional arguments. To do this, use an argument of this form:
A function may have only one such argument, and it must follow any positional or keyword arguments. For more information about this feature, see Section 8.4, “Extra positional arguments” (p. 46).
4. Sometimes it is also convenient to write a function that can accept any number of keyword arguments, not just the specific keyword arguments. To do this, use an argument of this form:
If a function has an argument of this form, it must be the last item in the argument list. For more information about this feature, see Section 8.5, “Extra keyword arguments” (p. 46).
8.3. Keyword arguments
If you want to make some of the arguments to your function optional, you must supply a default value. In the argument list, this looks like “name=value”.
Here's an example of a function with one argument that has a default value. If you call it with no arguments, the name mood has the string value 'bleah' inside the function. If you call it with an argument, the name mood has the value you supply.
If your function has multiple arguments, and the caller supplies multiple parameters, here is how they are matched up:
• The function call must supply at least as many parameters as the function has positional arguments.
• If the caller supplies more positional parameters than the function has positional arguments, parameters are matched with keyword arguments according to their position.
Here are some examples showing how this works.
Here is another feature: the caller of a function can supply what are called keyword parameters of the form “name=value”. If the function has an argument with a matching keyword, that argument will be set to value.
• If a function's caller supplies both positional and keyword parameters, all positional parameters must precede all keyword parameters.
• Keyword parameters may occur in any order.
Here are some examples of calling a function with keyword parameters.
8.4. Extra positional arguments
You can declare your function in such a way that it will accept any number of positional parameters. To do this, use an argument of the form “*name” in your argument list.
• If you use this special argument, it must follow all the positional and keyword arguments in the list.
• When the function is called, this name will be bound to a tuple containing any positional parameters that the caller supplied, over and above parameters that corresponded to other parameters.
Here is an example of such a function.
8.5. Extra keyword arguments
You can declare your function in such a way that it can accept any number of keyword parameters, in addition to any keyword arguments you declare.
To do this, place an argument of the form “**name” last in your argument list.
When the function is called, that name is bound to a dictionary that contains any keyword-type parameters that are passed in that have names that don't match your function's keyword-type arguments. In that dictionary, the keys are the names used by the caller, and the values are the values that the caller passed.
Here's an example.
>>> def k(p0, p1, nickname='Noman', *extras, **extraKeys):
print p0, p1, nickname, extras, extraKeys
>>> k(1,2,3)
1 2 3 () {}
>>> k(4,5)
4 5 Noman () {}
>>> k(6, 7, hobby='sleeping', nickname='Sleepy', hatColor='green')
6 7 Sleepy () {'hatColor': 'green', 'hobby': 'sleeping'}
>>> k(33, 44, 55, 66, 77, hometown='McDonald', eyes='purple')
33 44 55 (66, 77) {'hometown': 'McDonald', 'eyes': 'purple'} >>>
8.6. Documenting function interfaces
Python has a preferred way to document the purpose and usage of your functions. If the first line of a function body is a string constant, that string constant is saved along with the function as the documentation string. This string can be retrieved by using an expression of the form f.__doc__, where f is the function name.
Here's an example of a function with a documentation string.
9. Using Python modules
Once you start building programs that are more than a few lines long, it's critical to apply this overarching principle to programming design:
In other words, rather than build your program as one large blob of Python statements, divide it into logical pieces, and divide the pieces into smaller pieces, until the pieces are each small enough to understand.
Python has many tools to help you divide and conquer. In Section 8, “def: Defining functions” (p. 42), we learned how to package up a group of statements into a function, and how to call that function and retrieve the result.
Way back in Section 2.3, “More mathematical operations” (p. 8), we got our first look at another important tool, Python's module system. Python does not have a built-in function to compute square roots, but there is a built-in module called math that includes a function sqrt() that computes square roots.
In general, a module is a package of functions and variables that you can import and use in your programs. Python comes with a large variety of modules, and you can also create your own. Let's look at Python's module system in detail.
• In Section 9.1, “Importing items from modules” (p. 48), we learn to import items from existing modules.
• Section 9.2, “Import entire modules” (p. 49) shows another way to use items from modules.
• Section 9.4, “Build your own modules” (p. 51).
9.1. Importing items from modules
Back in Section 2.2, “The assignment statement” (p. 5), we learned that there is an area called the “global namespace,” where Python keeps the names and values of the variables you define.
The Python dir() function returns a list of all the names that are currently defined in the global namespace. Here is a conversational example; suppose you have just started up Python in conversational mode.
When Python starts up, three variables are always defined: __builtins__, __doc__, and __name__. These variables are for advanced work and needn't concern us now.
Note that when we define a variable (frankness), next time we call dir(), that name is in the resulting list. When we define a function (oi), its name is also added. Note also that you can use the type() function to find the type of any currently defined name: frankness has type float, and oi has type function.
Now let's see what happens when we import the contents of the math module into the global namespace:
>>> from math import *
>>> dir()
['__builtins__', '__doc__', '__name__', 'acos', 'asin', 'atan', 'atan2',
'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod',
'frankness', 'frexp', 'hypot', 'ldexp', 'log', 'log10', 'modf', 'oi', 'pi',
'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh']
>>> sqrt(64)
8.0
As you can see, the names we have defined (oi and frankness) are still there, but all of the variables and functions from the math module are now in the namespace, and we can use its functions and variables like sqrt() and pi.
In general, an import statement of this form copies all the functions and variables from the module into the current namespace:
where the keyword import is followed by a list of names, separated by commas.
Here's another example. Assume that you have just started a brand new Python session, and you want to import only the sqrt() function and the constant pi:
We didn't ask for the cos() function to be imported, so it is not part of the global namespace.
9.2. Import entire modules
Some modules have hundreds of different items in them. In cases like that, you might not want to clutter up your global namespace with all those items. There is another way to import a module. Here is the general form:
This statement adds only one name to the current namespace—the name of the module itself. You can then refer to any item inside that module using an expression of this form:
Here is an example, again using the built-in math module. Assume that you have just started up a new Python session and you have added nothing to the namespace yet.
As you can see, using this form of import adds only one name to the namespace, and that name has type module.
There is one more additional feature of import we should mention. If you want to import an entire module M1, but you want to refer to its contents using a different name M2, use a statement of this form:
You can apply Python's built-in dir() function to a module object to find out what names are defined inside it:
>>> import math
>>> dir()
['__builtins__', '__doc__', '__name__', 'math']
>>> dir(math)
['__doc__', '__file__', '__name__', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'cos', 'cosh', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp',
'hypot', 'ldexp', 'log', 'log10', 'modf', 'pi', 'pow', 'radians', 'sin',
'sinh', 'sqrt', 'tan', 'tanh']
>>>
9.3. A module is a namespace
Modules are yet another example of a Python namespace, just as we've discussed in Section 2.2, “The assignment statement” (p. 5) and Section 5.3, “A namespace is like a dictionary” (p. 33).
When you import a module using the form “import moduleName”, you can refer to some name N inside that module using the period operator: “moduleName.N”.
So, like any other namespace, a module is a container for a unique set of names, and the values to which each name is connected.
9.4. Build your own modules
If you have a common problem to solve, chances are very good that there are modules already written that will reduce the amount of code you have to write.
8
• Python comes with a large collection of built-in modules. See the .
• The site also hosts a collection of thousands of third-party modules: see the
9 .
You can also build your own modules. A module is similar to a script (see Section 7, “How to write a self-executing Python script” (p. 41)): it is basically a text file containing the definitions of Python functions and variables.
To build your own module, use a common text editor to create a file with a name of the form “moduleName.py”. The moduleName you choose must be a valid Python name—it must start with a letter or underbar, and consist entirely of letters, underbars, and digits.
Inside that file, place Python function definitions and ordinary assignment statements.
Here is a very simple module containing one function and one variable. It lives in a file named .
There is one more refinement we suggest for documenting the contents of a module. If the first line of the module's file is a string constant, it is saved as the module's “documentation string.” If you later import such a module using the form “import moduleName”, you can retrieve the contents of the documentation string using the expression “moduleName.__doc__”.
8
9
Here is an expanded version of our with a documentation string:
10. Input and output
Python makes it easy to read and write files. To work with a file, you must first open it using the builtin open() function. If you are going to read the file, use the form “open(filename)”, which returns a file object. Once you have a file object, you can use a variety of methods to perform operations on the file.
10.1. Reading files
For example, for a file object F, the method F.readline() attempts to read and return the next line from that file. If there are no lines remaining, it returns an empty string.
Let's start with a small text file named trees containing just three lines:
Note that the newline characters ('\n') are included in the return value. You can use the string .rstrip() method to remove trailing newlines, but beware: it also removes any other trailing whitespace.
To read all the lines in a file at once, use the .readlines() method. This returns a list whose elements are strings, one per line.
A more general method for reading files is the .read() method. Used without any arguments, it reads the entire file and returns it to you as one string.
To read exactly N characters from a file F, use the method F.read(N). If N characters remain in the file, you will get them back as an N-character string. If fewer than N characters remain, you will get the remaining characters in the file (if any).
As with the .readline() method, when you iterate over the lines of a file in this way, the lines will contain the newline characters. If the above example did not trim these lines with .rstrip(), each line of output would be followed by a blank line, because the print statement adds a newline.
10.2. File positioning for random-access devices
For random-access devices such as disk files, there are methods that let you find your current position within a file, and move to a different position.
• F.tell() returns your current position in file F.
• F.seek(N) moves your current position to N, where a position of zero is the beginning of the file.
• F.seek(N, 1) moves your current position by a distance of N characters, where positive values of N move toward the end of the file and negative values move toward the beginning.
For example, F.seek(80, 1) would move the file position 80 characters further from the start of the file.
• F.seek(N, 2) moves to a position N characters relative to the end of the file. For example, F.seek(0, 2) would move to the end of the file; F.seek(-200, 2) would move your position to 200 bytes before the end of the file.
10.3. Writing files
To create a disk file, open the file using a statement of this general form:
The second argument, "w", specifies write access. If possible, Python will create a new, empty file by that name. If there is an existing file by that name, and if you have write permission to it, the existing file will be deleted.
To write some content to the file you are creating, use this method:
Note that you must explicitly provide newline characters in the arguments to .write().
11. Introduction to object-oriented programming
So far we have used a number of Python's built-in types such as int, float, list, and file.
Now it is time to begin exploring some of the more serious power of Python: the ability to create your own types.
This is a big step, so let's start by reviewing some of the historical development of computer language features.
11.1. A brief history of snail racing technology
An entrepreneur name Malek Ology would like to develop a service to run snail races to help non-profit organizations raise funds. Here is the proposed design for Malek's snail-racing track:
At the start of the race, the snails, with their names written on their backs in organic, biodegradable ink, are placed inside the starting line, and Malek starts a timer. As each snail crosses the finish line, Malek records their times.
Malek wants to write a Python program to print the race results. We'll look at the evolution of such a program through the history of programming. Let's start around 1960.
11.2. Scalar variables
Back around 1960, the hot language was FORTRAN. A lot of the work in this language was done using scalar variables, that is, a set of variable names, each of which held one number.
Suppose we've just had a snail race, and Judy finished in 87.3 minutes, while Kyle finished in 96.6 minutes. We can create Python variables with those values like this:
If Judy and Kyle are the only two snails, this program will work fine. Malek puts this all into a script. After each race, he changes the first two lines that give the finish times, and then runs the script.
This will work, but there are a number of objections:
• The person who prepares the race results has to know Python so they can edit the script.
• It doesn't really save any time. Any second-grader can look at the times and figure out who won.
• The names of the snails are part of the program, so if different snails are used, we have to write a new program.
• What if there are three snails? There are a lot more cases: three cases where a snail clearly wins; three more possible two-way ties; and a three-way tie. What if Malek wants to race ten snails at once? Too complicated!
11.3. Snail-oriented data structures: Lists
Let's consider the general problem of a race involving any number of snails. Malek is considering diversifying into amoeba racing, so there might be thousands of competitors in a race. So let's not limit the number of competitors in the program.
Also, to make it possible to use cheaper labor for production runs, let's write a general-purpose script that will read a file with the results for each race, so a relatively less skilled person can prepare that file, and then run a script that will review the results.
We'll use a very simple text file format to encode the race results. Here's an example file for that first race between Judy and Kyle:
And here is a script that will process that file and report on the winning time. The script is called . First, reads a race results file named results and stores the times into a list. The .split() method is used to break each line into parts, with the first part containing the elapsed time.
At this point, timeList is a list of float values. We use the .sort() method to sort the list into ascending order, so that the winning time will be in the first element.
Try building the results file and the script yourself to verify that they work. Try some cases where there are ties.
This script is fine as far as it goes. However, there is one major drawback: it doesn't tell you who won!
11.4. Snail-oriented data structures: A list of tuples
To improve on the script above, let's modify the script so that it keeps each snail's time and name together in a two-element tuple such as (87.3, 'Judy').
In the improved script, the timeList list is a list of these tuples, and not just a list of times. We can then sort this list, using an interesting property of tuples. If you compare two tuples, and their first elements are not equal, the result is the same as if you compared their first elements. However, if the first elements are equal, Python then compares the second elements of each tuple, and so on until it either finds two unequal values, or finds that all the elements are equal.
Here's an example. Recall that the function cmp(a, b), function compares two arbitrary values and returns a negative number if a comes before b, or a positive number if a comes after b, or zero if they are considered equal:
If you compare two tuples and the first elements are unequal, the result is the same as if you compared the first two elements. For example:
If, however, the first elements are equal, Python then compares the second elements, or the third elements, until it either finds two unequal elements, or finds that all the elements are equal:
Let's try a larger results file with some names that have spaces in them, just to exercise the script. Here's the input file:
11.5. Abstract data types
The preceding section shows how you can use a Python tuple to combine two simple values into a compound value. In this case, we use a 2-element tuple whose first element is the snail's time and the second element is its name.
We might say that this tuple is an abstract data type, that is, a way of combining Python's basic types (such as floats and strings) into new combinations.
The next step is to combine values and functions into an abstract data type. Historically, this is how objectoriented programming arose. The “objects” are packages containing simpler values inside them. However, in general, these packages can also contain functions.
Before we start looking at how we build abstract data types in Python, let's define some import terms and look at some real-world examples.
class
When we try to represent in our program some items out in the real world, we first look to see which items are similar, and group them into classes. A class is defined by one or more things that share the same qualities.
For example, we could define the class of fountains by saying that they are all permanent man-made structures, that they hold water, that they are outdoors in a public place, and that they keep the water in a decorative way.
It should be easy to determine whether any item is a member of the class or not, by applying these defining rules. For example, Trevi Fountain in Rome fits all the rules: it is man-made, holds water, is outdoors, and is decorative. Lake Geneva has water spraying out of it, but it's not man-made, so it's not a fountain. instance
One of the members of a class. For example, the class of airplanes includes the Wright Biplane of 1903, and the Spirit of St. Louis that Charles Lindbergh flew across the Atlantic.
An instance is always a single item. “Boeing 747” is not an instance, it is a class. However, a specific Boeing 747, with a unique tail number like N1701, is an instance.
attribute
Since the purpose of most computer applications is in record-keeping, within a program, we must often track specific qualities of an instance, which we call attributes.
For example, attributes of an airplane include its wingspan, its manufacturer, and its current location, direction, and airspeed.
We can classify attributes into static and dynamic attributes, depending on whether they change or not. For example, the wingspan and model number of an airplane do not change, but its location and velocity can.
operations
Each class has characteristic operations that can be performed on instances of the class. For example, operations on airplanes include: manufacture; paint; take off; change course; land.
Here is a chart showing some classes, instances, attributes, and operations.
11.6. Abstract data types in Python
We saw how you can use a two-element tuple to group a snail's time and name together. However, in the real world, we might need to track more than two attributes of an instance.
Suppose Malek wants to keep track of more attributes of a snail, such as its age in days, its weight in grams, its length in millimeters, and its color. We could use a six-element tuple like this:
The problem with this approach is that we have to remember that for a tuple T, the time is in T[0], the name in T[1], the age in T[2], and so on.
A cleaner, more natural way to keep track of attributes is to give them names. We might encode those six attributes in a Python dictionary like this:
T = { 'time':87.3, 'name':'Judy', 'age':34, 'mass':1.66,
'length':39, 'color':'tan'}
With this approach, we can retrieve the name as T['name] or the weight as T['mass']. However, now we have lost the ability to put several of these dictionaries into a list and sort the list—how is Python supposed to know which dictionary comes first? What we need is something like a dictionary, but with more features. What we need is Python's object-oriented features.
Now we're to look at actual Python classes and instances in action.
11.7. class SnailRun: A very small example class
Let's start building a snail-racing application for Malek the object-oriented Python way. Let's assume that all we're tracking about a particular snail is its name and its finishing time. We need to define a class named SnailRun, whose instances track just these two attributes.
Here is the general form of a class declaration in Python:
A class declaration starts out with the keyword class, followed by the name of the class you are defining, then a colon (:). The methods of the class follow; each method starts with “def”, just as you use to define a function.
Before we look at the construction of the class, let's see how it works in practice. To create an instance in Python, you use the name of the class as if it were a function call, followed by a list of arguments in parentheses. Our SnailRun constructor method will need two arguments: the snail's name and its finish time. Once we have defined the class, we can build a new instance like this:
To get the snail's name and time attributes from an instance, we use the instance name, followed by a dot (.), followed by the attribute name:
Our example class, SnailRun, will have just two methods:
• All classes have a constructor method named “__init__”. This method is used to create a new instance.
• We'll write a .show() method to format the contents of the instance for display.
Continuing our example from above, here's an example of the use of the .show() method:
Instantiation means the construction of a new instance. Here is how instantiation works.
1. Somewhere in a Python program, the programmer starts the construction of a new instance by using the class's name followed by parentheses and a list of arguments. Let's call the arguments (a1, a2, ).
2. Python creates a new namespace that will hold the instance's attributes. Inside the constructor, this namespace is referred to as self.
Important
The instance is basically a namespace, that is, a container for attribute names and their definitions. For other examples of Python namespaces, see Section 2.2, “The assignment statement” (p. 5), Section 5.3, “A namespace is like a dictionary” (p. 33), and Section 9.3, “A module is a namespace” (p. 51).
3. The __init__() (constructor) method of the class is executed with the argument list (self, a1, a2, ).
Note that if the constructor takes N arguments, the caller passes only the last N-1 arguments to it.
4. When the constructor method finishes, the instance is returned to the caller. From then one, the caller can refer to some attribute A of the instance I as “A.I”.
Let's look again in more detail at the constructor:
All the constructor does is to take the snail's name and finish time and store these values in the instance's namespace under the names .name and .time, respectively.
Note that the constructor method does not (and cannot) include a return statement. The value of self is implicitly returned to the statement that called the constructor.
As for the other methods of a class, their definitions also start with the special argument self that contains the instance namespace. For any method that takes N arguments, the caller passes only the last N-1 arguments to it.
In our example class, the def for the .show() method has one argument named self, but the caller invokes it with no arguments at all:
11.8. Life cycle of an instance
To really understand what is going on inside a running Python program, let's follow the creation of an instance of the SnailRun class from the preceding section.
Just for review, let's assume you are using conversational mode, and you create a variable like this:
Whenever you start up Python, it creates the “global namespace” to hold the names and values you define. After the statement above, here's how it looks.
Global namespace Name Value
Next, suppose you type in the class definition as above. As it happens, a class is a namespace too—it is a container for methods. So the global namespace now has two names in it: the variable badPi and the class SnailRun. Here is a picture of the world after you define the class:
Global namespace class SnailRun
Next, create an instance of class SnailRun like this:
Here is the sequence of operations:
1. Python creates a new instance namespace. This namespace is initially a copy of the class's namespace: it contains the two methods .__init__() and .show().
2. The constructor method starts execution with these arguments:
• The name self is bound to the instance namespace.
• The name snailName is bound to the string value 'Judy'.
• The name finishTime is bound to the float value 87.3.
3. This statement in the constructor
creates a new attribute .name in the instance namespace, and assigns it the value 'Judy'.
4. The next statement in the constructor creates an attribute named .time in the instance namespace, and binds it to the value 87.3.
5. The constructor completes, and back in conversational mode, in the global namespace, variable j1 is bound to the instance namespace.
Here's a picture of the world after all this:
11.9. Special methods: Sorting snail race data
Certain method names have special meaning to Python; each of these special method names starts with two underbars, “__”.
A class's constructor method, __init__(), is an example of a special method. Whenever you use the class's name as if it were a function, in an expression like “SnailRun('Judy', 67.3)”, Python executes the constructor method to build the new instance.
10
There is a full list of all the Python special method names in the . Next we will look at another special method, __cmp__, that Python calls whenever you compare two instances of that class.
Going back to our snail-racing application, an instance of the SnailRun class contains everything we need to know about one snail's performance: its name in the .name attribute and its finish time in the .time attribute.
However, using the tuple representation back in Section 11.4, “Snail-oriented data structures: A list of tuples” (p. 58), we were able to put a collection of these tuples into a list, and sort the list so that they were ordered by finish time, with the winner first. Let's see what we need to add to class SnailRun so that we can sort a list of them into finish order by calling the .sort() method on the list.
First, a bit of review. Back in Section 6.1, “Conditions and the bool type” (p. 34), we learned about the built-in Python function cmp(x, y), which returns:
• a negative number if x is less than y; • a positive number if x is greater than y; or
• zero if x equals y.
In a Python class, if you define a method named “__cmp__”, that method is called whenever Python compares two instances of the class. It must return a result using the same conventions as the built-in cmp() function: negative for “<”, zero for “==”, positive for “>”.
10
In the case of “class SnailRun”, we want the snail with the better finishing time to be considered less than the slower snail. So here is one way to define the __cmp__ method for our class:
When this method is called, self is an instance of class SnailRun, and other should also be an instance of SnailRun.
However, this logic exactly duplicates what the built-in cmp() function does to compare two float values, so we can simplify it like this:
Let's look at another special method, __str__(). This one defines how Python converts an instance of a class into a string. It is called, for example, when you name an instance in a print statement, or when you pass an instance to Python's built-in str() function.
The __str__() method of a class returns a string value. It is up to the writer of the class what string value gets returned. As usual for Python methods, the self argument contains the instance. In the case of class SnailRun, we'll want to display the snail's name (.name attribute) and finishing time (.time attribute). Here's one possible version:
This method will format the finishing time into an 8-character string, with one digit after the decimal point, followed by one space, then the snail's name.
Let's assume that the __cmp__() and __str__() methods have been added to our module, and show their use in some conversational examples.
Now that we have two SnailRun instances, we can show how the __str__() method formats them for printing:
We can also show the various ways that Python compares two instances using our new __cmp__() method.
Now that we have defined how instances are to be ordered, we can sort a list of them in order by finish time. First we throw them into the list in any old order:
The .sort() method on a list uses Python's cmp() function to compare items when sorting them, and this in turn will call our class's __cmp__() method to sort them by finishing time.
For an extended example of a class that implements a number of special methods, see
11
. This example shows how to define a new kind of numbers, and specify how operators such as “+” and “*” operate on instances.
11