It's been a while so first a short(ish) recap of the previous 3 presentations, including in more detail the Input And Output section of session 3. Then I'll discuss some useful Python Tutorials. And finally we can go on to cover some further aspects of the language.
Introducton to computing and programming. A computer is: a processor (mass of logic circuits), memory (lots of indexable bytes), and input/output logic. A computer manipulates numbers, just numbers. Certain numbers mean things to the processor, e.g. the number 43 might be recognised by the logic circuits as addition; and by input/output circuits as the character '+'.
The processor runs a program by sequentially "executing" the numbers stored in memory.
A brief intro to Python, explaining that the course focuses on Python 3.
Lots of Links to useful Online information: Generally useful Python links; Python libraries, Tutorials, Cheat sheets and free to download books.
How to go about coding Python: IDEs, editors, debuggers, tracing and converting Python 2 to 3.
An introduction to Python modules, and suggested module boilerplate.
How to go about programming, and an example Python program from the pygame examples shipped with Python installs.
We're now broadly following the standard python.org tutorial.
Intro to Python Lexis (keywords, symbols, names, literals, separators and comments).
Python objects and names. Everything in Python is an object (even numbers). Each object has an identity (its address in memory). Each object can have zero or more names. Mutable objects can be changed, immutable objects cannot.
Types (integer, string, list etc.) are a fundamental apsect of Python.
Sequence types (such as string, list, tuple and range) can be indexed and sliced.
Operations on strings (immutable) and lists (mutable).
Functions, including the expansion operator '*'.
A disk file consists simply of a sequence of bytes. The file is automatically extended as you write more bytes.
You can input data (text or bytes) from a file or from stdin (the text window you are running your program in).
You can output data (text or bytes) to a file or to stdout (the text window you are running your program in).
You have to open a file for reading, or for writing, before you read or write data from or to that file. And you have to close it afterwards.
There are general functions available to format the data you write, and to help interpret the data you read.
You can do line-oriented reading/writing, or just read/write text/bytes without regard for a line structure.
There are straightforward facilities for writing and reading Python objects to a dedicated file, using json. Those facilities serialise and dedeserialise the objects. Serialisation records the object data, and its structure as a series of bytes, in a way that deserialisation can reconstruct the original object.
Operations on files should always be checked for success, and failures handled and reported appropriately. File operations can so easily fail – e.g. you don't have permission to read/write the file, or you've run out of disk space.
fout = open('Fred', 'w') # 'w' opens it for writing.
fin = open('Fred') # Second parameter defaults to 'r'.
fredsdata = fin.read() # Read all the data.
Aside: for line-based files the file object is actually a generator which is like an iterator except that it generates (obtains) each value as it is required – which means the whole file doesn't get read into memory, just each line as it is needed.
Using "with" here arranges for Python to "context manage" the contained block of code in a way that is specific to the open function. It effectively guarantees the file will be closed if it was successfully opened, even if an error occurs reading from the file.
fdbs = open('dbs', 'x') # 'x' means exclusive creation.
fdbs = open('dbs', 'r+b') # binary (bytes) read/write.
fdbs.seek(0) # Position at start of file.
But print doesn't print data as bytes: it prints them as Unicode characters. So it doesn't work for writing to a binary file.
There are a surprising number of free-to-use online learning tutorials and tutorial-like material. These can be excellent resources for self-learning, and I would recommend working through one. Interactive ones are great, but the non-interactive ones come with lots of examples and you can play around with those in a Python interpreter to help your understanding and learning.
python.org tutorial Free, comprehensive, non-interactive. The standard Python Tutorial.
Hitchhiker's Guide to Python (O'Reilly) A great read, even though somewhat incomplete. It doesn't itself cover the language details, but it does have some references for that in the Learning Python section. Lots of useful references.
TutorialsPoint See FAQ for use restrictions. Interactive, though a little clunky. Covers a lot.
Codecademy Python 2. Nicely interactive ensuring you really do learn it. Doesn't cover whole of language (but they've just announced significant updates for Q1 2017).
python-course.eu by Bernd Klein. Free. Extensive. Also many other courses relating to Python, including Python 2.
Computer Science Circles (University of Waterloo) Free. Semi-interactive.
How to Think Like a Computer Scientist (Runestone) Superbly interactive, but examples sometimes don't leverage the full power of Python. Open Source. Also further Python and other courses.
Problem Solving with Algorithms and Data Structures using Python (Runestone). Superbly interactive, but again seeing examples that could better leverage the power of Python, e.g. by using while with an else part.
Non-Programmer's Tutorial for Python 3 (Wikibooks) Free, non-interactive.
A Beginner's Python Tutorial (Wikibooks) Free, non-interactive.
Hands-on Python Tutorial Free for non-commercial use.
Python Programming Tutorial (Programiz) Free (according to Programiz Facebook page), very comprehensive, non-interactive. Very clear to follow.
Pythonspot pdf Free according to their Twitter account.
Welcome to Python for you and me (Book by Kushal Das) Lots of examples.
One Day of IDLE Toying. Visual guide to IDLE.
The design of Python can be viewed as consisting of a number of layers. At the lowest level, the Python data model consists of objects, just objects. Every object has a unique identity, a type and a value. All three of those are fixed, except that value can be changed if the object is mutable.
The object's identity is implementation dependent but is typically the memory address of the object (which therefore implies that Python does not move the object around in memory).
The object's type is itself an object, and we call that the class of the object. I.e. an object is an instance of a class object. The class describes what the created object should look like.
An object's value is the full set of information it contains. That information is organised much like an ordered dictionary, i.e. as a sequence of name:value pairs.
Python allows us to define our own objects. We do that by writing a class definition, and then using that to instantiate (create) an object.
Here's a class definition, combined within a program that uses it. This has examples of: class definition; object creation; object initialisation; method definition; method invocation.
""" A trip is named and consists of points (places).
A point has a place, time and optional info.
all_trips =  # all_trips is shared by all instances.
trip = Trip("Salt Spring Island") # Creates object.