Skip to content

Instantly share code, notes, and snippets.

@alivelee
Forked from raskasa/notes-headfirst-python.md
Created December 30, 2012 05:56
Show Gist options
  • Save alivelee/4411207 to your computer and use it in GitHub Desktop.
Save alivelee/4411207 to your computer and use it in GitHub Desktop.

TABLE OF CONTENTS

  1. Meet Python - Python Basics
  2. Sharing Your Code
  3. Files and Exceptions
  4. Persistence: Saving Data to Files
  5. Comprehending Data
  6. Custom Data Objects
  7. Web Development
  8. Mobile App Development
  9. Manage Your Data: Handling Input
  10. Scaling Your Web App
  11. Dealing with Complexity: Data Wrangling
  12. Leftovers: Last but Not Least

1. MEET PYTHON - PYTHON BASICS

  • 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() 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 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() 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 use Alt-N for Next (but use Ctrl if you're on Mac).

2. SHARING YOUR CODE

  • 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 (BIFs) 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 the print() 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__.
  • IDLE Notes
    • Press F5 to "run" the code in the IDLE edit windows.
    • When you press F5 to "load" a module's code into the IDLE shell, the module's names are specifically imported into IDLE's namespace. This is a convenience when using IDLE. Within your code, you need to use the import statement explicitly.

3. FILES AND EXCEPTIONS

  • 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 a tuple, 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 the IDLE 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.

4. PERSISTENCE - SAVING DATA TO FILES

  • 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 the as 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.

5. COMPREHENDING DATA

  • 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 either sort() or sorted() to arrange your data in descending order.
  • When you have code like this:
     new_l = []
     for t in old_l: 
     	new_l.append(len(t))
    rewrite it to use a list comprehension, like this:
     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.

6. CUSTOM DATA OBJECTS: BUNDLING CODE WITH DATA

  • 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 and set, 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 then new_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.

7. WEB DEVELOPMENT

  • 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 writing CGI 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 see CGI coding errors within your browser.
  • Use cgitb.enable() to switch on CGI tracking in your CGI 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.

8. MOBILE APP DEVELOPMENT

  • The json library module lets you convert Python’s built-in types to the text-based JSON data interchange format.
  • Use json.dumps() to create a stringed version of a Python type.
  • Use json.loads() to create a Python type from a JSON string.
  • Data sent using JSON needs to have its Content-Type: set to application/json.
  • The urllib and urllib2 library modules (both available in Python 2) can be used to send encoded data from a program to a web server (using the urlencode() and urlopen() functions).
  • The sys module provides the sys.stdin, sys.stdout, and sys.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.

9. MANAGE YOUR DATA: HANLDING INPUT

  • The fieldStorage() method from the standard library’s cgi module lets you access data sent to your web server from within your CGI 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 the sqlite3 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 an SQL 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 parameterize SQL 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.

10. SCALING YOUR WEBAPP

  • 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 a get() method.
  • When responding to a POST request, implement the required functionality in a post() 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".

11. DEALING WITH COMPLEXITY: DATA WRANGLING

  • 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 the raw_input() function instead.
  • Build complex data structures by combining Python’s built-in lists, sets, and dictionaries.
  • 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.

12. LEFTOVERS: LAST BUT NOT LEAST

  • 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment