Head First Python is a book on Python programming written by Paul Barry and published by O’Reilly. This post has some notes from the book. The book itself has much more information as well as helpful examples.
Meet Python:
• The application IDLE lets you write and run Python code.
• Create a list by:
1. converting names into strings by surrounding the data with quotes
2. separating each of the list items with a comma
3. surrounding the list of items with opening and closing square brackets
4. assigning the list to an identifier using the assignment operator: =
• You can display list items with print()
• Use remove() and insert() to remove and add data items to specific locations in a list.
• Python lists can contain data of mixed type (strings, numbers, etc).
• Python’s “for” and “while” statements are useful for processing lists and other iterations.
• Lists can be stored within lists.
• With repeating code, it’s good to create a reusable function that you can invoke whenever you need instead of copying and pasting code over and over again.
• The def statement is used to define a Python function.
Sharing Your Code:
• A module is a text file ending in .py that contains Python code that can be shared with others.
• Use a triple quote for multiple-line comments in Python code.
• Prepare a distribution by:
1. creating a folder for your module
2. creating a file with metadata about your distribution
3. building a distribution file
• Use the import statement to include a module in your program.
• Qualify the names of functions with the correct namespace so Python knows to look in the correct place.
• The PyPI community site has many useful modules available to download.
• Python has over 70 built in functions that provide solutions to common problems.
• You can turn a required argument to a function into an optional argument by providing the argument with a default value by specifying the default value after the argument name.
Files and Exceptions:
• Two potential ways of dealing with runtime errors:
1. adding extra logic that’s required
2. identifying that an error occurred and then recovering from it
• The traceback tells you that something unexpected occurred during runtime.
• Python’s exception handling lets the error occur and then gives you an opportunity to recover.
• The try statement is a way to catch exceptions and errors at runtime.
• The pass statement is a way to handle exceptions.
• The os module has ways to determine whether a data file exists.
Persistence:
• When you use open() to work with a disk file, you can specify an access mode to use (reading or writing)
• Use out.close() when you’re done to ensure all of your data is written to disk.
• print() displays your processed data on the screen.
• When an error occurs at runtime, Python creates an exception object.
• The with statement makes it so that you no longer have to worry about closing any opened files.
• Python ships with the pickle library, which can save and load Python data objects.
Comprehending Data:
• The sort() method is used for in-place sorting.
• The sorted() function supports copied sorting.
• The list comprehension tool reduces the code needed when transforming one list into another.
• The set data structure can be used to remove duplicate data items.
Custom Data Objects:
• The Python dictionary lets you associate data with keys instead of numbers.
• You can define the code and the data it works on as a class. You can then use the class to create data objects.
• Each class has a method called _init_() that lets you control how objects are initialized.
• Python arranges for the first argument of every method to be the invoking object instance.
• When you invoke a class method on an object instance, Python arranges for the first argument to be the invoking object instance, which is always assigned to each method’s self argument. This fact alone explains why self is so important and also why self needs to be the first argument to every object method you write.
• Python’s class lets you either create a class from scratch or create a class by inheriting from any other existing class, including Python’s built-in data structure classes.
Web Development:
• The Model-View-Controller pattern helps divide a webapp’s code into manageable components.
• An example of a MVC pattern in Python includes data in a pickle file (model), a template engine that generates HTML (view), and controller code.
• Python comes with a built-in web server.
Mobile App Development:
• Scripting Layer for Android lets you run Python on any Android device.
• To get started:
1. configure the Android SDK and emulator
2. install and configure Android Scripting
3. add Python to the SL4A installation
• You can request data using Python in an Android app by using JSON.
Manage Your Data:
• Use the <FORM> and <INPUT> tags within an HTML web page or use a call to the dialogGetInput() function on a mobile device to get input from users.
• Avoid race conditions by using a database management system.
• Python 3 comes preinstalled with SQLite, a SQL-based data management system.
• Steps for using a database with Python:
1. Connect to the database back end.
2. Create a cursor to communicate with your data.
3. Manipulate your data using SQL.
4. Commit the data or rollback the data.
5. Close the connection to the database back end.
Scaling Your Webapp:
• Google App Engine lets you host your webapp on Google’s servers.
• An App Engine-enabled webapp uses a datastore based on Google’s BigTable NoSQL technology.
• Google App Engine includes Django’s forms-building technology.
• Data items stored within the App Engine datastore are referred to as properties.
• You can use the SELECT/OPTION HTML tag pairing to restrict the data that can be used for fields on your form.
Dealing with Complexity:
• When it comes to linking two data items with each other, the Python dictionary is the data structure of choice.