Python Notes
Python Notes
DSC551
List of Chapters
Chapter 1: Very Basic Stuff
Chapter 2: Conditionals
Chapter 3: Functions
Chapter 4: Iteration
Chapter 5: Strings
Chapter 6: Collection Data Types
Chapter 7: Advanced Functions
Chapter 8: Exception Handling
Chapter 9: Python Modules
Chapter 10: Files
Chapter 11: Documentation
Chapter 12: Classes
Chapter 13: CGI Programming
Disclaimers
#!/usr/bin/python
Hello, world
More about printing
Output:
4
3.1415926
Hello, world
Variable types
In the previous example, “pi” and “message” are variables,
but one is a floating point number, and the other is a string.
Notice we didn't declare the types in our example. Python
has decided types for the variables, however.
Actually, “variables” in python are really object references.
The reason we don't need to declare types is that a
reference might point to a different type later.
references.py:
x=42
y=”hello”
print x,y # prints 42 hello
print x,y # prints 42 42
Variable types
Example types.py:
pi = 3.1415926
message = "Hello, world"
i = 2+2
print type(pi)
print type(message)
print type(i)
Output:
<type 'float'>
<type 'str'>
<type 'int'>
Variable names
Output:
4
8
1
0.5
0
Note the difference between floating point division and
integer division in the last two lines
+= but not ++
Python has incorporated operators like +=, but
++ (or --) do not work in Python
Type conversion
int(), float(), str(), and
bool() convert to
integer, floating point,
string, and boolean
(True or False) types,
respectively
Example typeconv.py: Output:
print 1.0/2.0 0.5
print 1/2 0
print float(1)/float(2) 0.5
print int(3.1415926) 3
print str(3.1415926) 3.1415926
print bool(1) True
print bool(0) False
Operators acting on strings
>>> "Ni!"*3
'Ni!Ni!Ni!'
>>> "hello " + "world!"
'hello world!'
Input from keyboard
Example input.py
i = raw_input("Enter a math expression: ")
print i
j = input("Enter the same expression: ")
print j
Output:
localhost(workshop)% ./input.py
Enter a mathematical expression: 3+2
3+2
Enter the same expression: 3+2
5
Comments
Anything after a # symbol is treated as a
comment
This is just like Perl
Chapter 2: Conditionals
True and False booleans
Comparison and Logical Operators
if, elif, and else statements
Booleans: True and False
>>> type (True)
<type 'bool'>
>>> type (true)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'true' is not defined
>>> 2+2==5
False
Note: True and False are of type bool. The
capitalization is required for the booleans!
Boolean expressions
A boolean expression can be evaluated as True
or False. An expression evaluates to False if it
is...
the constant False, the object None, an empty
sequence or collection, or a numerical item of
value 0
Everything else is considered True
Comparison operators
== : is equal to?
!= : not equal to
> : greater than
< : less than
>= : greater than or equal to
<= : less than or equal to
is : do two references refer to the same object?
(See Chapter 6)
More on comparisons
Can “chain” comparisons:
>>> a = 42
>>> 0 <= a <= 99
True
Logical operators
and, or, not
Output:
The square of 3 is 9
The def statement
Output:
File "./square2.py", line 9, in <module>
print m
NameError: name 'm' is not defined
Scope
Variables assigned within a function are local to that
function call
Variables assigned at the top of a module are global to
that module; there's only “global” within a module
Within a function, Python will try to match a variable
name to one assigned locally within the function; if that
fails, it will try within enclosing function-defining (def)
statements (if appropriate); if that fails, it will try to
resolve the name in the global scope (but the variable
must be declared global for the function to be able to
change it). If none of these match, Python will look
through the list of built-in names
Scope example
scope.py
a=5 # global
def func(b):
c=a+b
return c
scope.py
a=5 # global
def func(b):
global c
c=a+b
return c
def f1(x,y):
x=x*1
y=y*2
print x, y # 0 [1, 2, 1, 2]
def f2(x,y):
x=x*1
y[0] = y[0] * 2
print x, y # 0 [2, 2]
a=0
b = [1,2]
f1(a,b)
print a, b # 0 [1, 2]
f2(a,b)
print a, b # 0 [2, 2]
Multiple return values
def onetwothree(x):
return x*1, x*2, x*3
print onetwothree(3)
3, 6, 9
Built-in Functions
Several useful built-in functions. Example
math.py
print pow(2,3)
print abs(-14)
print max(1,-5,3,0)
Output:
8
14
3
Functions of Functions
Example funcfunc.py Output:
def iseven(x,f): True
if (f(x) == f(-x)): False
return True
else:
return False
def square(n):
return(n*n)
def cube(n):
return(n*n*n)
print iseven(2,square)
print iseven(2,cube)
Default arguments
print_error(42)
error at line 42
Functions without return values
output:
1*4=4
2 * 5 = 10
3 * 6 = 18
Chapter 5: Strings
String basics
Escape sequences
Slices
Block quotes
Formatting
String methods
String basics
Strings can be delimited by single or double quotes
Python uses Unicode, so strings are not limited to ASCII
characters
An empty string is denoted by having nothing between
string delimiters (e.g., '')
Can access elements of strings with [], with indexing
starting from zero:
>>> “snakes”[3]
'k'
Note: can't go other way --- can't set “snakes”[3] = 'p' to
change a string; strings are immutable
a[-1] gets the last element of string a (negative indices
work through the string backwards from the end)
Strings like a = r'c:\home\temp.dat' (starting with an r
character before delimiters) are “raw” strings (interpret
literally)
More string basics
Type conversion:
>>> int(“42”)
42
>>> str(20.4)
'20.4'
Compare strings with the is-equal operator, ==
(like in C and C++):
>>> a = “hello”
>>> b = “hello”
>>> a == b
True
>>>location = “Chattanooga “ + “Tennessee”
>>>location
Chattanooga Tennessee
Escape sequences
Escape Meaning
\\ \
\' '
\” “
\n newline
\t tab
\N{id} unicode dbase id
\uhhhh unicode 16-bit hex
\Uhhhh... Unicode 32-bit hex
\x Hex digits value hh
\0 Null byte (unlike C, doesn't end
string)
Block quotes
Multi-line strings use triple-quotes:
>>> lincoln = “””Four score and seven years
... ago our fathers brought forth on this
... continent, a new nation, conceived in
... Liberty, and dedicated to the proposition
... that all men are created equal.”””
String formatting
Formatting syntax:
format % object-to-format
>>> greeting = “Hello”
>>> “%s. Welcome to python.” % greeting
'Hello. Welcome to python.'
Note: formatting creates a new string (because strings are immutable)
The advanced printf-style formatting from C works. Can format multiple
variables into one string by collecting them in a tuple (comma-separated list
delimited by parentheses) after the % character:
>>> “The grade for %s is %4.1f” % (“Tom”, 76.051)
'The grade for Tom is 76.1'
Simple example:
z = squares(4)
z.next()
0
z.next()
1
Persistence of mutable default
arguments
Mutable default arguments persist between
calls to the function (like static variables in C)
This may not be the behavior desired
If not, copy the default at the start of the
function body to another variable, or move the
default value expression into the body of the
function
Chapter 8: Exception Handling
Basics of exception handling
Basic Exception Handling
An “exception” is a (recognized type of) error, and
“handling” is what you do when that error occurs
General syntax:
try:
code-you-want-to-run
except exception1 [as variable1]:
exception1 block
...
except exceptionN [as variableN]:
exceptionN block
If an error occurs, if it's of exception type 1, then variable1
becomes an alias to the exception object, and then
exception1 block executes. Otherwise, Python tries
exception types 2 ... N until the exception is caught, or else
the program stops with an unhandled exception (a
traceback will be printed along with the exception's text)
The optional [as variable] will not work with older Python
Exception example
value-error.pl
try:
i = int("snakes")
print "the integer is", i
except ValueError:
print "oops! invalid value"
Other exceptions
EOFError is raised at the end of a file
IndexError happens if we use an invalid index
for a string/collection, e.g., if we try to get
argv[1] if there is only one command-line
argument (counting starts from zero)
TypeError happens when, e.g., comparing two
incomparable types
Chapter 9: Python Modules
Basics of modules
Import and from … import statements
Changing data in modules
Reloading modules
Module packages
__name__ and __main__
Import as statement
Module basics
Each file in Python is considered a module. Everything
within the file is encapsulated within a namespace (which
is the name of the file)
To access code in another module (file), import that file,
and then access the functions or data of that module by
prefixing with the name of the module, followed by a period
To import a module:
import sys
(note: no file suffix)
Can import user-defined modules or some “standard”
modules like sys and random
Any python program needs one “top level” file which
imports any other needed modules
Python standard library
Basic operations:
import sys
sys.stdout = open('output.txt', 'w')
print message # will show up in output.txt
infile.close()
Chapter 11: Documentation
Comments
dir
Documentation strings
Comments
As we've seen, anything after a # character is
treated as a comment
dir
The dir function prints out all the attributes of an
object (see later)
print sum.__doc__
This function just adds two numbers
Docstrings for built-in objects
Can print the docstrings for built-in Python
objects
self.name = name
self.test1 = test1
self.test2 = test2
def compute_average(self):
def print_data(self):
David.print_data()
Bob.print_data()
Comments on Student Example
a = Point()
b = Point(1,1)
print a==b
Fancier equals checking
def display(self):
print self.data
class Class2(Class1):
def square(self):
self.data = self.data * self.data
a = Class2(5)
a.square() # member function specific to Class2
a.display() # inherited member function from Class1
Alternative Syntax for Method Calls
• Instead of calling methods through their objects, we can also
call them through the class name:
x = SomeClass()
x.method1()
-or-
SomeClass.method1(x)
• Useful if we need to guarantee that a superclass constructor
runs as well as the subclass constructor:
class Class2(Class1):
def __init__(self, data):
Class1.__init__(self, data)
… new code for Class2 here...
__getitem__
• We can overload array indexing syntax with __getitem__
class testit:
def __getitem__(self, n):
return self.data[n]
A = testit()
A.data = “junk”
A[1]
'u'
__getattr__ and __setattr_
Class SomeClass:
def __setattr__(self, attr, value):
self.__dict__[attr] = value
class Number:
def __init__(self, data):
self.data = data
def __repr__(self):
return 'Number(%s)' % self.data
a = Number(5)
print a
>> Number(5)
destructors
• First line:
print “Content-type: text/html\n” # need \n
• Everything else will be passed along for
interpretation by the client web browser. Plain
text will usually print ok, but typically you want
to format as Hypertext Markup Language
(HTML)
• These notes won't explain basic HTML; that
information is easy to find elsewhere
Input
import urllib
tmpstr = '<a href=”test.py?name=s”>' % \
(urllib.quote_plus(name))
• While we're on the subject, items going into
HTML may also need to be escaped; can do
this with cgi.escape(). Does mostly, but not
exactly, the same as urllib.quote_plus()
Form example
• htmlform = """ POST input
»<FORM method=POST action="testcgi.py">
»<P>
»<table>
»<tr><td>Name:</td>
»<td><input type=text name=name size=40 value="%(name)s" /></td></tr>
»<tr><td>Job:</td>
»<td><input type=text name=job size=40 value="%(job)s" /></td></tr>
»<tr><td>Years with company:</td>
»<td>
»<select name=years value=%(years)s>
» <option value="1" %(years1)s>1</option>
» <option value="2" %(years2)s>2</option>
» <option value="3" %(years3)s>3</option>
»</select>
»</td></tr>
»<tr><td>Manager:</td>
»<td><input type=checkbox name=manager value="1"
%(managerchecked)s></td>
» </tr>
» <tr><td><input type="submit" name="send" value="Submit"></td>
» """
print htmlform % suppdata
Reading the input
• Python provides a unified module which can
parse both GET and POST input (or both
simultaneously)
data = {}
for field in ('name', 'job', 'years', 'manager'):
if not form.has_key(field):
data[field] = ""
else:
if type(form[field]) != list:
data[field] = form[field].value
else: # merge lists to string with and's
values = [x.value for x in form[field]]
data[field] = ' and '.join(values)
Reading the input
suppdata['years1'] = ""
suppdata['years2'] = ""
suppdata['years3'] = ""
if data['years'] == "1":
suppdata['years1'] = "selected"
elif data['years'] == "2":
suppdata['years2'] = "selected"
elif data['years'] == "3":
suppdata['years3'] = "selected"
else:
# upload data to a database here
# print ok to the user
print html_header
print "Adding data to the database.<br />\n"
print "name=%s, job=%s, years=%s.<br />\n" % \
(data['name'], data['job'], data['years'])
print "Is",
if (data['manager'] == "1"):
print "a manager.<br />\n"
else:
print "not a manager.<br />\n"
print "New data submitted!<br />\n"
print html_footer