Skip to: Site menu | Main content

CS4HS 2011: Python Workshop

Hi everyone! Welcome to the Python workshop. I set up this webpage to help you prepare your laptop for the workshop, and to hold the workshop materials and resources once we're finished. If you have any questions, please feel free to contact me!

In our four hours, we'll learn most of the core of Python, and work together on solving some fun programming problems. With our remaining time, we'll look at one problem domain in particular: processing sounds stored as wav files. I'll demonstrate how easy it is to do some really neat things with sounds and hopefully give you some ideas for some cool assignments.

Preparation

I'd prefer that you use your own laptops during the workshop for a couple of reasons:

  • I'd like you to install your own Python environment, so that you know how it's done, and so that you can mess around a little prior to the workshop (see below)
  • I'd like you to use your favourite OS, and a way of accessing Python with which you are comfortable

I'm told that wireless access will be available for you with your laptop. For the record, you'll be fine with no wireless, as long as you follow the steps below.

Don't have a laptop? Don't worry... we're in a lab, with lab computers available. Or maybe someone can work with me on my laptop. We'll see!

Install Python

Please install Python on your laptop. The instructions in the following links are excellent. However, please do not install Python 2.7, but instead install Python 2.6. If you already have Python 2.5, that's fine too.

Install a Text Editor

You can use whatever text editor you like. On Windows, the built-in Notepad will do fine. In case you want something new, my all-time favourite editor on Windows is Metapad.

Make Sure you can Start Python

Run Python:

Goal 4 on this Python guide might be helpful if you aren't familiar with the commandline. Please work through goal 5 as well, which teaches you how to run a Python script from the commandline. They use Notepad++ (ignore that), and they recommend using the desktop directory in Windows (but feel free to create a new directory instead, as long as you know how to get to it from the command prompt). In the final step below, you're going to drop some Python and wav files into whichever directory you've decided to use, be it your desktop or some other directory.

Dependencies

There are two Python packages that I'd like you to install. They're used by our sound module below, for when we get to playing around with digital sounds. The first is called Numpy; the second is called Pygame.

First, install Numpy. Be sure to get the version for Python 2.6 (which you installed above).

Next, install Pygame.

Linux note: From one of our participants: on Debian Linux distros, try sudo apt-get install python-pygame to automatically install Pygame and Numpy.

Finally, please download all of the following files to a directory in which you will work (i.e. a directory on your desktop, or somewhere else, where you will place these files and all Python files you work on during the workshop):

Make sure that you can do the following from within Python, with no errors. Be sure you invoke Python from the directory in which you placed the files, and that you have speakers or headphones so that you can hear sound output.

import sound_media
snd = sound_media.load_sound ('cde.wav')
sound_media.play(snd)

If you have any problems at this step (e.g. sound not playing, Python errors, etc.), please let me know.

Done!

Alright, that's it. Please let me know that you're ready (or that you're having a problem). Talk to you Wednesday!

Materials