Catching Our Breath

Character     Servant ho!
He then goes underwater again. The servant in the boat steps into the water and goes under. Cut to announcer, now up to his waist in sea.

In this tutorial we review what we have covered over the last few tutorials, and lay some more groundwork for working with files and using the text editor that you organised in the last tutorial.

In recent tutorials we’ve learned quite a bit:

  • pickling basics: we learnt about a way of storing data called “pickling”. We did some basic pickling. Pickles are a way of storing data. This means that, in our trivia quiz, we’ll be able to add questions from time to time, without having to retype all of the questions we have added earlier.
  • pickling data: we took our understanding of pickles and applied it to our data. We saw how we can save the data that we were using for our game into a file for later use.
  • introspection: Python objects are able to tell you about themselves. In particular we learnt about the .__doc__ string and the dir() functions. Introspection allows us to find out stuff about the program we are working with. In some cases it can be a sort of help file by telling us some stuff about an object. Introspection is part of Python ‘for free’. It is available whenever you run Python without having to (eg) import introspection in the way we imported cPickle.
  • more on pickles: how to load data that’s been stored in a file, modify it and dump it back to the file. So we can keep data that our programs rely on in a file. The point of this tutorial was to show how the pickle can be used like a locker for your data. If you put your data into a pickle not only will it be there for you to take out later, but you can take it out, change it, then replace the old data with the new data.
  • dictionaries. Dictionaries are a little like lists, except you can map a given value to an index. In the homework, your example was to map names of your friends to their favourite game). Unlike lists, which access values by knowing their position in the list, dictionaries access values by giving the value a name, called a ‘key’. In order to use a list, you are dependent upon knowing the order of the items in the list. This can cause problems if you want to change the sort of data stored in the list. In this tutorial we saw that we couldn’t add an alternative correct answer because there was literally nowhere in our list to store it (because we’d earlier decided to consider all entries in the list after a certain point to be incorrect answers. Adding an alternative correct answer would invalidate all the existing data (eg every question that you had already written to the pickle file). Because dictionaries access values through keys they do not suffer from this problem. Position is not important for dictionaries. If you have data stored in a dictionary, then to add new categories of answer is a simple question of adding a new key to identify the new category of data; and, finally,
  • in the most recent tutorial, I told you to go get a text editor and learn the basics of how to use it. Using a text editor will allow us to store the Python code that we are writing in much the same way as we have been saving data in the other tutorials. One of the benefits of this is that we don’t need to retype code all the time. If we want to modify a function we just open the text editor and modify it – without having to retype it from scratch.

Most of the things we’ve covered here have to do with files and (therefore) the file system on your computer. In order to do further tutorials we’re going to be changing the file system, and if we do that we’ve got a problem in that we may end up deleting or changing something we didn’t want to. For that reason I’d like you to talk to a responsible adult and set up a separate folder on your hard drive specifically for these tutorials. Call it python4kids (or p4k if you are a lazy typist).

Homework: have a responsible adult set up a separate folder (also called a directory) for these tutorials.  Name it python4kids or p4k.

Homework: work out (ask someone or Google) how to change directory into this new folder so that any files you make are made here. Whenever you run Python change directory to this folder first, so that Python runs in the directory.

Homework: work out how to load files from this directory using your text editor and how to save files to this directory using your text editor.

Homework: Copy the pickle file we’ve been working with to this new directory. If you have not made any or many additions to it, you can just recreate it by running the tutorials in the directory.

If you do these things and whenever you do these tutorials you do them in that folder, saving files to that folder, then I don’t have to worry about accidentally overwriting your other data, because the only data that will be there is data we’ve created in these tutorials.

In terms of Python language features – so far we have covered quite a lot of them. We know about:

  • data: strings, numbers (integers), lists and dictionaries
  • controlling program flow: if/then/else, while, for
  • some regularly used in built functions: range, len
  • subroutines: def (functions), modules, import
  • other language features: objects, introspection

There is a big one which is still missing – classes.  We will get to classes in a few more… classes. Till then… do your homework.

3 Responses to Catching Our Breath

  1. Pingback: Linux News » Python4Kids: New Tutorial – Catching Our Breath

  2. Pingback: Links 4/5/2011: KDE Publishes 2010 Report, Red Hat Eyes $1 Billion in Revenue | Techrights

  3. Pingback: Using Images in the GUI « Python Tutorials for Kids 8+

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: