- Meet Python - Python Basics
- Sharing Your Code
- Files and Exceptions
- Persistence: Saving Data to Files
- Comprehending Data
- Custom Data Objects
- Web Development
- Mobile App Development
- Manage Your Data: Handling Input
- Scaling Your Web App
- Dealing with Complexity: Data Wrangling
- Leftovers: Last but Not Least
- Run Python 3 from the command line or from within
IDLE
. Identifier
s are names that refer to data objects. The identifiers have no “type,” but the data objects that they refer to do.print()
BIF
displays a message on screen.- A list is a collection of data, separated by commas and surrounded by square brackets.
- Lists are like arrays on steroids.
- Lists can be used with
BIF
s, but also support a bunch of list methods. - Lists can hold any data, and the data can be of mixed type. Lists can also hold other lists.
- Lists shrink and grow as needed. All of the memory used by your data is managed by Python for you.
- Python uses indentation to group statements together.
len()
BIF
provides a length of some data object or count the number of items in a collection, such as a list.- The
for
loop lets you iterate a list and is often more convenient to use that an equivalent while loop. - The
if... else...
statement lets you make decisions in your code. isinstance()
BIF
checks whether an identifier refers to a data object of some specified type.- Use
def
to define a custom function. - Python Lingo
BIF
- a built-in function.Suite
- a block of Python code, which is indented to indicate grouping.Batteries included
- a way of referring to the fact that Python comes with most everything you'll need to get going quickly and productively.
IDLE
Notes- The
IDLE
shell lets you experiment with your code as your write it. - Adjust
IDLE
's preferences to suit the way you work. - Remember: when working with the shell, use
Alt-P
for Previous and useAlt-N
for Next (but useCtrl
if you're on Mac).
- The
- A
module
is a text file that contains Python code. - The distribution utilities let you turn your module into a shareable package.
- The
setup.py
program provides metadata about your module and is used to build, install, and upload your packaged distribution. - Import your module into other programs using the
import
statement. - Each module in Python provides its own namespace, and the namespace name is used to qualify the module’s functions when invoking them using the
module.function()
form. - Specifically import a function from a module into the current namespace using the from module import function form of the
import
statement. - Use
#
to comment-out a line of code or to add a short, one-line comment to your program. - The built-in functions (
BIF
s) have their own namespace called__builtins__
, which is automatically included in every Python program. - The
range()
BIF
can be used with for to iterate a fixed number of times. - Including
end=’’
as a argument to theprint()
BIF
switches off its automatic inclusion of a new-line on output. - Arguments to your functions are optional if you provide them with a default value.
- Python Lingo
- Use a "triple-quoted string"
"""
to include a multiple-line comment in your code. PyPi
is the Python Package Index and is well worth a visit.- A
namespace
is a place in Python's memory where names exist. - Python's main namespace is know as
__main__
.
- Use a "triple-quoted string"
IDLE
Notes- Press
F5
to "run" the code in theIDLE
edit windows. - When you press
F5
to "load" a module's code into theIDLE
shell, the module's names are specifically imported intoIDLE
's namespace. This is a convenience when usingIDLE
. Within your code, you need to use theimport
statement explicitly.
- Press
- Use the
open()
BIF
to open a disk file, creating an iterator that reads data from the file one line at a time. - The
readline()
method reads a single line from an opened file. - The
seek()
method can be used to “rewind” a file to the beginning. - The
close()
method closes a previously opened file. - The
split()
method can break a string into a list of parts. - An unchangeable, constant list in Python is called a
tuple
. Once list data is assigned to atuple
, it cannot be changed. Tuples are immutable. - A
ValueError
occurs when your data does not conform to an expected format. - An
IOError
occurs when your data cannot be accessed properly (e.g., perhaps your data file has been moved or renamed). - The
help()
BIF
provides access to Python’s documentation within theIDLE
shell. - The
find()
method locates a specific substring within another string. - The not keyword negates a condition.
- The
try/except
statement provides an exception-handling mechanism, allowing you to protect lines of code that might result in a runtime error. - The pass statement is Python’s empty or
null
statement; it does nothing. - Python Lingo
- An "exception" occurs as a result of a runtime error, producing a traceback.
- A "traceback" is a detailed description of the runtime error that has occured.
IDLE
Notes- Access Python's documentation by choosing Python Docs from IDLE's Help menu. The Python 3 documentation set should open in your favorite web browser.
- The
strip()
method removes unwanted whitespace from strings. - The file argument to the
print()
BIF
controls where data is sent/saved. - The finally suite is always executed no matter what exceptions occur within a
try/except
statement. - An exception object is passed into the except suite and can be assigned to an identifier using the as keyword.
- The
str()
BIF
can be used to access the stringed representation of any data object that supports the conversion. - The
locals()
BIF
returns a collection of variables within the current scope. - The in operator tests for membership.
- The
+
operator concatenates two strings when used with strings but adds two numbers together when used with numbers. - The with statement automatically arranges to close all opened files, even when exceptions occur. The
with
statement uses theas
keyword, too. sys.stdout
is what Python calls “standard output” and is available from the standard library’s sys module.- The standard library’s
pickle
module lets you easily and efficiently save and restore Python data objects to disk. - The
pickle.dump()
function saves data to disk. - The
pickle.load()
function restores data from disk. - Python Lingo
- "Immutable types" - data types in Python that, once assigned a value, cannot have that value changed.
- "Pickling" - the process of saving a data object to persistence storage.
- "Unpickling" - the process of restoring a saved data object from persistence storage.
- The
sort()
method changes the ordering of lists in-place. - The
sorted()
BIF
sorts most any data structure by providing copied sorting. - Pass
reverse=True
to eithersort()
orsorted()
to arrange your data in descending order. - When you have code like this:
rewrite it to use a list comprehension, like this:
new_l = [] for t in old_l: new_l.append(len(t))
new_l = [len(t) for t in old_l]
- To access more than one data item from a list, use a slice. For example:
my_list[3:6]
accesses the items from index location 3 up-to-but-not-including index location 6.
- Create a set using the
set()
factory function. - Python Lingo
- "In-place" sorting - transforms and then replaces.
- "Copied" sorting - transforms and then returns.
- "Method Chaining" - reading from left to right, applies a collection of methods to data.
- "Function Chaining" - reading from right to left, applies a collection of functions to data.
- "List Comprehension" - specify a transformation on one line (as opposed to using an iteration).
- A "slice" - access more than one item from a list.
- A "set" - a collection of unordered data items that contains no duplicates.
- Create a empty dictionary using the
dict()
factory function or using{}
. - To access the value associated with the key Name in a dictionary called person, use the familiar square bracket notation:
person['Name']
. - Like
list
andset
, a Python’s dictionary dynamically grows as new data is added to the data structure. - Populate a dictionary as you go:
new_d = {} or new_d = dict()
and thennew_d['Name'] = 'Eric Idle'
or do the same thing all in the one go:new_d = {'Name': 'Eric Idle'}
- The class keyword lets you define a class.
- Class methods (your code) are defined in much the same way as functions, that is, with the def keyword.
- Class attributes (your data) are just like variables that exist within object instances.
- The
__init__()
method can be defined within a class to initialize object instances. - Every method defined in a class must provide
self
as its first argument. - Every attribute in a class must be prefixed with
self
in order to associate it data with its instance. - Classes can be built from scratch or can inherit from Python’s built-in classes or from other custom classes.
- Classes can be put into a Python module and uploaded to
PyPI
. - Python Lingo
- "Dictionary" - a built-in data structure that allows you to associate data values with keys.
- "Key" - the look-up part of the dictionary.
- "Value" - the data part of the dictionary (which can be any value, including another data structure).
- "self" - a method argument that always refers to the current object instance.
- The Model-View-Controller pattern lets you design and build a webapp in a maintainable way.
- The model stores your webapp’s data.
- The view displays your webapp’s user interface.
- The controller glues everything together with programmed logic.
- The standard library string module includes a class called Template, which supports simple string substitutions.
- The standard library http.server module can be used to build a simple web server in Python.
- The standard library
cgi
module provides support for writingCGI
scripts. - The standard library
glob
module is great for working with lists of filenames. - Set the executable bit with the
chmod +x
command on Linux and Mac OS X. - The standard library
cgitb
module, when enabled, lets you seeCGI
coding errors within your browser. - Use
cgitb.enable()
to switch onCGI
tracking in yourCGI
code. - Use
cgi.FieldStorage()
to access data sent to a web server as part of a web request; the data arrives as a Python dictionary. - Python Lingo
- "@property" - a decorator that lets you arrange for a class method to appear as if it is a class attribute.
- Web Lingo
- "webapp" - a program that runs on the Web.
- "web request" - sent from the web browser to the web server.
- "web response" - sent from the web server to the web browser in response to a web request.
- "CGI" - the Common Gateway Interface, which allows a web server to run a server-side program.
- "CGI script" - another name for a server-side program.
- The
json
library module lets you convert Python’s built-in types to the text-basedJSON
data interchange format. - Use
json.dumps()
to create a stringed version of a Python type. - Use
json.loads()
to create a Python type from aJSON
string. - Data sent using
JSON
needs to have its Content-Type: set toapplication/json
. - The
urllib
andurllib2
library modules (both available in Python 2) can be used to send encoded data from a program to a web server (using theurlencode()
andurlopen()
functions). - The
sys
module provides thesys.stdin
,sys.stdout
, andsys.stderr
input streams. - Python Lingo
- "Python 2" - the previous release of Python, which has compatibility "issues" with Python 3 (and are not worth getting worked up over).
- Android Lingo
- "SL4A" - the Scripting Layer for Android lets you run Python on your Android device.
- "AVD" - an Android Virtual Device which lets you emulate your Android device on your computer.
- The
fieldStorage()
method from the standard library’scgi
module lets you access data sent to your web server from within yourCGI
script. - The standard
os
library includes the environ dictionary providing convenient access to your program’s environment settings. - The
SQLite
database system is included within Python as thesqlite3
standard library. - The
connect()
method establishes a connection to your database file. - The
cursor()
method lets you communicate with your database via an existing connection. - The
execute()
method lets you send anSQL
query to your database via an existing cursor. - The
commit()
method makes changes to your database permanent. - The
rollback()
method cancels any pending changes to your data. - The
close()
method closes an existing connection to your database. - The
?
placeholder lets you parameterizeSQL
statements within your Python code. - Python Lingo
- "Database API" - a standardized mechanism for accessing an SQL-based database system from within a Python program.
- Database Lingo
- "Database" - a collection of one or more tables
- "Table" - a collection of one or more rows of data, arranged as one or more columns.
- "SQL" - the "Structured Query Language" is the language of the database world and it lets you work with your data in your database using statements such as CREATE, INSERT, and SELECT.
- Every AppEngine webapp must have a configuration file called
app.yaml
. - Use the GAE Launcher to start, stop, monitor, test, upload, and deploy your webapps.
- App Engine’s templating technology is based on the one use in the Django Project.
- App Engine can also use Django’s Form Validation Framework.
- Use the
self.response
object to construct a GAE web response. - Use the
self.request
object to access form data within a GAE webapp. - When responding to a
GET
request, implement the required functionality in aget()
method. - When responding to a
POST
request, implement the required functionality in apost()
method. - Store data in the App Engine datastore using the
put()
method. - App Engine Lingo
- "Datastore" - the data repository used by Google App Engine to permanently store your data.
- "Entity" - the name used for a "row of data".
- "Property" - the name used for a "data value".
- The
input()
BIF
lets you prompt and receive input from your users. - If you find yourself using Python 2 and in need of the
input()
function, use theraw_input()
function instead. - Build complex data structures by combining Python’s built-in
lists
,sets
, anddictionaries
. - The
time
module, which is part of the standard library, has a number of functions that make converting between time formats possible. - Python Lingo
- A "conditional" list comprehension is on that includes a trailing "if" statement, allowing you to control which items are added to the new list as the comprehension runs.
- List comprehensions can be rewritten as an equivalent "for" loop.
- Learn to use a professional IDE: WingWare Python IDE (for Windows) and KDevelop IDE (for Linux) are good choices.
- Python has 2 testing frameworks in the standard library: "unittest" (based on the popular xUnit testing framework) and "doctest".
- Take time to form an understanding of the following concepts:
- Anonymous functions
- Generators
- Custom exceptions
- Function decorators
- Metaclasses
- Python supports regular expressions--to learn more search the
re
module in the Python documentation. - Some popular Python web frameworks are Django, Zope, TurboGears, Web2py, and Pylons.
- An alternative to
SQL
databases are ORM's (object relational mappers) and NoSQL datbases. - ORM's provide an object-oriented interface to your data, exposing it via method calls and attribute lookups as opposed to columns and rows.
- A populat ORM is SQL Alchemy--it supports both Python 2 and 3. Google App Engine's datastore API is very similar to ORM.
- NoSQL provides a non-SQL API's to your data. If you like working with your data in a Python dictionary and wished your database technology let you store data in much the same way, then NoSQL is for you.
- Popular NoSQL databases are MongoDB and CouchDB.
- Python supports programming of GUI's for native apps with the pre-installed "tkinter". There are also PyGTK, PyKDE, wxPython, and PyQT (but their main support is for Python 2).
- Threads in Python are available, but DON'T use them if you don't have too.
- This has everything to do with Python's implementation (Global Interpreter Lock (GIL)).
- GIL enforces a retriction that Python can only ever run on a single interpreter process, even in the presence of multiple processors.
- Basically, your threaded program will NEVER run faster on multi-processor machines because it CAN'T use them.
- Recommended books
- Dive Into Python 3 (includes a greate case study involving the porting of a complex Python 2 module to Python 3)
- Learning Python (at 1,200 pages, this is the definitive language referene for Python: it's got everythin in it)
- Python Essential Reference (the best desktop reference on the market)
- Python for Unix and Linux System Administration (if you are a sysadmin, then this is the Python book for you)
- Programming Python 3 (includes some big examples with big technologyL XML, parsing, and advances language features)