small medium large xlarge

Practical Programming (2nd edition): An Introduction to Computer Science Using Python 3


Cover image for Practical Programming

Practical Programming (2nd edition)

An Introduction to Computer Science Using Python 3


This book is for anyone who wants to understand computer programming. You’ll learn to program in a language that’s used in millions of smartphones, tablets, and PCs. You’ll code along with the book, writing programs to solve real-world problems as you learn the fundamentals of programming using Python 3. You’ll learn about design, algorithms, testing, and debugging, and come away with all the tools you need to produce quality code. In this second edition, we’ve updated almost all the material, incorporating the lessons we’ve learned over the past five years of teaching Python to people new to programming.

Customer Reviews

I wish I could go back in time and give this book to my 10-year-old self when I
first learned programming! It’s so much more engaging, practical, and accessible
than the dry introductory programming books that I tried (and often failed) to
comprehend as a kid. I love the authors’ hands-on approach of mixing explanations
with code snippets that students can type into the Python prompt.

- Philip Guo

Creator of Online Python Tutor and Assistant Professor, Department of Computer Science, University of Rochester

“Practical Programming” delivers just what it promises: a clear, readable, usable
introduction to programming for beginners. This isn’t just a guide to hacking
together programs. The book provides foundations to lifelong programming skills:
a crisp, consistent, and visual model of memory and execution and a design recipe
that will help readers produce quality software.

- Steven Wolfman

Senior Instructor, Department of Computer Science, University of British Columbia

The second edition of this excellent text reflects the authors’ many years of experience
teaching Python to beginning students. Topics are presented so that each
leads naturally to the next, and common novice errors and misconceptions are
explicitly addressed. The exercises at the end of each chapter invite interested
students to explore computer science and programming language topics.

- Kathleen Freeman

Director of Undergraduate Studies, Department of Computer and Information Science, University of Oregon

See All Reviews

Choose Your Format(s)

  • $25.00 In Stock
  • Ebooks are DRM free.

  • Ebook delivery options.

About this Title

Pages: 400
Published: 2013-09-16
Release: P3.0 (2016-01-07)
ISBN: 978-1-93778-545-1

Exercise Solutions:

Chapter 2;
Chapter 3;
Chapter 4;
Chapter 5;
Chapter 6;
Chapter 7;
Chapter 8;
Chapter 9;
Chapter 10;
Chapter 11;
Chapter 12;
Chapter 13;
Chapter 14;
Chapter 15;
Chapter 16;
Chapter 17;

You don’t need any programming experience to get started. First, you’ll get a detailed introduction to Python and to programming. You’ll find out exactly what happens when your programs are executed. Through real-world examples, you’ll learn how to work with numbers, text, big data sets, and files. Then you’ll see how to create and use your own data types.

The incremental examples show you the steps and missteps that happen while developing programs, so you know what to expect when you tackle a problem on your own. Inspired by “How to Design Programs” (HtDP), you’ll learn a six-step recipe for designing functions, which helps you as you start to learn the concepts—and becomes an integral part of writing programs by the end.

As you learn to use the fundamental programming tools in the first half of the book, you’ll see how to document and organize your code so that you and other programmers can more easily read and understand it. Beyond the basics, you’ll learn how to ensure that your programs are reliable, and how to work with databases, download data from the web automatically, and build user interfaces. Most importantly, you’ll learn how to think like a professional programmer.


What You Need

You’ll need to download Python 3, available from
With that download comes IDLE, the editor we use for writing and
running Python programs. (If you use Linux, you may need to install
Python 3 and IDLE separately.)

Here are the details:

Installing and Running Python 3

Practical Programming uses Python 3. As of July 2013, the most recent version is Python 3.3.2, although anything starting with a "3" will do. Python 2 is not compatible with the book.

The program we'll use to write Python programs is called IDLE.

Select the appropriate set of installation instructions:

Linux Installation Instructions

Depending on the version of Linux you have, you might already have Python 3 installed. To check, look for an application that gives you a command line, such as Terminal or xterm, and type python3. You should see something like this:

    pgries@minty$ python3
    Python 3.2.3 (default, Apr 10 2013, 05:07:54)
    [GCC 4.7.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.

To quit this, type Control-D.

Again, the exact version number doesn't matter, as long as it starts with a 3.

If you don't have Python 3 installed, you will see something like this:

    pgries@minty$ python3
    python3: command not found

If you don't have Python 3 installed, you can use your package manager to install it, or you can install it from the command line. You might need to install IDLE separately; we have included instructions for that just in case:


    pgries@minty$ sudo apt-get install python3
    pgries@minty$ sudo apt-get install idle3

Fedora/Red Hat/CentOS:

    pgries@minty$ sudo yum install python3
    pgries@minty$ sudo yum install idle3

To test your installation, type idle3 at the command line. You should see something like this window:

! IDLE)!

This is called the Python Shell, and is where we start in the book.

Apple OS X Installation Instructions

Follow these instructions to install Python 3 on Mac OS X.

  1. Visit and download the newest version of Python 3. You have two choices: the 64-bit/32-bit Installer and the 32-bit Installer. If you have a 64-bit system, choose the X86-64 MSI Installer; otherwise, choose the X86 MSI Installer. (If you are running OS X 10.5, you must choose the 32-bit installer.)

  2. Open the downloaded disk image (.dmg) and double-click Python.mpkg. Follow the instructions.

  3. Visit and download and install the latest version of ActiveTcl.

To test your installation, open your Applications folder, find the Python 3.3 folder, and double-click You should see something like this window:

! IDLE)!

This is called the Python Shell, and is where we start in the book.

Windows Installation Instructions

Follow these instructions to install Python 3 on Microsoft Windows.

  1. Visit and download the newest version of Python 3. You have two choices: the X86 MSI Installer and the X86-64 MSI Installer. If you know you have a 64-bit system, choose the X86-64 MSI Installer; otherwise, choose the X86 MSI Installer.

  2. Double-click the downloaded .msi file and follow the instructions. All the default options should be fine.

To test your installation, find Python 3 in your Start menu, click it, and then click IDLE. You should see something like this window:

! IDLE)!

This is called the Python Shell, and is where we start in the book.

Contents & Extracts

  • What’s Programming?
    • Programs and Programming
    • What’s a Programming Language?
    • What’s a Bug?
    • The Difference Between Brackets, Braces, and Parentheses
    • Installing Python
    • For Instructors: How This Book is Organized
    • What You’ll See in This Book
  • Hello, Python excerpt
    • How Does a Computer Run a Python Program?
    • Expressions and Values: Arithmetic in Python
    • What Is a Type?
    • Variables and Computer Memory: Remembering Values
    • How Python Tells You Something Went Wrong
    • A Single Statement That Spans Multiple Lines
    • Describing Code
    • Making Code Readable
    • The Object of This Chapter
    • Exercises
  • Designing and Using Functions
    • Functions that Python Provides
    • Memory Addresses: How Python Keeps Track of Values
    • Defining our Own Functions
    • Using Local Variables for Temporary Storage
    • Tracing Function Calls in the Memory Model
    • Designing New Functions: A Recipe
    • Writing and Running a Program
    • Omitting a Return Statement: None
    • Dealing with Situations That Your Code Doesn’t Handle
    • What Did You Call That?
    • Exercises
  • Working with Text excerpt
    • Creating Strings of Characters
    • Using Special Characters in Strings
    • Creating a Multiline String
    • Printing Information
    • Getting Information from the Keyboard
    • Quotes About Strings in This Text
    • Exercises
  • Making Choices
    • A Boolean Type
    • Choosing Which Statements to Execute
    • Nested If Statements
    • Remembering Results of Boolean Expression Evaluation
    • You Learned About Booleans: True or False?
    • Exercises
  • A Modular Approach to Program Organization
    • Importing Modules
    • Defining Your Own Modules
    • Testing Your Code Semiautomatically
    • Tips for Grouping Your Functions
    • Organizing Our Thoughts
    • Exercises
  • Using Methods
    • Modules, Classes, and Methods
    • Calling Methods the Object-Oriented Way
    • Exploring String Methods
    • What Are Those Underscores?
    • A Methodical Review
    • Exercises
  • Storing Collections of Data Using Lists
    • Storing and Accessing Data in Lists
    • Modifying Lists
    • Operations on Lists
    • Slicing Lists
    • Aliasing: What’s in a Name?
    • List Methods
    • Working With a List of Lists
    • A Summary List
    • Exercises
  • Repeating Code Using Loops
    • Processing Items in a List
    • Processing Characters in Strings
    • Looping Over a Range of Numbers
    • Nesting Loops in Loops
    • Looping Until a Condition Is Reached
    • Repetition Based On User Input
    • Controlling Loops Using Break and Continue
    • Repeating What You’ve Learned
    • Exercises
  • Reading and Writing Files
    • What Kinds of Files Are There?
    • Opening a File
    • Techniques for Reading Files
    • Files over the Internet
    • Writing Files
    • Writing Algorithms That Use the File-Reading Techniques
    • Multiline Records
    • Looking Ahead
    • Notes to File Away
    • Exercises
  • Storing Data Using Other Collection Types
    • Storing Data Using Sets
    • Storing Data Using Tuples
    • Storing Data Using Dictionaries
    • Inverting a Dictionary
    • Using the In Operator on Tuples, Sets, and Dictionaries
    • Comparing Collections
    • A Collection of New Information
    • Exercises
  • Designing Algorithms
    • Searching for the Smallest Values
    • Timing the Functions
    • At a Minimum, You Saw This
    • Exercises
  • Searching and Sorting excerpt
    • Searching a List
    • Binary Search
    • Sorting
    • More Efficient Sorting Algorithms
    • Mergesort: An NlogN Algorithm
    • Sorting Out What You Learned
    • Exercises
  • Object-Oriented Programming
    • Understanding a Problem Domain
    • Function “Isinstance,” Class Object, and Class Book
    • Writing a Method in Class Book
    • Plugging into Python Syntax: More Special Methods
    • A Little Bit of OO Theory
    • A Case Study: Molecules, Atoms, and PDB Files
    • Classifying What You’ve Learned
    • Exercises
  • Testing and Debugging
    • Why Do You Need to Test?
    • Case Study: Testing above_freezing
    • Case Study: Testing running_sum
    • Choosing Test Cases
    • Hunting Bugs
    • Bugs We’ve Put in Your Ear
    • Exercises
  • Creating Graphical User Interfaces
    • Using Module Tkinter
    • Building a Basic GUI
    • Models, Views, and Controllers, Oh My!
    • Customizing the Visual Style
    • Introducing a Few More Widgets
    • Object-Oriented GUIs
    • Keeping the Concepts from Being a GUI Mess
    • Exercises
  • Databases
    • Overview
    • Creating and Populating
    • Retrieving Data
    • Updating and Deleting
    • Using NULL for Missing Data
    • Using Joins to Combine Tables
    • Keys and Constraints
    • Advanced Features
    • Some Data Based On What You Learned
    • Exercises


Paul Gries has been teaching in the Department of Computer Science at the University of Toronto for more than 15 years. During his time at UofT, Paul has won numerous teaching awards, including UofT’s most prestigious teaching award and an Ontario-wide teaching award. Paul has also co-authored two textbooks, has been a leader in departmental curriculum design and renewal, and, with Jen, got to teach Python to tens of thousands of students in a MOOC.

Jennifer Campbell is a senior lecturer in the Department of Computer Science at the University of Toronto. Over the past 10 years, Jen’s primary focus has been on teaching and curriculum design of introductory courses. Jen is involved in several projects exploring student experiences in introductory computer science courses and the factors that contribute to success, including the effectiveness of the inverted classroom.

Jason Montojo is a research officer at the Donnelly Centre for Cellular and Biomolecular Research at the University of Toronto, where he develops scientific software for the Cytoscape and GeneMANIA projects. He has a strong interest in teaching computer science and frequently mentors students for Google’s Summer of Code program.