Python Tutorial

« Previous | Next »

The Python Tutorial is an optional part of 6.01. Students with Python programming experience can skip this section and proceed to Unit 1.

Learning Python

You should be familiar with the basics of programming before starting 6.01. These exercises are to make sure that you have enough familiarity with programming and, in particular, Python programming.

This is not meant to be a stand-alone introduction to computer programming. Rather, it’s a way for someone with some previous exposure to programming to get some practice and to learn the basics of Python. The goal of the exercises is to give you practice with Python concepts and to help diagnose your level of programming ability.

Some of the later problem sets are much longer than the earlier ones, because we need the concepts in the earlier sections before we can really write many interesting programs. So, don’t be misled by the short length of the early problem sets.

Python Documentation

Each section of this tutorial includes notes on Python written for 6.01, as well as pointers to chapters of Think Python: How to Think Like a Computer Scientist.

Another useful reference for Python details is the official Python Tutorial.

Software

For 6.01, we’ll be using Python 2.6.x and the IDLE environment for editing and executing Python code (although some people may prefer to use Emacs for editing code). See the Software and Tools page for more details.

Tutorial Sections

Types, values, expressions; variables and binding

Functions and scope

Using if, else, and while

Quadratic roots

Loops and list comprehensions

Arrays as lists of lists

Association lists

« Previous | Next »

« Previous | Next »

Python doesn’t have a formal array data type (although there are some very nice libraries for numerical computation, such as numpy, that support arrays). But we can make two-dimensional arrays using lists of lists, which we’ll explore in the next few problems.

The problems in the table below are taken from the 6.01 Python Tutor, an interactive environment that is not available on OCW. Do not try to answer these questions in the PDF files; answers will not be checked, and cannot be submitted.

PROBLEM # QUESTIONS
6.1.1 What a bunch of zeros (PDF)
6.1.2 Setting an element of a vector (PDF)
6.1.3 A bigger bunch of zeros (PDF)
6.1.4 Setting an element of an array (PDF)
6.1.5 Add ’em up (PDF)

« Previous | Next »

« Previous | Next »

The idea of a dictionary is very important and useful in programming. A dictionary allows you to associate values with keys. In an actual English dictionary, the words are the keys and the definitions are the values. So, given a key, you can look up the value. In a phone book, names are the keys and phone numbers are the values.

Python has a built-in dictionary data structure, which you have read about already. We can make our own, simpler and less efficient data structure as a list of lists. It operates like this:

d = emptyAlist()
>» addEntry(d, 4, 5)
>» addEntry(d, 5, None)
>» addEntry(d, ‘ben’, ‘boa’)
>» addEntry(d, ‘kim’, ‘krait’)
>» d
[[4, 5], [5, None], [‘ben’, ‘boa’], [‘kim’, ‘krait’]]
>» addEntry(d, ‘bella’, ‘bi-colored-python-rock-snake’)
>» d
[[4, 5], [5, None], [‘ben’, ‘boa’], [‘kim’, ‘krait’], [‘bella’, ‘bi-colored-python-rock-snake’]]
>» lookup(d, ‘ben’)
[‘ben’, ‘boa’]
>» lookup(d, ‘biz’)

The problems in the table below are taken from the 6.01 Python Tutor, an interactive environment that is not available on OCW. Do not try to answer these questions in the PDF files; answers will not be checked, and cannot be submitted.

PROBLEM # QUESTIONS
7.1.1 Add an entry to an alist (PDF)
7.1.2 Look up an entry in an alist (PDF)

« Previous | Next »

« Previous | Next »

Before starting these problems, please read:

The problems in the table below are taken from the 6.01 Python Tutor, an interactive environment that is not available on OCW. Do not try to answer these questions in the PDF files; answers will not be checked, and cannot be submitted.

PROBLEM # QUESTIONS
2.1.1 Fun with functions (PDF)
2.1.2 Functions and scope (PDF)
2.1.3 Scoping (PDF)
2.2.1 Square (PDF)
2.2.2 Fourth power (PDF)
2.2.3 Odd test (PDF)
2.2.4 From point A to point B (PDF)
2.2.5 Evaluate a quadratic at a point (PDF)
2.2.6 Distance from point to line (PDF)

« Previous | Next »

« Previous | Next »

Before starting these problems, please read:

The problems in the table below are taken from the 6.01 Python Tutor, an interactive environment that is not available on OCW. Do not try to answer these questions in the PDF files; answers will not be checked, and cannot be submitted.

PROBLEM # QUESTIONS
5.1.1 More expressions (PDF)
5.1.2 Ranger (PDF)
5.2.1 From point A to point B (PDF)
5.2.2 Distance from point to line (PDF)
5.3.1 Multiple times (PDF)
5.3.2 Every other (PDF)
5.3.3 Cat 6 (PDF)
5.3.4 Home on the range (PDF)
5.3.5 The mean of a list of numbers (PDF)
5.3.6 Standard deviation (PDF)
5.3.7 Dictionaries (PDF)
5.3.8 Slice of Pi (PDF)
5.3.9 Polly (PDF)
5.3.10 As close as necessary (PDF)

« Previous | Next »

« Previous | Next »

In this section, we’ll look at computing both real and complex roots of quadratic equations.

The problems in the table below are taken from the 6.01 Python Tutor, an interactive environment that is not available on OCW. Do not try to answer these questions in the PDF files; answers will not be checked, and cannot be submitted.

PROBLEM # QUESTIONS
4.1.1 Get real (PDF)
4.1.2 More complex (PDF)

« Previous | Next »

« Previous | Next »

Before starting these problems, please read:

The problems in the table below are taken from the 6.01 Python Tutor, an interactive environment that is not available on OCW. Do not try to answer these questions in the PDF files; answers will not be checked, and cannot be submitted.

PROBLEM # QUESTIONS
1.1.1 Basic types (PDF)
1.1.2 Numeric expressions (PDF)
1.1.3 Relations and boolean expressions (PDF)
1.1.4 Mixed-type expressions (PDF)
1.2.1 Variable binding (PDF)

« Previous | Next »

« Previous | Next »

Before starting these problems, please read:

The problems in the table below are taken from the 6.01 Python Tutor, an interactive environment that is not available on OCW. Do not try to answer these questions in the PDF files; answers will not be checked, and cannot be submitted.

PROBLEM # QUESTIONS
3.1.1 If statements (PDF)
3.1.2 Compare (PDF)
3.1.3 Arithmetic if (PDF)
3.1.4 Clipping shears (PDF)
3.1.5 Clip clop (PDF)
3.2.1 Multiple times (PDF)
3.2.2 Multiple times, again (PDF)
3.2.3 A la mod (PDF)
3.2.4 Muad-div (PDF)
3.2.5 Primo (PDF)
3.2.6 Powers of 2 (PDF)
3.2.7 Perfectly square (PDF)

« Previous | Next »

Learning Resource Types
Lecture Videos
Recitation Videos
Problem Sets
Exams with Solutions
Lecture Notes
Instructor Insights
Programming Assignments
Exams