Professional Documents
Culture Documents
Outline
35.1 Introduction
35.1.1 First Python Program
35.1.2 Python Keywords
35.2 Basic Data Types, Control Statements and Functions
35.3 Tuples, Lists and Dictionaries
35.4 String Processing and Regular Expressions
35.5 Exception Handling
35.6 Introduction to CGI Programming
35.7 Form Processing and Business Logic
35.8 Cookies
35.9 Database Application Programming Interface (DB-API)
35.9.1 Setup
35.9.2 Simple DB-API Program
35.10 Operator Precedence Chart
35.11 Web Resources
• Python
– Interpreted
– Cross-platform
– Object-oriented
– Large-scale Internet search engines
– Small administration scripts
– GUI applications
– CGI scripts
– Freely distributed
• Python
– Can be executed on a program stored in a file
– Can run in interactive mode
• Users enter lines of code one at a time
• Enables programmers to test small blocks of code quickly
• Python is case-sensitive
• Keywords can be obtained from keyword module
Python
keywords
and continue else for import not raise
assert def except from in or return
break del exec global is pass try
class elif finally if lambda print while
Fig. 35.3 Python keywords.
Escape Meaning
sequence
\n Newline (line feed).
\r Carriage return.
\t Tab.
\' Single quote.
\" Double quote.
\b Backspace.
\\ Backslash.
Fig. 35.6 Escape sequences.
• list
– A sequence of related data
• Tuple
– A list whose elements may not be modified
– Immutable
– Singleton
• One-element tuple
• Dictionary
– A list of values that are accessed through their associated
keys
The raw dictionary data is: {12: 'December', 11: 'November', 10: 'October', 9:
'September', 8: 'August', 7: 'July', 6: 'June', 5: 'May', 4: 'April', 3: 'March',
2: 'February', 1: 'January'}
Method Description
clear() Deletes all items from the dictionary.
copy() Creates a copy of the dictionary.
get( key [, falseValue] ) Returns the value associated with key. If key is not
in the dictionary and if falseValue is specified,
returns the specified value.
has_key( key ) Returns 1 if key is in the dictionary; returns 0 if key
is not in the dictionary.
items() Returns a list of tuples that are key-value pairs.
keys() Returns a list of keys in the dictionary.
setdefault( key [, Behaves similarly to method get. If key is not in
falseValue] ) the dictionary and falseValue is specified, inserts
the key and the specified value into the dictionary.
update( otherDictionary ) Adds all the key-value pairs from otherDictionary
to the current dictionary.
values() Returns a list of values in the dictionary.
Fig. 35.9 Dictionary methods.
Symbol Meaning
c Single character (i.e., a string of length 1).
s String.
d Signed decimal integer.
u Unsigned decimal integer.
o Unsigned octal integer.
x Unsigned hexadecimal integer (using format abcdef).
X Unsigned hexadecimal integer (using format ABCDEF).
f Floating-point number.
e, E Floating-point number (using scientific notation).
g, G Floating-point number (using least-significant digits).
Fig. 35.11 String-format characters.
• XHTML forms
– Allow users to enter data to be sent to a Web server for
processing
60
61 <span style = "color: blue">Name:</span> %s<br />
62 <span style = "color: blue">Height:</span> %s<br />
63 <span style = "color: blue">Favorite Color:</span>
64 <span style = "color: %s"> %s</span><br />""" \
65 % ( name, height, color, color )
66
67 print """<br /><a href= "fig35_21.py">
68 Read cookie values</a>"""
69
70 print """</body></html>"""