Upload
warwithin
View
220
Download
0
Embed Size (px)
Citation preview
8/14/2019 PythonIntro0821
1/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 1
Python in 3 Hours
Steve HoldenHolden Web LLC
OSCON
Portland, OR
July 21, 2008
8/14/2019 PythonIntro0821
2/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 2
What Will You Achieve?
Reading/writing familiarity with Python
Understanding of the basic data types andoperations
Appreciation of object-oriented programming Insight into various standard library modules
The ability to keep code appropriately simple
Some understanding of how the interpreter works
Underneath the hood
8/14/2019 PythonIntro0821
3/181
8/14/2019 PythonIntro0821
4/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 4
How This Class Works
The material is for programmers If you are new to programming, also look at
http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python
An immersion approach
Designed to help you understand how Python works
Dont just sit there and soak it up!
Try the interactive interpreter! Python users are friendly: ask them for help
Mailing lists, newsgroups, conferences etc.
8/14/2019 PythonIntro0821
5/181
8/14/2019 PythonIntro0821
6/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 6
Other Python Information (1)
www.python.org has an extensive reading list docs.python.org documents the current
release
Dive Into Python immersive text, also in print http://www.diveintopython.org/
Comp.lang.python newsgroup
Also available as a mailing listpython-list
Subscribe athttp://mail.python.org/mailman/listinfo
8/14/2019 PythonIntro0821
7/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 7
Other Python Information (2)
Python in a Nutshell Alex Martelli (OReilly) ActiveStates Python Cookbook:
http://aspn.activestate.com/ASPN/Python/Cookbook/
Many other useful texts Depending on personal taste
Google is your friend
There is a huge amount of information about Python onthe web
8/14/2019 PythonIntro0821
8/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 8
Chapter 1
Introduction to Python
8/14/2019 PythonIntro0821
9/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 9
Python Characteristics (1)
Simple Interpreted
Interactive
Can test ideas immediately, verify learning points
Dynamically typed
Variables (names) don't have types, values do
Object-Oriented
Multi-Platform
8/14/2019 PythonIntro0821
10/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 10
Python Characteristics (2)
Clean syntax with indented block structure C and C++ programmers tend to prefer braces
Though they indent their code just the same!
Wide range of built-in functions and methods Batteries included
Extensive standard libraries for many applications
Good, extensible set of data types
Integer, float, complex, string, list, tuple, dict, ...
8/14/2019 PythonIntro0821
11/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 11
Interactive Characteristics
Python runs interactively when no filename given Expression values are printed out
Statements are executed
Reads one line at a time, prompting the user
>>>: interpreter expects an expression or statement
... : expression or statement needs more input
Very useful for trying things out
8/14/2019 PythonIntro0821
12/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 12
An Interactive Python Session
$ pythonPython 2.5.1 (r251:54863, May 18 2007, 16:56:43)[GCC 3.4.4 (cygming special, gdc 0.12, using dmd 0.125)] on cygwinType "help", "copyright", "credits" or "license" for moreinformation.>>> import os>>> len(os.listdir('.'))
504>>> 53+121.2174.19999999999999>>> 53+121.2174.19999999999999>>> d ={1: "one", 2: "two"}>>> d[2]
'two'>>> d["large"] = 10000000.0>>> len(d... )3>>>
8/14/2019 PythonIntro0821
13/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 13
Pythons Spaces (1)
Objects are allocated from a heap (object space) Namespaces hold names, which refer to objects
>>> s = This is a string # Pound sign introduces comment>>> s1 = Another string # and are really '' and "">>> s + + s1This is a string Another string
Garbage(space can
be collectedfor re-use)
8/14/2019 PythonIntro0821
14/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 14
Pythons Spaces (2)
Eachfunction callhas a local namespace Each module has a global namespace
There is a built-in namespace
Contains built-in functions
Classes and instances each have a namespace
Memory allocation and deallocation is automatic
Bounds are always checked
8/14/2019 PythonIntro0821
15/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 15
Basic Syntax
Each line (unless continued) is a single statement A line is continued if it ends with a backslash
A line is also continued if any brace, bracket or
parenthesis is unmatched at the end of the line Multiple statements on a single line can be
separated by semicolons
Leading spaces are significant! Statements at the same indentation level form a block
orsuite
8/14/2019 PythonIntro0821
16/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 16
Chapter 2
Data Types, Values and
Literals
8/14/2019 PythonIntro0821
17/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 17
Names and Assignment
As in most programming languages the equals signindicates an assignment
Python programmers tend to talk about bindingnames rather than assigning to variables
Names consist of letters, digits and underscores
First character may not be a digit
8/14/2019 PythonIntro0821
18/181
8/14/2019 PythonIntro0821
19/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 19
Numeric Data Types
Integer: 10 In Python 2.5 and up the integers are unbounded
Float: 23.456 or3.14159E19
Complex: 3+4j Contains a real part and an imaginary part
When combined in arithmetic operations theinterpreter widens to make types compatible
>>> 1 + 2.3 + (5 + 6j)
(8.3000000000000007+6j)
8/14/2019 PythonIntro0821
20/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 20
Arithmetic Operations
All the usual suspects are present: + - * / etc. Exponentiation is **
Can also do bitwise not, and, or, xor (~, &, |, ^)
on integer values, and shift them () % returns remainder after division
always returns a value with same sign as divisor:
>>> -7 % 3
2
// is an explicit integer division operator
8/14/2019 PythonIntro0821
21/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 21
Comparisons
Numeric values of all types can be mixed Widening is used before comparison
Again all the standard operators are available
>, >=, = =,>> 7 > 5 > 12False>>> 7 > 5 > 3True>>>
8/14/2019 PythonIntro0821
22/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 22
Truthiness In Python
Python has a flexible idea of truth and falsity* not, and, or
andand or are short-circuiting
They dont evaluate unnecessary operands andreturns its left-hand operand if false
otherwise its right-hand operand
or returns its left-hand operand if true
otherwise its right-hand operand
The right-hand operand may not be evaluated
* As described later
8/14/2019 PythonIntro0821
23/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 23
Sequence Types
Sequences allow individual elements to beindividually addressed by index value
The most natural types to iterate over
The simplest Python sequence is the string
Single characters are just strings of length 1
Lists are changeable sequences
Elements are usually of the same type
Tuples contain immutable sequences
Elements are frequently of different types
8/14/2019 PythonIntro0821
24/181
What Do You Need to KnowToday?
2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 24
Sequence Handling
The following rules apply to all sequence types Indexing is indicated by square brackets
Can access individual elements s[i]
The first element in a sequence s is s[0] Negative indexes start from the right
Can extract a subsequence usingslicing: s[m:n]
Think of indexes as numbering the gaps:
8/14/2019 PythonIntro0821
25/181
What Do You Need to KnowToday? 2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 25
Python String Literals
Rich set of string literal representations Simple literals can use single- or double-quotes as
the opening and closing quotes
These simple literals cannot span line boundaries More complex values can use three quotes as
delimiter
These literals can span line boundaries The newlines are just additional characters in the string
Escape them with a backslash at the end of the lin
8/14/2019 PythonIntro0821
26/181
What Do You Need to KnowToday? 2008 Holden Web: Reproduction prohibited without writtenpermission 080721 Python in 3 Hours 26
String Literal Escapes*
Hexadecimal character value\xnn
Octal character value\0nn
Ignored allows continuation of triple-
quoted strings
\{newline}
Line feed\n
Carriage return\r
Backslash\\
Single-quote (apostrophe)\
Double-quote\
* Only the most frequently-used escape sequences are shown in this table
Certain characters need special representations:
8/14/2019 PythonIntro0821
27/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 27
String Handling Examples
A slice s[m:n] will (normally) be of length n-mIndexing past the end of a string raises an exceptionSlices, however, can reference non-existent positions
>>> s4 = "Nineteen characters">>> s4[-1]'s'>>> s4[-19]'N'>>> s4[0:19] # a nineteen-character slice'Nineteen characters'
>>> s4[:] # omitted index means start or end 'Nineteen characters'>>> s4[:-1] # includes all but last item
'Nineteen character'>>> s4[1:] # includes all but first item 'ineteen characters'
8/14/2019 PythonIntro0821
28/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 28
Unicode Strings
Unicode literals are introduced by U or u uThis is Unicode
When Unicode and regular strings are mixed the
result is Unicode
8/14/2019 PythonIntro0821
29/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 29
Raw String Literals
Useful when strings contain backslashes Introduced by R or r
rThis contains \r as two characters
Need to use care when reading interpreter output!>>> "\r"'\r'>>> r"\r"'\\r'>>> print r"\r"\r>>>
8/14/2019 PythonIntro0821
30/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 30
Functions and Methods
Python objects have methods, which you can calllike functions
Available methods depends on the type of object
How do you call an objects methods? follow a reference to the object with a dot and the
method name
>>> s = This is a string
>>> s.upper()THIS IS A STRING>>> s.capitalize()This Is A String>>>
8/14/2019 PythonIntro0821
31/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 31
String Functions and Methods (1)
Python includes functions that operate on stringsand characters:
chr(n): the character whose code value is n
ord(c): the code value of characterclen(s): the number of characters in strings Strings also have methods you can use to operate
on them
Since strings are immutable* the methods return newstrings
* Immutable: cannot be modified
8/14/2019 PythonIntro0821
32/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 32
Common String Methods
Returns a list of substrings ofs
(various argument combinations are
possible)
s.split()
Returns a copy ofs with the firstmax occurrences ofoldreplaced by
new
s.replace(
old, new[, max])
Returns true if strings begins (or
ends) with the stringx
s.startswith(x)
s.endswith(x)
Returns a copy ofs with bothleading and trailing whitespace
characters removed
s.strip()
Returns a copy ofs with all letters in
upper case
s.upper()
Returns a copy ofs with all letters inlower case
s.lower()
8/14/2019 PythonIntro0821
33/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 33
Lists and Tuples
Handled similarly to strings Same indexing rules, including slicing
Tuples are indicated by comma separation
Parentheses are often optional but usually present
Lists are introduced by brackets: [ ]
Elements of either structure can be ofany type
Including other lists and tuples
Allows for arbitrarily nested structures in source code
8/14/2019 PythonIntro0821
34/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 34
Some Lists and Tuples
>>> l1 = [1, 4, 'nine', 'sixteen', 25]
>>> l1[0]1>>> l1[-1]25>>> t1 = 'one', 'four', 'nine', 16, 'twenty-five'>>> t1[0]'one'>>> t1[4]'twenty-five'>>> t1[2][2]'n>>> l2 = ['*', ['/', 32, 4], ['+', 3, 10]]>>> l2[1]
['/', 32, 4]>>> l2[1][0]'/'
8/14/2019 PythonIntro0821
35/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 35
Why Lists and Tuples?
Tuples are immutable (like strings) Dictionary keys (see later) mustbe immutable
Note that a list and a tuple can never be equal
Even when they have exactly the same elements
Interpreter does not widen as it does for numbers
Its the way things are! Dont get hung up on it
8/14/2019 PythonIntro0821
36/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 36
List Methods
Sorts the items ofL in placeL.sort([f])Reverses the order of the items inLL.reverse()Returns the value ofL[i] andremoves it
L.pop([i])
Removes the first occurrence ofx
fromL
L.remove(x)
Inserts itemx at index i inLL.insert(i,x)
Appends all items of list lto the endofL
L.extend(l)
Appends the itemx to the end ofLL.append(x)Returns index of the first occurrence
ofx
L.index(x)Returns the number of occurrences ofx
L.count(x)
8/14/2019 PythonIntro0821
37/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 37
Mapping Types: the Dict
The dict (dictionary) is Pythons only built-inmapping type
Can indexed by arbitrary* keys
Dict literals are a comma-separated list ofkey:value pairs surrounded by braces:
{one: 1, two: 2}
Both keys and values can be arbitrarily complex
Numeric keys of different types will compare equal
* There are some limitations: technically indexes must behashable
8/14/2019 PythonIntro0821
38/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 38
Simple Dict Operations
>>> d1 = {'one': 1, 'two': 2,'three': 3}>>> d1["one"]1>>> d1["non-existent"]
KeyError: non-existent>>> d1[(1, 2, 3)] = "tuple">>> d1[1] = "integer">>> d1[1, 2, 3]'tuple'
>>> d1[1.0+0.0j]'integer'
8/14/2019 PythonIntro0821
39/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 39
Files in Python
Files are just sequences of bytes Unicode decoding must be specified if required
Three files are available by default in the sys
module sys.stdin: standard input
sys.stdout: standard output
sys.stderr: standard error These will be the terminal by default, but you can
alter them with command-line options
8/14/2019 PythonIntro0821
40/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 40
The None Type
This type has only one value! Always referred to asNone
Often used as a sentinel or null indicator
The only value not printed as an expression valueby the interactive interpreter:
>>> None
>>>
Bi di A i t
8/14/2019 PythonIntro0821
41/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 41
Binding vs. Assignment
Python is unlike many other languages Other languages require declaration of the type
associated with a name (the name of a variable)
The variable is a fixed portion of memory, sized tohold a value of the declared type
Python does not associate specific areas ofmemory to the values for particular names
So we talk about binding values to namesinstead ofassigning values to variables
U ki A i t
8/14/2019 PythonIntro0821
42/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 42
Unpacking Assignments
The elements of a sequence can be bound toindividual variables in a single statement
Use a comma-separated list of names on the lefthand side:
a, b, c = (1, 2, 3) This even extends to nested structures:a, (b, c) = (1, (2, 3))
A i t t C t i El t
8/14/2019 PythonIntro0821
43/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 43
>>> l2
['*', ['/', 32, 4], ['+', 3, 10]]>>> l2[1] = 8 # Bind list element to value>>> l2['*', 8, ['+', 3, 10]]>>> d1{'one': 1, 'three': 3, 'two': 2}>>> d1["one"] = "Have a banana
# Bind new dictionary key to value>>> d1{'one': 'Have a banana', 'two': 2, 'three': 3}
Assignment to Container Elements
Left-hand side of an assignment can nominate anelement of a list or dict
This causes update in-place
Wh t A i t R ll D
8/14/2019 PythonIntro0821
44/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 44
What Assignment Really Does
>>> d2 = d1>>> d2{'one': 'Have a banana', 'two': 2, 'three': 3}
>>> d2["one"] = "Zipfile">>> d1{'one': 'Zipfile', 'two': 2, 'three': 3}
Elements are Also References
8/14/2019 PythonIntro0821
45/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 45
Elements are Also References
Container elements are also references to values Not always obvious
Dereferencing is automatic, as we usually want values
Remember: the value provides the type
Assignment to Slices (1)
8/14/2019 PythonIntro0821
46/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 46
Assignment to Slices (1)
Can assign to slices of a list>>> l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> l[2:4][2, 3]>>> l[2:4] = ["two", "three"]>>> l
[0, 1, 'two', 'three', 4, 5, 6, 7, 8, 9]>>> l[2:3] = [7, 7, 7, 7, 7]>>> l[0, 1, 7, 7, 7, 7, 7, 'three', 4, 5, 6, 7, 8, 9]>>> l[14:14][]>>> l[14:14] = ["extra", "elements"]
>>> l[0, 1, 7, 7, 7, 7, 7, 'three', 4, 5, 6, 7, 8, 9, 'extra','elements']
Assignment to Slices (2)
8/14/2019 PythonIntro0821
47/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 47
Assignment to Slices (2)
Assigned value mustalso be a list
Assigning an empty list deletes elements *
* But see the del statement for a better way
>>> l[5:16] = "Wrong"TypeError: must assign list (not "string") to slice>>> l[6:16] = ["Right"]>>> l[0, 1, 7, 7, 7, 7, 'Right']
>>> l[2:4] = []
>>> l[0, 1, 7, 7, 'Right']
Augmented Assignments(1)
8/14/2019 PythonIntro0821
48/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 48
Augmented Assignments(1)
Most operators have an augmented form Specifies perform operation then assign
Care is needed until you know what you are doing
Sometimes these operations update in place So other references to same object also see value
change
Sometimes they create a new value In which case the target is re-bound
Augmented Assignments(2)
8/14/2019 PythonIntro0821
49/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 49
Augmented Assignments(2)
In-place modification:lst = [thing1, thing2]lst += [thing3]
The list is extended by the addition of an element
After first statement After second statement
Augmented Assignments(3)
8/14/2019 PythonIntro0821
50/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 50
Augmented Assignments(3)
Rebinding:x = 1x += 3
Integers are immutable, so the name is rebound to the new value
After first statement After second statement
Common List and Tuple Operations
8/14/2019 PythonIntro0821
51/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 51
Common List and Tuple Operations
Can add two lists/tuples together:>>> [1, 2, 3] + [4, 5, 6][1, 2, 3, 4, 5, 6]
Can multiply them by an integer:
>>> ("a", "b") * 3('a', 'b', 'a', 'b', 'a', 'b')
Can test for membership:>>> "dick" in ("tom", "dick", "harry")
True
8/14/2019 PythonIntro0821
52/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 52
Chapter 3
Python Statements
Simple Python Statements (1)
8/14/2019 PythonIntro0821
53/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 53
Simple Python Statements (1)
Assignment (already covered)pass simply does nothing
del removes a reference from a namespace orcontainer object
break and continue are used in looping
return returns a value from a function
global identifies a name as global to the module
exec executes a Python string
Use only as a last resort
Simple Python Statements (2)
8/14/2019 PythonIntro0821
54/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 54
Simple Python Statements (2)
print sends (string representations of) a comma-separated list of values to the output stream
Inserts a space between each value
Puts a newline at the end
Unless the statement has a trailing comma
raise raises an exception
Can be used on its own to re-raise an existingexception
Simple Python Statements (3)
8/14/2019 PythonIntro0821
55/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 55
Simple Python Statements (3)
import makes external code available Discussed in detail in Chapter 5
Python comes with an extensive library
Compound Statements
8/14/2019 PythonIntro0821
56/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 56
Compound Statements
Pythons block structuring is different from mostlanguages
Indentation is the block structure
This is a contentious feature for some Newcomers from C and C++ often dislike it
Dont criticize until you get used to it!
No more braces and indentation dont match issues!
Conditional Execution (1)
8/14/2019 PythonIntro0821
57/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 57
Conditional Execution (1)
if statement allows code guarded by conditions
If one statement is guarded, it can go on the sameline
Unless it too is a complex statement
* This is considered poor style in finished code
>>> if not mylst:... print "List is empty"...List is empty
>>> if not mylst: print "List is empty"...List is empty
Conditional Execution (2)
8/14/2019 PythonIntro0821
58/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 58
Conditional Execution (2)
Can specify a second alternative with else
One-line forms are considered bad style
Reduces readability for little advantage But it can help to get code into a single slide
>>> mylst = [1, 2, 3, "bananas"]>>> if not mylst: print "List is empty"... else: print "List contents:", mylst
...List contents: [1, 2, 3, 'bananas']
Conditional Execution (3)
8/14/2019 PythonIntro0821
59/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 59
Co d t o a ecut o (3)
Python has no case orswitch statement It has been seriously discussed, and rejected
elseifcan be elided into elif
Reduces nesting levels
if this:
else if that:
else if the other:
and so on
if this:
elif that:
elif the other:
and so on
Whats True in Python?
8/14/2019 PythonIntro0821
60/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 60
y
The result of comparisons is True orFalse Any object can be used as a truth value
Most values will be treated as true in decisions
The following specific values are treated as false Any numeric zero 0, 0.0, 0+0j
Any empty container [], (), {}
None False
Others a little too complex to discuss here
For Loops
8/14/2019 PythonIntro0821
61/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 61
p
Used to iterate over sequences
Binds successive elements to the control variable(s)
Note that there is no special naming scope here
>>> for w in ["this", "is", "a",
... "python", "program"]:... print w
...thisis
apythonprogram
So We Dont Index the Elements?
8/14/2019 PythonIntro0821
62/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 62
Most often you just dont need them
The for iteration give you the elements directly!
The enumerate function provides (index, value) tuples For loops allow unpacking, so each element gets its own name
>>> for i, w in enumerate(... ["this", "is", "a", "python", "program"]):... print i, w...0 this1 is
2 a3 python4 program
While Loops
8/14/2019 PythonIntro0821
63/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 63
p
Iterate until some condition becomes false
>>> lst = ["this", "is", "a",... "python", "program"]>>> while lst: # false for empty list... print lst
... del lst[0]...['this', 'is', 'a', 'python', 'program']['is', 'a', 'python', 'program']['a', 'python', 'program']['python', 'program']['program']
8/14/2019 PythonIntro0821
64/181
Early Termination of an Iteration
8/14/2019 PythonIntro0821
65/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 65
continue immediately starts the next iterationfor i in range(4):
if i % 2 == 0:
continue
print i, is odd
1 is odd
3 is odd
Detecting Normal Loop Termination
8/14/2019 PythonIntro0821
66/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 66
An else clause after any loop is executedonly if the loop terminates normally
If abreak statement is executed, the else clauseis skipped
forname insequence:loop-suite
else:
termination-suite
while condition:loop-suite
else:
termination-suite
Exceptions
8/14/2019 PythonIntro0821
67/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 67
Python is an exception-based language This goes well with its general philosophy
Simplifies mainstream logic: can simply assume success
Exceptional conditions alter normal flow of control
try:suiteexcept exceptions[, value]:
handler 1 suiteexcept exceptions[, value]:
handler 2 suiteelse:
no-exception suite
finally:termination suite (always runs)
8/14/2019 PythonIntro0821
68/181
Handling Exceptions (2)
8/14/2019 PythonIntro0821
69/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 69
exceptionsin except clause may be
A single exception
A tuple of exceptions
Allows different exceptions to share the same logic
Mustput parentheses around tuple form
Avoids misinterpretations: comma might indicate value
Several different except clauses can catchseparate (sets of) exceptions if required
Running Programs
8/14/2019 PythonIntro0821
70/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission080721 Python in 3 Hours 70
Edit a script file namedsomething.py
Under Windows or Unix:
python something.py
Alternatively, under Unix only, with #! line:
chmod +x something.pysomething.py
The__name__ Variable
8/14/2019 PythonIntro0821
71/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission080721 Python in 3 Hours 71
The interpreter stores a modules name in its
namespace in the__name__variable This is normally the name of the .py file
When the module is run as a program it gets aspecial value for__name__
__main__ Test for this value to see if your module has been run
as a program
>>> import mymodule
>>> mymodule.__name__
mymodule
Programs are Modules
8/14/2019 PythonIntro0821
72/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission080721 Python in 3 Hours 72
A soundly-written module should run tests when itis run as a program
Test for the special name __main__
if __name__ == __main__:
run_test_code() run_test_code() is called only when the module is
executed as a program
Never when another program imports it
This feature is used by many standard library modules
You should incorporate it in your own modules
8/14/2019 PythonIntro0821
73/181
Purpose of Functions
8/14/2019 PythonIntro0821
74/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission080721 Python in 3 Hours 74
Functions allow the same piece of code to be usedat different places in a program, with variations
def celsius_to_fahrenheit(c_temp):return 9.0 / 5.0 * c_temp + 32
To convert a Celsius temperature c to Farenheityou would write
f = celsius_to_farenheit(c)
The argumentc is passed into the function body,where it can be referenced as theparameterc_temp
Defining Functions
8/14/2019 PythonIntro0821
75/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission080721 Python in 3 Hours 75
Functions are defined by thedef
statement
def function-name(parameters):function-body # indented suite
def is an executable statement
The interpreter reads and compiles the indented body
Resulting function object is bound to the function
name in current namespace
Functions can be bound to other names, passed asarguments, etc. like any other Python objects
The body is executed when the function is called
Parameter Specifications
8/14/2019 PythonIntro0821
76/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission
080721 Python in 3 Hours 76
A function definition lists the parameters (if any)
A call of the function provides arguments
The arguments give values to the parameters
The argument values are bound to the parameter names
in the calls local namespace A parameter may bepositional: a simple identifier
Values must be provided for every call
It may also be a keywordparameter: name=default If absent from call, takes default value
Positional parameters must precede keywords
Variable Parameter Lists
8/14/2019 PythonIntro0821
77/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission
080721 Python in 3 Hours 77
Some functions can take an indefinite number ofpositional and keyword arguments
These are caught with a special parameter syntax
*name becomes a tuple of extrapositionalarguments to a call
**name becomes a dictof extra keywordargumentsto a call: n=vbecomes {n: v}
If both are present, * must precede ** Use any name you often see *args and **kw
Arguments to Function Calls(1)
8/14/2019 PythonIntro0821
78/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission
080721 Python in 3 Hours 78
Simple expressions arepositionalarguments
Those of the form identifier=expressionare namedarguments
The identifier must be the name of a formal parameter*
The expression values are assigned to theparameters in the function calls local namespace
* Unless the function has a **kw parameter
Arguments to Function Calls (2)
8/14/2019 PythonIntro0821
79/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 79
Can supply additional positional arguments as*sequence
Can supply additional named arguments as**dict
>>> def testf(a, b, c, d):... print "A:", a, "B:", b... print "C:", c, "D:", d...>>> t = ("one", "two")>>> d = {'c': "three", 'd': "four"}>>> testf(*t, **d)
A: one B: twoC: three D: four>>>
Calling a Function (1)
8/14/2019 PythonIntro0821
80/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 80
Arguments to the call associate values with thefunctions parameters (for this call only)
Arguments can be supplied in two ways
Positional: expression position in argument listdetermines correspondingparameter receiving expression as value
Keyword: name=expression name of argument determined corresponding parameter
receiving expression as value
Positional arguments must precede keywords
Calling a Function (2)
8/14/2019 PythonIntro0821
81/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 81
The positional arguments are bound first
One at a time to successive parameters
Then keyword arguments are bound
To parameters with matching names
Unmatched keyword parameters take their defaults
As specified in the function definition
The returnStatement
8/14/2019 PythonIntro0821
82/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 82
Outside a function this is illegal
return is optionally followed by an expression
This is the value to be returned from the call
Function body terminates when return executed Function returnsNone if no return is present
Or when no expression follows return
Avoid return None at the end of a function Confusing andunnecessary
Which Parameter Gets WhichArgument?
8/14/2019 PythonIntro0821
83/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 83
Makes a list of positional arguments to the call
Including those provided by *args, if any
Assigns them to the formal parameters
In the order they appear in the definition
Includingnamed parameters if all positionals filled Remainder assigned to the *args parameter, if any
Keyword arguments are then assigned to their respectivenamed parameters
Remainder assigned to the **kw parameter, if any
Duplicated or unassigned arguments are in error
Passing Values to Function CallsCallers namespace Object space
8/14/2019 PythonIntro0821
84/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 84
Definition:
def f(a, b, c=[])
Call:
f(x, y)
x
y
x value
y value
[]
a
b c
Called function namespaceCalled function namespace
Arguments and Parameters
8/14/2019 PythonIntro0821
85/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 85
Arguments are references to objects
Names are direct references
Expressions become references to the expression value
The corresponding parameter is a copy of that reference
Nota copy of the value
Rebinding a parameter cannot rebind the argument
But a change to a mutable parameter (passed as arguments) alsochanges the argument
Remember: the argument and the parameter are simply tworeferences to the same (mutable) value
Mutable Default Values (1)
8/14/2019 PythonIntro0821
86/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 86
The same object is bound to a keyword parametereach time no corresponding argument is supplied
This can be tricky if the object is changed!
def f(x, y=[]):y.append(x)return y
print f(23) # prints: [23]
print f(42) # prints: [23, 42] How do we bindyto a new empty list each call?
Mutable Default Values (2)
8/14/2019 PythonIntro0821
87/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 87
Standard solution is to useNone as sentinel
def f(x, y=None):if y is None:
y = []y.append(x)return y
print f(23) # prints: [23]print f(42) # prints: [42]
The is comparison tests for identity Only sensible comparison forNone
Function Execution
8/14/2019 PythonIntro0821
88/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 88
The call to a function creates a new namespace
Initially populated only by the argument values
Assignments inside the body bind to names in thecalls namespace: local variables
Unless the bound name is defined as global in thefunction body
Then the name is bound in the module global namespace
Names are resolved in a specific namespace order: Local; module; built-in
Functions Are Just Values
F i b b d ( i d) d d
8/14/2019 PythonIntro0821
89/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 89
Functions can be bound (assigned) and passed as
arguments just like other values Remember: the def statement binds the newly-
created function to its name
>>> def celsius_to_fahrenheit(c_temp):... return 9.0 / 5.0 * c_temp + 32...>>> f = celsius_to_fahrenheit>>> print f(100)
212.0>>>
Nested Functions and Closures
8/14/2019 PythonIntro0821
90/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 90
It is possible to define one function inside another
The inner function then has access to the namespace ofthe outer function
This allows you to build closures: parameterized
functions created on demand Before Python 2.2 you could achieve similar effects
using defaulted named arguments
def adderOld(n):def add(m, N=n):return m+N
return add
def adderNew(n):def add(m):return m+n
return add
Built-In Functions and Methods
8/14/2019 PythonIntro0821
91/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 91
We havent yet looked at object orientation
That is where methods will be formally defined
For now, regard a method as a function with animplicit argument
E.g. the lst.append() call we used earlier operates onthe list whose name is qualified by the method name
There are quite a few functions built in to thelanguage
Built-in types have pre-defined methods
Further Functions and Methods
I ld b h di d il
8/14/2019 PythonIntro0821
92/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 92
It would be somewhat tedious to detail
All the built-in functions
All the methods of all the built-in types
So use the documentation
And talk to people you willusually get help
How can I questions will get quick answers oncomp.lang.python if theres a built-in to do the
work
Some Useful Built-In Functions
P th h b ilt i f ti
8/14/2019 PythonIntro0821
93/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 93
Python has many built-in functions
Some obscure but occasionally useful
Others more generally useful
There is a still more functionality in thestdlib
Difficult to cover all that in a short course
Be prepared to read the documentation
We cover the easier/more useful built-ins here
Standard library covered later
Conversion Functions
R d l it( )
8/14/2019 PythonIntro0821
94/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 94
Returns the ordinal value of the ASCII
or Unicode characterc
ord(c)
Returns the (Unicode) character whose
ordinal value isx
chr(x)unichr(x)
Returns a complete string
representation ofx
repr(x)
Returnsx converted to a stringstr(x)
Returnsx converted to a hexadecimal
string
hex(x)
Returnsx converted to an octal stringoct(x)
Introspection Functions
8/14/2019 PythonIntro0821
95/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 95
Returns True ifcls1 is a direct or indirectsubclass ofcls2
issubclass(cls1, cls2)
Returns True ifobj is an instance of the givenclass (or of one of a tuple of classes)
isinstance(obj, cls)
Deletes the named attribute of the given objectdelattr(obj,name)
Sets the named attribute of the given object to the
given value
setattr(obj,
name, value)
Returns the value of the named attribute of the
given object
getattr(obj,name[, dflt])
Returns the sorted list of all variables bound in
the objects namespace (defaults to currentscope)
dir([obj])
Mathematical Functions
R t th b l t l f babs(x)
8/14/2019 PythonIntro0821
96/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 96
Returnsx rounded to n decimal placesround(x, n)
Returns x**y%z ifzomitted, returns
x**y
pow(x,y[, z])
Returns (x//y, x%y) the quotient and
remainder of dividingx byy
divmod(x, y)
Returns the absolute value of numberxabs(x)
8/14/2019 PythonIntro0821
97/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 97
Chapter 5
Modules and Packages
Programming Mechanics
Modularity is an established organizing principle
8/14/2019 PythonIntro0821
98/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 98
Modularity is an established organizing principle
Splitting code makes it more manageable
People more readily understand smaller units
Also encourages code re-use
You can package useful functions inside modules
The standard library comprises packages andmodules
A module is just a piece of Python code in a file
You access modules with the import statement
Importing a Module
The first execution of the statement
8/14/2019 PythonIntro0821
99/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 99
Thefirstexecution of the statement
import mod1causes the interpreter to look for a module or
package named mod1 (well see how shortly)
The code ofmod1 is executed The module namespace is then bound to the namemod1 in the current namespace
It will also be bound to sys.modules[mod1] The modules namespace is bound to its name!
A Simple Module Example
# mod1.py # app1.py
8/14/2019 PythonIntro0821
100/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 100
# mod1.py
print "Mod1 executing"x = 100
# app1.py
print "First import"import mod1
print "Second import"import mod1
print mod1.x
$ python app1.pyFirst import
Mod1 executingSecond import
100
How Modules Are Found
The sys module contains information about your
8/14/2019 PythonIntro0821
101/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 101
The sys module contains information about your
system
sys.path is a list of directories
The interpreter searches each one in turn for a .py
(Python source) or.pyc (Python compiled) file You can set the PYTHONPATH environment
variable to a list of directories to be added to
sys.pathon interpreter start-up
Import Semantics (1)
The module code is only executed the first time
8/14/2019 PythonIntro0821
102/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 102
The module code is only executed the first time
the module is imported Avoids unnecessary duplication of initialization code
The modules namespace is accessible to the
importing module or program Qualify the modules name with the name you want
This makes modules useful for defining functions
And other re-usable code objects like classes
sys.modules is a dict of imported modules
Import Semantics (2)
The interpreter locates the modules code ( py)
8/14/2019 PythonIntro0821
103/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 103
The interpreter locates the module s code (.py)
It also looks for a compiled (.pyc) file
If .pyc is found, it compares dates
If the source is newer it is recompiled
A new .pyc file is created (if possible)
This minimizes the amount of compilation needed
import as
Sometimes you would like to import a module
8/14/2019 PythonIntro0821
104/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 104
y p
under a different name To avoid a clash with a name already in use
To allow different modules to be used in the same roledepending on conditions
import module as mynameis (almost) equivalent toimport module
myname = moduledel module
from import
Sometimes you only want a few names
8/14/2019 PythonIntro0821
105/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 105
Sometimes you only want a few names
And you dont want to use qualified names
In that case you can use this form:from module import name1, name2
The module is imported (if necessary)
Then the requested names are copied into thecurrent namespace
If the module rebinds a name this is invisible to theimporter
Python Packages
Sometimes a coordinated set of modules is
8/14/2019 PythonIntro0821
106/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 106
required Apackage is a directory containing (at a
minimum) an__init__.py file
When thepackage is imported__init__.py is run
Other modules can be imported from the packagetree
A package can also contain sub-packages
8/14/2019 PythonIntro0821
107/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 107
Chapter 6
Object-Oriented Programming
Types and Instances
We have seen a number of built-in data types
8/14/2019 PythonIntro0821
108/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 108
Each different type has methods that performcertain operations on it
list.sort(), dict.keys() etc.
We dont have to redefine the methods for eachnew list or dict they are defined by the types
We could do this is by defining functions
But that requires they be called with correct arguments
Representing Data Structures (1)
Suppose we want to write an accounting program
8/14/2019 PythonIntro0821
109/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 109
pp g p g
Each account need a name, an opening date, abalance and a history
Could represent this as a list:Amex = ["American Express", "2000-11-18", 0.0, []]
Can get name asAmex[0], balance asAmex[2]
This is not the easiest way to program
Difficult to read, and therefore maintain
Representing Data Structures (2)
Could use symbolic constants as field indexes:
8/14/2019 PythonIntro0821
110/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 110
y
AcName = 0OpenDate = 1Balance = 2History = 3
Can now refer to balance asAmex[Balance] But if code is imported (say, from module accts)
we would then have to use
Amex[accts.Balance]
Representing Data Structures (3)
So, how about a dict?
8/14/2019 PythonIntro0821
111/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 111
Amex = {'AcName': 'American Express','OpenDate': '2000-11-08','Balance': 0.0,'History': []}
Now we use strings as indexes:
Amex["AcName"]Amex["History"].append(('2000-11-20','Laptop Computer', 3896.99))
Yerch, all that typing: []
Again we could use symbolic constants for the strings
But the brackets would still remain
What Would We Really Like?
Simpler, more usable, more readable mechanism
8/14/2019 PythonIntro0821
112/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 112
A true namespace similar to a module
So we can writeacbal = Amex.balance
We need to define the class of all accounts In Python we can do this with a class statement! Very similar to def its also executable
Interpreter compiles the indented suite and binds it tothe class name in the current namespace
Simplest Possible Class Definition
class Account:
8/14/2019 PythonIntro0821
113/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 113
pass We can use this class definition in the following way:
Amex = account() # call creates an instance
This is OK, but there are no attributes. Lets set some!
>>> Amex = Account()>>> Amex.AcName = "American Express">>> Amex.OpenDate = '2000-11-18'>>> Amex.Balance = 0.0>>> Amex.History = []>>> dir(Amex)
['AcName', 'Balance', 'History', 'OpenDate','__doc__', '__module__']>>>
An Account Factory Function
If we put all these bits together we could define a
8/14/2019 PythonIntro0821
114/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 114
function to create accounts
Gives us an easy way to build account instances Makes sure nothing is forgotten
def newAccount(AcName, OpenDate, Balance=0.0):a = Account()a.OpenDate = OpenDatea.AcName = AcNamea.Balance = Balance
a.History = []return aAmex = newAccount("American Express",
"2000-11-18")
Separating Creation and Initialization
Another way to organize the code:
8/14/2019 PythonIntro0821
115/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 115
Create an instance, then initialize it
In this case, initAccount takes anAccountinstance argument and modifies it
We can think of it as an operation on anaccount
def initAccount(ac, AcName, OpenDate, Balance=0.0):ac.OpenDate = OpenDateac.AcName = AcNameac.Balance = Balance
ac.History = []Amex = Account()initAccount(Amex, "American Express", "2000-11-18")
Another Account Operation
Suppose we want to standardize transaction
8/14/2019 PythonIntro0821
116/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 116
processing:
Could implement all this in a module and use that:
What does class do for us that this doesnt?
def processTransaction(ac, date, amount, reason):ac.Balance += amountac.History.append((date, amount, reason))
import acctsAmex = accts.Account()accts.initAccount(Amex, American Express, 2000-11-18)accts.ProcessTransaction(Amex, 2000-11-30, 3499.99,
Personal Computer)
Instances are Namespaces
We can pass an instance as an argument
8/14/2019 PythonIntro0821
117/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 117
Then access and/or change the arguments attributesinside the function
Does the interpreter know in advance what
attributes an instance has? No! It looks them up when access is attempted!
This late binding behavior seems strange to
programmers used to C or C++ And it does incur a run-time cost
Classes Are Namespaces Too
We have seen that a function call creates a
8/14/2019 PythonIntro0821
118/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 118
namespace Defining a class also creates a namespace
Bindings inside the class body are made in the
class namespace, giving rise to class attributes Defining a function inside the class body puts the
function name in the class namespace
This is how we create methods!
Namespace Hierarchy
The interpreter will look in the class for an attribute it
8/14/2019 PythonIntro0821
119/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 119
cannot find in an instance
As you see, an instance attribute will override a classattribute with the same name
>>> class AttTest:... attval = "DEFAULT...>>> i1 = AttTest()>>> i2 = AttTest()
>>> i2.attval = "instance>>> i1.attval'DEFAULT>>> i2.attval'instance>>>
Classes Can Specify Behavior
We can define functions to operate on instances of a
8/14/2019 PythonIntro0821
120/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 120
specific class How do we make the functions a part of the class?
That looks like it might work lets try it(note: the method code is exactly that of the functions we were just looking at)
class Account:
def initAccount(ac, AcName, OpenDate, Balance=0.0):
ac.OpenDate = OpenDateac.AcName = AcNameac.Balance = Balanceac.History = []
def processTransaction(ac, date, amount, reason):ac.Balance = acct.Balance + amount
ac.History.append((date, amount, reason))
8/14/2019 PythonIntro0821
121/181
Methods as Instance AttributesCurrent = Account()Current.initAccount(Current, "Checking Account",
"2000-11-26", 1234.56)
8/14/2019 PythonIntro0821
122/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 122
Five arguments?
Where does the extra one come from?
The interpreter special-cases instance method calls!
Current.processTransaction(Current, "2000-11-26",-1000.00, "Cash Withdrawal")
print "%8.2f" % Current.BalanceTraceback (most recent call last):
File "exectest.py", line xx, in ?Current.initAccount(Current, "Checking Account",
"2000-11-26", 1234.56)TypeError: initAccount() takes at most 4 arguments (5 given)
Instance Method Calls
When you call a methods instance
8/14/2019 PythonIntro0821
123/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 123
You almost invariably need a reference to the instance
So the interpreter provides it as the first argument
Simple fix: remove the instance arguments!
Current = Account()Current.initAccount("Checking Account",
"2000-11-26", 1234.56)Current.processTransaction("2000-11-26",
-1000.00, "Cash Withdrawal")print "%8.2f" % Current.Balance
234.56
The selfConvention
When we qualify a class with a method name
h li i l d
8/14/2019 PythonIntro0821
124/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 124
The argument list is exactly as passed
When we qualify an instance with a method name
When called, the instance is put as the first argument
Such references are called bound methods
Usually we call this implicit argument self
As we have just seen, it works whatever the name
But any other name makes your code harder to read
The__init__() Special Method
When a class is called, Python creates an instance
If h l h i i () h d i i ll d
8/14/2019 PythonIntro0821
125/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 125
If the class has an __init__() method it is called
Arguments to the call are passed to__init__()
Preceded by the newly-created instance, as self
__init__() must neverreturn a value Instance is created before__init__() is called
So, what would our class look like if we use all
this new stuff?
The Final Account Classclass Account:
def __init__(self, AcName, OpenDate,
B l 0 0)
8/14/2019 PythonIntro0821
126/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 126
We create a new account as follows:
Balance=0.0):self.OpenDate = OpenDateself.AcName = AcNameself.Balance = Balanceself.History = []
def processTransaction(self, date,amount, reason):
self.Balance += amountself.History.append((date, amount, reason))
Amex = Account("American Express", "2000-11-18")
Polymorphism
Polymorphism means that different types of object
d t th ti
8/14/2019 PythonIntro0821
127/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 127
can respond to the same operations They have the same methods, and the methods
take the same arguments
Suppose classesAand B both have a doit()method
You can write this.doit() in your code
It doesnt matter whetherthis is anAor a B The interpreter will find the method
Encapsulation
Encapsulation means you dont need to know the
i l t ti d t il f bj t
8/14/2019 PythonIntro0821
128/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 128
implementation details of an object You just need to know its API
Python users arent rigorous about encapsulation
Its OK to access attributes directly
This makes it easier to reorganize your code
As long as the API remains the same nobodys code
will breakAPI: Application programmer interface the methods, and their required arguments
Inheritance
Inheritance allows you to form new classes from
th h l d d fi d
8/14/2019 PythonIntro0821
129/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 129
those you have already defined You createsubclasses of existing classes
A checking account is like an account but
You can
Add new methods
Replace existing methods
Create methods that extend the superclasss methods
A Simple Inheritance Example (1)
We have already defined anAccount class
Four attributes and two methods
8/14/2019 PythonIntro0821
130/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 130
Four attributes and two methods
Suppose we want a class that does everything ourAccount class does
Except it has a method to print the history
Could write an enhanced class that is a subclass ofAccount
With aprintHistory()method specific to thesubclass
A Simple Inheritance Example (2)
import accts
class QuickAccount(accts Account):
8/14/2019 PythonIntro0821
131/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 131
Does everything the same way except printing thehistory
Because it inherits all methods and attributes from the
parentAccountClass.Account class
class QuickAccount(accts.Account):
def printHistory(self):for h in self.History:print "%12s %10.2f" % (h[0], h[1])
How Inheritance Works
When an instance attribute (including a method) is
accessed the interpreter looks
8/14/2019 PythonIntro0821
132/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 132
accessed, the interpreter looks In the instance namespace
Then in the instances classs namespace
Then in the classs parent classs namespace and so on until there is nowhere left to look
Failure raises anAttributeError exception
This method resolution order is how a subclasscan override its parents attributes
The Object Hierarchy
In modern Python (since 2.2) everything
ultimately inherits from the object class
8/14/2019 PythonIntro0821
133/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 133
ultimately inherits from the object class Prior to 2.2 you could not subclass the built-in types
The issubclass(c, classinfo) built-in
returns True if the first argument is a direct orindirect subclass of the second
classinfo may be either a class or a tuple of classes
In the latter case result is True ifc is a subclass ofany class in the classinfo tuple
8/14/2019 PythonIntro0821
134/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 134
Chapter 7
Formatting and I/O
Opening a File
Use the open(path[, mode[, bufsiz]])
built-in to open a file
8/14/2019 PythonIntro0821
135/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 135
built-in to open a file Read, write or append indicated by mode r, w, ora
Binary files follow the mode with a b
bufsize 0: unbuffered; 1: line buffered; +ve: use(roughly) that much space; -ve: use system default
Returns a file object, whose methods are used to access
the file
Binary vs Text, Read vs Write
The second argument to open() specifies modes
r : file will be read
8/14/2019 PythonIntro0821
136/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 136
r : file will be read w: file will be written (truncates to zero length)
a : file will be appended
+ : file can be updated, modifying existing content
b : file is opened in binary mode
In binary mode the file is read/written byte for byte Otherwise the program sees \n line endings
No matter what line endings the OS uses on media
The File API (Abbreviated)
The open() returns a file with the usual methods
All d t f filread()
8/14/2019 PythonIntro0821
137/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 137
There are others too, see the documentation
A generator of the lines in a
file
Xreadlines()
Reports current positiontell()
Moves current positionseek(p)Writes s into the filewrite(s)The next line in the filereadline()
A list of the lines in a filereadlines()All data from fileread()
File-Like Objects
The standard library provides StringIO and
cStringIO objectsFil i f d d i
8/14/2019 PythonIntro0821
138/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 138
cStringIO objects File interface, data stored in memory
This raises a frequent approach in Python
Two objects with the same methods are effectively
interchangeable Sometimes called duck typing (if it quacks like a duck )
Many contexts accept a file-like object
If the necessary methods are supported, polymorphismallows file handling to work
File Handling Example (1)
# Write a file out_file = open("test.txt", "w")out_file.write("This Text is going out\nLook at the file!")out_file.close()# R d th fil b k i
8/14/2019 PythonIntro0821
139/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 139
# Read the same file back inin_file = open("test.txt", "r")text = in_file.read()in_file.close()print text
This test is going outLook at the file!
This straightforward example shows how to deal
with whole text files This approach is harder with large files
Formatting Operations
With a string as the left-hand operand% is a formatting operator
Th l ft h d d t i f t ifi
8/14/2019 PythonIntro0821
140/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 140
The left-hand operand contains format specifiers
Introduced by percent signs
Codes parallel Csprintf formatting codes
The right-hand operand can be
A single value (if LHS has only one format code)
A tuple: format specifiers follow % immediately
A dict or other mapping: format specifiers follow %and a parenthesized mapping key
Most Common Format Codes
Decimald
String uses values str() methods
8/14/2019 PythonIntro0821
141/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 141
Inserts a literal percent sign%
Unsigned hexadecimalX, x
Single characterc
General: fixed or floating, depending ondata value
G, g
Floating-point decimalF, f
Decimald
8/14/2019 PythonIntro0821
142/181
File Handling Example (2)
import sysif len(sys.argv) < 2:
sys.exit("No filename")f = open(sys.argv[1], 'r')n = 0
8/14/2019 PythonIntro0821
143/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 143
n = 0for line in f:
n += 1print "%4d %s" % (n, line[:-1])
f.close()
1 import sys2 if len(sys.argv) < 2:3 sys.exit("No filename")4 f = open(sys.argv[1], 'r')5 n = 06 for line in f:7 n += 18 print "%4d %s" % (n, line[:-1])9 f.close()
8/14/2019 PythonIntro0821
144/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 144
Chapter 8
Some More Advanced Topics
(Optional)
List Comprehensions
A concise way to create lists
Avoids use of repeated append() callsE l t i t dl
8/14/2019 PythonIntro0821
145/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 145
pp Evaluates an expression repeatedly
Under the control of a for iteration With the chance to veto values with if
>>> vec = [1, 2, 3]>>> [3*x for x in vec][3, 6, 9]>>> [3*x for x in vec if x != 2][12, 18]>>> [3*x for x in vec if not (x % 5)][]
>>> vec2 = [4, 3, -9]>>> [x*y for x in vec for y in vec2][4, 3, -9, 8, 6, -18, 12, 9, -27]
Another List Comprehension
>>> words = 'The quick brown fox jumps over the lazy dog\.split()
>>> print words
['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the','lazy', 'dog']
8/14/2019 PythonIntro0821
146/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 146
lazy , dog ]>>>>>> stuff = [[w.upper(), w.lower(), len(w)] for w in words]>>> for i in stuff:... print i...
['THE', 'the', 3]['QUICK', 'quick', 5]['BROWN', 'brown', 5]['FOX', 'fox', 3]['JUMPS', 'jumps', 5]['OVER', 'over', 4]['THE', 'the', 3]['LAZY', 'lazy', 4]['DOG', 'dog', 3]
Containers Can Be Expensive
Generating a large list or tuple just to iterate over
it is inefficient
8/14/2019 PythonIntro0821
147/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 147
All items have to be created (occupying memory)
The container itself occupies memory
Also takes time to allocate memory and garbagecollect it
The container has a reference to each item
No memory can be reclaimed until list is no longerreferenced
Replacing Containers (1)
Lists and tuples are sequences of references to
objects
8/14/2019 PythonIntro0821
148/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 148
j Lists are mutable, tuples arent
Space is required for each value, and for the
references What if sequences could be generated on demand?
Each element taking up space only as long asreferenced
Replacing Containers (2)
Its easy to iterate over a list or a tuple
Because the values can be accessed by indexing
8/14/2019 PythonIntro0821
149/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 149
Could we write a function that returned the
values one at a time, on demand, to a loop?
This would let us deal with infinite sequences We have no way to create an infinite list or tuple!
But functions are one-shot
A single call returns a result
Generator Functions
How do we handle the situation where computation isneeded to create each element of the sequence?
A class of objects called generators are also iterable
8/14/2019 PythonIntro0821
150/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 150
A class of objects calledgenerators are also iterable
You can define your owngenerator functions
Body is like normal function body
But it contains one or more yieldexpressions
Calling the function returns a generator-iterator (usually
just called a generator)
Generators: Beyond the Container
New keyword yield
yieldcan only be used inside a function bodyil li d i h i h k
8/14/2019 PythonIntro0821
151/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 151
Easily policed with static checks
But does the function return anything?
Functions containing a yieldexpression do not runwhen called!
Instead the call returns agenerator
When used in a loop the generator yields successive values
No need to create a sequence to hold the values!
Introduction to Generators
A generator can be used in place of a sequence
Inside a looping context
8/14/2019 PythonIntro0821
152/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 152
It creates values, one by one, on demand
How do you use these generators?
The usual way is just to iterate over them
x = generatorFunction()for x in generator:
process(x)
Generator Function Example
Suppose we needed [1, 1, 2, 2, , N, N]
For some value of N to be decided by the user
8/14/2019 PythonIntro0821
153/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 153
Duplication is easy with two successive yields
>>> def pairsto(N):... for i in range(N): # 0, 1, , N-1... yield i+1... yield i+1
...>>> [j for j in pairsto(3)][1, 1, 2, 2, 3, 3]>>>
Iterating Over a Generator (1)
When the function is called, it returns agenerator-
iteratorobject Without the function body running at all
8/14/2019 PythonIntro0821
154/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 154
Without the function body running at all
The function namespace is created, including the
arguments
The namespace continues to exist until
Function body returns (raising StopIteration)
Or the generator is no longer referenced
Iterating Over a Generator (2)
Iterating over the generator repeatedly calls its
next() method Each time next() is called the function body resumes
8/14/2019 PythonIntro0821
155/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 155
Each time next() is called the function body resumes
A yieldexpression generates a result fornext()
Expression value passes into iteration Namespace is retained by generator for re-activation
When the function returns (terminates) this causesnext() to raise StopIteration
Advantages of Generators
Can express producer-consumer algorithms morenaturally
Generation of values is cleanly separated from their
8/14/2019 PythonIntro0821
156/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 156
y pprocessing
Each generator has its own namespace
Complex logic can be layered inside the generatorAPI
Avoids messy solutions like callback arguments
Provides a simplified interface between producer andconsumer
Containers, Generators andIterators
Sometimes you needto have all elements present
together In which case lists and tuples are fine
8/14/2019 PythonIntro0821
157/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 157
In which case lists and tuples are fine
When it isnt necessary we can use generators
Saves memory overhead Is often quicker too
Sometimes its nice to have other ways to interact
with Pythons iteration features For this we can roll our own iterators
Avoiding Containers andGenerators
Sometimes it is better to do without containers
Avoids using space to store all elements
S ti d l ti th t i
8/14/2019 PythonIntro0821
158/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 158
Sometimes we need a solution that is morecomplex than a generator function
Typically implementing our solution as a class But how?
Need to interact with the interpreters iteration protocol
Which we therefore have to understand
Iteration in Python
Python has several iteration contexts
Basically, anywhere the keyword for is used So far we have shown you iteration over containers
8/14/2019 PythonIntro0821
159/181
What Do You Need to Know
Today?
2008 Holden Web: Reproduction prohibited without written
permission 080721 Python in 3 Hours 159
So far we have shown you iteration over containers
for item in list:
for item in tuple:for (key, value) in dict.items():
How does the interpreter interact with these objects to
iterate over them?
Iteration Support in 2.1 and Before
Lists and tuples have a__getitem__(self, i)
method for loops would call it repeatedly with successively
8/14/2019 PythonIntro0821
160/181