Lectures‎ > ‎



  • Strategies for doing labs and studying.
  • Chapter 4
  • Chapter 5
  • More on HTML and web directories
  • Executing python in web directories

Functions - Return Values

  • Default return value is None
  • Can return anything
  • Make sure you expect the return value type

Functions with if statements (conditional execution)

  • Make sure all paths return a value
def absolute_value(x):
    if < 0:
        return -x
    elif x > 0:
        return x

  • Use return variable to have only one return value
  • Show example using a return variable.
def absolute_value(x):
    if x < 0:
        return -x
        return x

Incremental Development

  • Define the purpose of a function
  • Give it a name
  • Write a template, include parameters
  • Return a default value
  • Add functionality with temporary variables and print statments
  • Build 
    • is_square(x1,y1,x2,y2,x3,y3,x4,y4)

Function Composition

  • Functions can call other functions

Boolean Functions

  • Functions can return boolean values
  • Useful for asking questions for if statements
  • Usually you name such function with is_<something>
    • is_square(...)
    • is_divisible(...)

Functions as Data

  • Functions can be passed as arguments
  • Functions have type <type 'function'>

Programming Style

  • See Section 5.6 in HTLCS

Unit Tests using Doc Tests

  • Used to test python code
  • Extremely useful
  • Example

def is_divisible_by_2_or_5(n):
        >>> is_divisible_by_2_or_5(8)

if __name__ == '__main__':
    import doctest

Iteration with While 

  • Need a way to do something repeatedly
  • One way in Python is using the while statement
    # body

  • Common pattern:
    • Use a counter variable
    • Check for counter value
    • Increment/decrement value

i = 0
while (i < 10):
    print i
    i = i + 1

Executing Python Code in Web Directories

  • First, create a cs110 subdirectory in your web directory:
$ cd /home/web/<usernmae>
$ mkdir cs110
$ chmod 755 cs110
  • The last chmod allows the CS web server to access your new cs110 directory
    • You will need to do this for each new subdirectory you create.
  • In your /home/web/<username>/cs110 directory, create an ".htaccess" file:
$ cd /home/web/<username>/cs110
$ cat > .htaccess
Options +ExecCGI
AddHandler cgi-script .py
$ chmod 644 .htaccess

  • Now you can put .py files in any subdirectory
  • Remember to set the permissions for each file you add to your web directory (chmod 644 <filename>)
  • If you create new subdirectories, be sure to set the permissions of the subdirectory with chmod 755 <dirname>
  • The first line of your python program should be:
#!/usr/bin/env python
  • Your Python code must print the following first before any HTML:
print 'Content-type: text/html'
  • For each Python file you create, you also need to give the web server permission to read and execute it:
chmod 755 file.py

  • Example:
$ cat test.py
#!/usr/bin/env python

print 'Content-type: text/html'

print '<html>'
print '<body>'

print 'Hello World directly from Python!'

print '</body>'
print '</html>'
$ chmod 755 test.py

Format Strings in Python

  • So, far we have used print with "," and "+" to construct strings
  • Python also provides a special "format" string that can be used as a template.
  • For example:
a = 1
name = 'Greg'

print 'a = %d and name = %s' % (a, name)

  • Multi-line strings:
$ cat multiline.py 
text = """
This is a

print text
$ python multiline.py 

This is a


Simple HTML Form and Python CGI

  • You can create a simple HTML form that accepts input from the user.
  • Here is an example:
$ cat simpleform.py 
#!/usr/bin/env python
import cgi

print "Content-type: text/html"

form = cgi.FieldStorage()
first_name = form.getvalue('first_name', '')
last_name = form.getvalue('last_name', '')

print """
<form action='simpleform.py'>
First Name: <input type="text" name="first_name" />
Last Name: <input type="text" name="last_name" />
<input type='submit' />
Your name: %s %s
</html>""" % (first_name, last_name)
  • If you put this example in your web directory and point your web browser at it, e.g.:
    •  http://www.cs.usfca.edu/~<username>/cs110/week4/simpleform.py
  • It will ask for a first and last name.
  • When you press return or click on the "Submit" button, it will call the simpleform.py program again, but this time with the first and last name that was provided by the user.
  • Some things to notice:
    • We import cgi so that we can get at the user supplied input
    • In the HTML, we use a "form" and specify different input fields.
    • Notice that we need to match up the names with the Python variable names
    • We use a multi-line format string to print the user-supplied name.
  • You can learn more about HTML forms here: