Index / 001 EN
Think Python: How to Think Like a Computer Scientist cover

Computer Science

Computer Science

Think Python: How to Think Like a Computer Scientist

Allen B. Downey

A clear, practical introduction to computer science and Python programming, covering fundamentals, data structures, object-oriented concepts, algorithms, and problem-solving.

Difficulty Level
Beginner
Academic Level
Undergraduate
python programmingprogramming fundamentalsdata structuresobject oriented programmingalgorithmic thinkingcomputer science

01 / Reading Guide

Reading Guide

Citation

Downey, A. B. (2024). Think Python: How to Think Like a Computer Scientist (3rd ed.). O'Reilly Media. Free official edition: https://allendowney.github.io/ThinkPython/

Chapter Summary

Each of the 19 chapters is a Jupyter notebook that can be run in the browser on Colab. Chapters 1 to 18 end with a glossary and exercises, and most contain a "Debugging" section on the typical errors of that topic.

Chapter 1: Programming as a way of thinking

The chapter presents programming as a way of thinking that borrows from mathematics, engineering and science. It covers arithmetic operators, expressions, values and types, strings, and the difference between formal and natural languages, with the vocabulary the rest of the book relies on.

Chapter 2: Variables and Statements

Assignment, state diagrams, variable names, the import statement, the difference between expressions and statements, print, arguments and comments.

Chapter 3: Functions

Defining and calling functions, parameters, loops with for and range, local variables, stack diagrams and tracebacks. The examples use Monty Python lyrics, and the chapter ends by asking why functions are worth writing.

Chapter 4: Functions and Interfaces

A turtle-graphics module draws squares, polygons and circles while the chapter teaches encapsulation, generalization, refactoring, a development plan and docstrings. The theme is designing functions whose interfaces fit together.

Chapter 5: Conditionals and Recursion

Integer division and modulus, boolean expressions, if, else and chained and nested conditionals, then recursion, stack diagrams for recursive functions, infinite recursion and keyboard input.

Chapter 6: Return Values

Functions that return values and functions that return None, incremental development, boolean functions, recursion with return values, the "leap of faith" approach to recursion, Fibonacci and type checking.

Chapter 7: Iteration and Search

Looping over strings and the words of a file, updating variables, counting, the in operator and linear search, framed by the question of how many English words contain the letter "e". It also introduces doctest, and the exercises include a Spelling Bee puzzle.

Chapter 8: Strings and Regular Expressions

Strings as sequences, slices, immutability, comparison and string methods, then writing files, find and replace, regular expressions and string substitution. The exercises use the word game Wordle.

Chapters 9–11: Lists; Dictionaries; Tuples

The three core data structures. Chapter 9 covers list operations, methods, sorting, aliasing and list arguments. Chapter 10 treats dictionaries as mappings, counters, looping, accumulating lists and memos. Chapter 11 covers immutability, tuple assignment, tuples as return values, argument packing, zip, sorting and inverting a dictionary. Word puzzles such as palindromes and anagrams run through the exercises.

Chapter 12: Text Analysis and Generation

Counting unique words and word frequencies in a novel, then bigrams, Markov analysis and generating new text. The chapter notes that these algorithms resemble parts of a large language model.

Chapter 13: Files and Databases

Filenames and paths, f-strings, storing data in YAML and with shelve, checking for equivalent files and walking directories. The final exercise is to find duplicate photos in a folder tree.

Chapters 14–16: Classes and Functions; Classes and Methods; Classes and Objects

An introduction to object-oriented programming in three steps. Chapter 14 defines a Time class and works with attributes, pure functions, prototype and patch, and design-first development. Chapter 15 turns functions into methods and adds static methods, str, init and operator overloading. Chapter 16 builds Point, Line and Rectangle classes to explain equivalence and identity, deep copy and polymorphism.

Chapter 17: Inheritance

Playing cards and decks are the running example, followed by parents and children and specialization, with attention to representing, comparing and sorting cards.

Chapter 18: Python Extras

Features the earlier chapters left out: sets, Counter, defaultdict, conditional expressions, list comprehensions, any and all, named tuples and packing keyword arguments. Its debugging section adds unittest alongside doctest.

Chapter 19: Final thoughts

Suggestions for continuing: other books by the author for data science and modelling, tools such as virtual assistants and other development environments, Python user groups, and learning a second language, with functional languages recommended.

Key Concepts

Chapters 1–6: Basics, functions and recursion

  • Value, type and expression: the vocabulary for talking about programs, introduced first so that error messages and documentation can be read.
  • Stack diagram: a picture of function frames used to follow local variables and recursive calls.
  • Incremental development: building a function in small tested steps, applied again in Chapter 6.

Chapters 7–13: Strings, collections and files

  • Linear search: scanning a sequence for an element, the pattern of Chapter 7.
  • Aliasing and mutability: several names for one list object, and why a tuple or string cannot be changed in place.
  • Dictionary as mapping: keys looked up directly, used for counting, memoizing and Markov analysis.
  • Persistence: keeping data beyond one run through files, YAML and shelve.

Chapters 14–18: Objects and Python extras

  • Programmer-defined type: a class that bundles data and operations.
  • Pure function and method: the same operation first written as a function, then moved into the class.
  • Inheritance and polymorphism: reusing a class through a child class, and code that works with objects of different classes.
  • Testing: doctest in earlier chapters and unittest in Chapter 18.

Critical Analysis

Strengths

  • A deliberately small language subset. The preface states the aim of covering the part of Python that gives the most capability with the fewest concepts, and Chapter 18 is the place where the omitted conveniences are collected.
  • Vocabulary taught on purpose. Terms are defined on first use and reviewed in a glossary at the end of every chapter, and the preface explains that the second thing a beginner must learn is the language used to talk about programs.
  • Debugging and testing built in. Almost every chapter has a Debugging section, and the third edition adds doctest and unittest, which the preface lists among its changes.
  • Exercises that mix practice with reflection. Each chapter's exercises begin with an "Ask a virtual assistant" item that models checking an assistant's answers, and the later exercises are word puzzles, text generation, duplicate-file search and card games rather than abstract drills.
  • Free and runnable. The text is licensed under Creative Commons, and the notebooks run in a browser without installing anything.

Limitations

  • Scope is intentionally narrow. The author says the book does not cover the whole language; there is no systematic treatment of algorithm analysis, standard-library breadth or large projects.
  • Dependence on the notebook setup and on assistants. The edition is built around Jupyter and around virtual assistants, so readers who prefer a plain editor and a terminal will have to adapt the instructions.
  • Slow start for experienced programmers. The preface warns that readers with substantial Python experience may find the first chapters too slow.
  • Object-oriented programming is introduced in a staged way. The first OOP chapter deliberately presents code that is not idiomatic, which can feel indirect until Chapter 15 supplies the usual form.

Where It Fits in the Course

Within the introductory-programming course list, this book is a compact first course in Python that pays equal attention to vocabulary, debugging and problem solving. Python Crash Course is the nearest alternative: it moves into larger projects, including a game, data visualizations and a web application, while Think Python stays with small programs and the vocabulary and habits behind them. How to Design Programs and Structure and Interpretation of Computer Programs teach programming through a different tradition, with design recipes and a Scheme-family language, and suit readers who want a stronger conceptual foundation than a Python-specific book gives.

Guttag's Introduction to Computation and Programming Using Python covers more ground, including computational modelling and data analysis, and is the natural next step after Think Python. The C Programming Language serves a different purpose, giving a short and low-level view of memory and types once the first language is comfortable.

How to Use This Book

The book is for people who have never programmed and for people who know another language but want to learn Python. It assumes no prerequisites. Chapters depend on one another, and the preface asks readers to go in order and to do the exercises before moving on.

A practical plan: start with Chapters 1 to 6, which are the foundation (arithmetic, functions, conditionals and recursion). Then work through Chapters 7 to 13 on strings, collections and files, which are the core of everyday Python. Chapters 14 to 17 on object-oriented programming can be treated as a second stage, and a reader mainly interested in scripting may read Chapter 14 and then skim. Chapter 18 is best read when the earlier chapters have raised a wish for shorter code, and Chapter 19 is a short list of next steps.

Run each chapter in Colab, type the examples rather than only reading them, and complete the exercises, including the Debugging sections. Notebooks with solutions are available from the book's website for teachers. When an assistant is used for hints, as the book suggests, test its code and compare it with the chapter's own approach.

Edition and Sources

This guide covers the third edition (2024), the free online version published by the author under a Creative Commons license at https://allendowney.github.io/ThinkPython/. It is based on the preface, the full table of contents with every section heading of Chapters 1 to 18, each chapter's introduction, and the exercises and closing chapter.