Lab 12: Python Lab

This is the archived website of SI 413 from the Fall 2012 semester. Feel free to browse around; you may also find more recent offerings at my teaching page.

This lab is due at 0900 next Tuesday, November 27. You should submit files for each problem you solve, each named something like p001.py or similar. You do not have to submit any tests for this lab.

Overview

This lab is all about the programming language Python. In this lab, you are going to try out some new ideas in this new programming language. Python is a scripting language that was invented relatively recently, and it was designed above all else to be easy to use and easy to learn, while also being very powerful.

Python is also designed to let you communicate easily using programs. There is a saying in python that "readability counts". In other words, your programs should be self-documenting. The language designers want you to write programs that are simple and easy to understand. They have included a number of interesting language features to help make that possible.

Your instructor will be an excellent resource in learning Python, but there are also a number of fantastic documentation sources online. The Python Tutorial is a pretty good one, and there is also the more terse and to-the point library reference and language details. Just be sure when you find information online that it works for Python 3, which is the newer version of the language that we will be using for this lab.

Tutorial/walkthrough

As a super-fast tutorial and intro, I've made a program intro.py to give you some examples of a few of the basic features of the langauge. Go ahead and download it, read it, run it, play with it, and refer to it as you go along. Really, go ahead!

The problems!

In this lab, you are going to teach yourself Python in the same way that your instructor did just a few years ago: by solving the simple programming problems from the excellent website projecteuler.net. This is a website that posts math-type problems that you pretty much need a computer to solve. You write a program to get the solution (or do whatever else you have to do), then type it in the box and get a pat on the back if the answer is correct.

There are literally hundreds of problems on their site, and they get progressively more difficult as you go along. I want you to start with the very first problem and see how far you can get. Every problem on the site has a single correct answer which is just a number (an integer, actually).

You may not search for solutions to these problems online. You may only search online for websites with help on using Python, or background information on the basic math concepts. You must document all resources (specific websites) in comments in the files that you submit.

You must write a Python program to solve each problem. Your program to solve, say, problem 5, must be called p005.py. When I run it like

python3 p005.py
it should only print out a single integer, which is the final, correct answer to that problem. If you insist on printing out other stuff, then be sure that the last line of what the program prints out is the answer.

You should try to complete problems 1-8 at least. Beyond that, you can submit solutions to any of the other problems that you are able to get to. Bonus points will be available, and there will be a tangible prize for whoever gets the most. Since you will be judged on style as well as correctness, try to write your solution programs so that:

The Project Euler website has a whole system for you to make an account and submit your answers etc. You do not need to do this, but you might want to (it's free and will tell you whether you have the right answer for any problem right away).