Introduction to Python

This is a collection of notebooks that form the "lecture" notes for an Introduction to Programming class that I teach each year.

I put "lecture" in quotes because these are really presented as brief mini-lessons. After participating in a mini-lesson, students are presented with a set of exercises and challenges that ask them to use and apply what they have just learned. Students have access to the notebooks as they are working through the exercises and challenges.

This approach aims to maximize the time students spend applying what they have seen, which is the fun part of programming. This approach minimizes the time students spend asking me to repeat what I showed them in the lesson, which is the boring part of teaching and learning.

Syllabus

The syllabus for the course.

Syllabus notebook

Programming Environment

We use Ubuntu Linux as our main programming environment, for a number of reasons. This page covers a number of things we need to be aware of to help use and maintain a Linux computer.

Programming Environment notebook

top

Python Essentials

Hello World

The classic "Hello world" program in Python. Use this to make sure your computer is set up to run Python programs properly.

Hello World notebook

Variables, Strings, and Numbers

In this section, you will learn to store information in variables. You will learn about two types of data: strings, which are sets of characters, and numerical data types.

Variables Strings Numbers noteboook

Lists and Tuples

In this notebook, you will learn to store more than one valuable in a single variable. This by itself is one of the most powerful ideas in programming, and it introduces a number of other central concepts such as loops. If this section ends up making sense to you, you will be able to start writing some interesting programs, and you can be more confident that you will be able to develop overall competence as a programmer.

Lists and Tuples notebook

Introducing Functions

One of the core principles of any programming language is, "Don't Repeat Yourself". If you have an action that should occur many times, you can define that action once and then call that code whenever you need to carry out that action.

Introducing Functions notebook

If Statements

By allowing you to respond selectively to different situations and conditions, if statements open up whole new possibilities for your programs. In this section, you will learn how to test for certain conditions, and then respond in appropriate ways to those conditions.

If Statements notebook

While Loops and Input

While loops are really useful because they let your program run until a user decides to quit the program. They set up an infinite loop that runs until the user does something to end the loop. This section also introduces the first way to get input from your program's users.

While Loops and Input notebook

Basic Terminal Apps

By this point, you have learned enough Python to start building interactive apps. If you are set on the larger projects such as building a video game, making a visualization, or making a web app, you can skip this section. But if you would like to start building some simpler apps that run directly in your terminal, check out this notebook.

Terminal apps are a great way to build the core functionality of a program you are interested in, which can then be extended into a more accessible format.

Basic Terminal Apps notebook

Dictionaries

Dictionaries allow us to store connected bits of information. For example, you might store a person's name and age together.

Dictionaries notebook

More Functions

Earlier we learned the most bare-boned versions of functions. In this section we will learn more general concepts about functions, such as how to use functions to return values, and how to pass different kinds of data structures between functions.

More Functions notebook

Classes

So far we have learned about Python's core data types: strings, numbers, lists, tuples, and dictionaries. In this section we learn about the last major data structure, classes. Classes are quite unlike the other data types, in that they are much more flexible. Classes allow you to define the information and behavior that characterize anything you want to model in your program. Classes are a rich topic, so we will learn just enough here to dive into the projects you'd like to get started on.

Classes notebook

Exceptions

Every program has errors while it runs, and has to deal with unexpected behavior and bad input. Exceptions provide a clean way to deal with these situations.

Exceptions notebook

Testing

The programs that we write can quickly become complicated, and with complexity comes bugs. Programmers have long accepted that every program will have its share of bugs. To deal with this, we have come up with many ways to defend against avoidable bugs. Learning to write tests is one of the best things you can do to guard against avoidable bugs creeping into your own programs. Basically, you write a set of tests that you then run against your program every time you or someone else changes the program. These tests, if written correctly, prove that changes to the code do not break anything that was working previously.

Testing notebook

top

Projects - Data Visualization

Mapping Global Earthquake Activity

This project introduces the Basemap library, which can be used to create maps and plot geographical datasets.

Mapping Global Earthquake Activity notebook

top