- 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. Identifiers are names that refer to data objects. The identifiers have no “type,” but the data objects that they refer to do.print()BIFdisplays 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
BIFs, 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()BIFprovides a length of some data object or count the number of items in a collection, such as a list.- The
forloop 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()BIFchecks whether an identifier refers to a data object of some specified type.- Use
defto 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.
IDLENotes- The
IDLEshell 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-Pfor Previous and useAlt-Nfor Next (but useCtrlif you're on Mac).
- The
- A
moduleis a text file that contains Python code. - The distribution utilities let you turn your module into a shareable package.
- The
setup.pyprogram provides metadata about your module and is used to build, install, and upload your packaged distribution. - Import your module into other programs using the
importstatement. - 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
importstatement. - Use
#to comment-out a line of code or to add a short, one-line comment to your program. - The built-in functions (
BIFs) have their own namespace called__builtins__, which is automatically included in every Python program. - The
range()BIFcan be used with for to iterate a fixed number of times. - Including
end=’’as a argument to theprint()BIFswitches 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. PyPiis the Python Package Index and is well worth a visit.- A
namespaceis a place in Python's memory where names exist. - Python's main namespace is know as
__main__.
- Use a "triple-quoted string"
IDLENotes- Press
F5to "run" the code in theIDLEedit windows. - When you press
F5to "load" a module's code into theIDLEshell, the module's names are specifically imported intoIDLE's namespace. This is a convenience when usingIDLE. Within your code, you need to use theimportstatement explicitly.
- Press
- Use the
open()BIFto 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
ValueErroroccurs when your data does not conform to an expected format. - An
IOErroroccurs when your data cannot be accessed properly (e.g., perhaps your data file has been moved or renamed). - The
help()BIFprovides access to Python’s documentation within theIDLEshell. - The
find()method locates a specific substring within another string. - The not keyword negates a condition.
- The
try/exceptstatement 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
nullstatement; 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.
IDLENotes- 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()BIFcontrols where data is sent/saved. - The finally suite is always executed no matter what exceptions occur within a
try/exceptstatement. - An exception object is passed into the except suite and can be assigned to an identifier using the as keyword.
- The
str()BIFcan be used to access the stringed representation of any data object that supports the conversion. - The
locals()BIFreturns 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
withstatement uses theaskeyword, too. sys.stdoutis what Python calls “standard output” and is available from the standard library’s sys module.- The standard library’s
picklemodule 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()BIFsorts most any data structure by providing copied sorting. - Pass
reverse=Trueto 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
listandset, 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
selfas its first argument. - Every attribute in a class must be prefixed with
selfin 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
cgimodule provides support for writingCGIscripts. - The standard library
globmodule is great for working with lists of filenames. - Set the executable bit with the
chmod +xcommand on Linux and Mac OS X. - The standard library
cgitbmodule, when enabled, lets you seeCGIcoding errors within your browser. - Use
cgitb.enable()to switch onCGItracking in yourCGIcode. - 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
jsonlibrary module lets you convert Python’s built-in types to the text-basedJSONdata interchange format. - Use
json.dumps()to create a stringed version of a Python type. - Use
json.loads()to create a Python type from aJSONstring. - Data sent using
JSONneeds to have its Content-Type: set toapplication/json. - The
urllibandurllib2library 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
sysmodule provides thesys.stdin,sys.stdout, andsys.stderrinput 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’scgimodule lets you access data sent to your web server from within yourCGIscript. - The standard
oslibrary includes the environ dictionary providing convenient access to your program’s environment settings. - The
SQLitedatabase system is included within Python as thesqlite3standard 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 anSQLquery 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 parameterizeSQLstatements 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.responseobject to construct a GAE web response. - Use the
self.requestobject to access form data within a GAE webapp. - When responding to a
GETrequest, implement the required functionality in aget()method. - When responding to a
POSTrequest, 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()BIFlets 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
timemodule, 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
remodule in the Python documentation. - Some popular Python web frameworks are Django, Zope, TurboGears, Web2py, and Pylons.
- An alternative to
SQLdatabases 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)