Think Python, 3rd Edition

Book description

Python is an excellent way to get started in programming, and this clear, concise guide walks you through Python a step at a time—beginning with basic programming concepts before moving on to functions, data structures, and object-oriented design. This revised third edition reflects the growing role of large language models (LLMs) in programming and includes exercises on effective LLM prompts, testing code, and debugging skills.

With this popular hands-on guide at your side, you'll get:

  • A grounding in the syntax and semantics of the Python language
  • A clear definition of each programming concept, with emphasis on clear vocabulary
  • How to work with variables, statements, functions, and data structures in a logical progression
  • Techniques for reading and writing files and databases
  • A solid understanding of objects, methods, and object-oriented programming
  • Debugging strategies for syntax, runtime, and semantic errors
  • An introduction to recursion, interface design, data structures, and basic algorithms
  • How to use LLMs—including effective prompts, testing code, and debugging
  • And more

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. Who Is This Book For?
    2. Goals of the Book
    3. Navigating the Book
    4. What’s new in the third edition?
    5. Getting started
    6. Resources for Teachers
    7. Conventions Used in This Book
    8. Using Code Examples
    9. O’Reilly Online Learning
    10. How to Contact Us
    11. Acknowledgments
  2. 1. Programming as a way of thinking
    1. Arithmetic operators
    2. Expressions
    3. Arithmetic functions
    4. Strings
    5. Values and types
    6. Formal and natural languages
    7. Debugging
    8. Glossary
    9. Exercises
      1. Ask a virtual assistant
      2. Exercise
      3. Exercise
      4. Exercise
      5. Exercise
  3. 2. Variables and Statements
    1. Variables
    2. State diagrams
    3. Variable names
    4. The import statement
    5. Expressions and statements
    6. The print function
    7. Arguments
    8. Comments
    9. Debugging
    10. Glossary
    11. Exercises
      1. Ask a virtual assistant
      2. Exercise
      3. Exercise
  4. 3. Functions
    1. Defining new functions
    2. Parameters
    3. Calling functions
    4. Repetition
    5. Variables and parameters are local
    6. Stack diagrams
    7. Tracebacks
    8. Why functions?
    9. Debugging
    10. Glossary
    11. Exercises
      1. Ask an assistant
      2. Exercise
      3. Exercise
      4. Exercise
      5. Exercise
  5. 4. Functions and Interfaces
    1. The jupyturtle module
    2. Making a square
    3. Encapsulation and generalization
    4. Approximating a circle
    5. Refactoring
    6. Stack diagram
    7. A development plan
    8. Docstrings
    9. Debugging
    10. Glossary
    11. Exercises
      1. Exercise
      2. Exercise
      3. Exercise
      4. Exercise
      5. Exercise
      6. Ask an assistant
  6. 5. Conditionals and Recursion
    1. Integer division and modulus
    2. Boolean Expressions
    3. Logical operators
    4. if statements
    5. The else clause
    6. Chained conditionals
    7. Nested Conditionals
    8. Recursion
    9. Stack diagrams for recursive functions
    10. Infinite recursion
    11. Keyboard input
    12. Debugging
    13. Glossary
    14. Exercises
      1. Ask an assistant
      2. Exercise
      3. Exercise
      4. Exercise
      5. Exercise
      6. Exercise
      7. Exercise
  7. 6. Return Values
    1. Some functions have return values
    2. And some have None
    3. Return values and conditionals
    4. Incremental development
    5. Boolean functions
    6. Recursion with return values
    7. Leap of faith
    8. Fibonacci
    9. Checking types
    10. Debugging
    11. Glossary
    12. Exercises
      1. Ask a virtual assistant
      2. Exercise
      3. Exercise
      4. Exercise
      5. Exercise
  8. 7. Iteration and Search
    1. Loops and strings
    2. Reading the word list
    3. Updating variables
    4. Looping and counting
    5. The in operator
    6. Search
    7. Doctest
    8. Glossary
    9. Exercises
      1. Ask a Virtual Assistant
      2. Exercise
      3. Exercise
      4. Exercise
      5. Exercise
      6. Exercise
      7. Exercise
      8. Exercise
  9. 8. Strings and Regular Expressions
    1. A string is a sequence
    2. String slices
    3. Strings are immutable
    4. String comparison
    5. String methods
    6. Writing files
    7. Find and replace
    8. Regular expressions
    9. String substitution
    10. Debugging
    11. Glossary
    12. Exercises
      1. Ask a virtual assistant
      2. Exercise
      3. Exercise
      4. Exercise
      5. Exercise
  10. 9. Lists
    1. A list is a sequence
    2. Lists are mutable
    3. List slices
    4. List operations
    5. List methods
    6. Lists and strings
    7. Looping through a list
    8. Sorting lists
    9. Objects and values
    10. Aliasing
    11. List arguments
    12. Making a word list
    13. Debugging
    14. Glossary
    15. Exercises
      1. Ask a virtual assistant
      2. Exercise
      3. Exercise
      4. Exercise
      5. Exercise
  11. 10. Dictionaries
    1. A dictionary is a mapping
    2. Creating dictionaries
    3. The in operator
    4. A collection of counters
    5. Looping and dictionaries
    6. Lists and dictionaries
    7. Accumulating a list
    8. Memos
    9. Debugging
    10. Glossary
    11. Exercises
      1. Ask an assistant
      2. Exercise
      3. Exercise
      4. Exercise
      5. Exercise
      6. Exercise
  12. 11. Tuples
    1. Tuples are like lists
    2. But tuples are immutable
    3. Tuple assignment
    4. Tuples as return values
    5. Argument packing
    6. Zip
    7. Comparing and Sorting
    8. Inverting a dictionary
    9. Debugging
    10. Glossary
    11. Exercises
      1. Ask a virtual assistant
      2. Exercise
      3. Exercise
      4. Exercise
      5. Exercise
      6. Exercise
      7. Exercise
  13. 12. Text Analysis and Generation
    1. Unique words
    2. Punctuation
    3. Word frequencies
    4. Optional parameters
    5. Dictionary subtraction
    6. Random numbers
    7. Bigrams
    8. Markov analysis
    9. Generating text
    10. Debugging
    11. Glossary
    12. Exercises
      1. Ask a virtual assistant
      2. Exercise
      3. Exercise
      4. Exercise
  14. 13. Files and Databases
    1. Filenames and paths
    2. f-strings
    3. YAML
    4. Shelve
    5. Storing data structures
    6. Checking for equivalent files
    7. Walking directories
    8. Debugging
    9. Glossary
    10. Exercises
      1. Ask a virtual assistant
      2. Exercise
      3. Exercise
      4. Exercise
  15. 14. Classes and Functions
    1. Programmer-defined types
    2. Attributes
    3. Objects as return values
    4. Objects are mutable
    5. Copying
    6. Pure functions
    7. Prototype and patch
    8. Design-first development
    9. Debugging
    10. Glossary
    11. Exercises
      1. Ask a virtual assistant
      2. Exercise
      3. Exercise
      4. Exercise
  16. 15. Classes and Methods
    1. Defining methods
    2. Another method
    3. Static methods
    4. Comparing Time objects
    5. The __str__ method
    6. The init method
    7. Operator overloading
    8. Debugging
    9. Glossary
    10. Exercises
      1. Ask a virtual assistant
      2. Exercise
  17. 16. Classes and Objects
    1. Creating a Point
    2. Creating a Line
    3. Equivalence and identity
    4. Creating a Rectangle
    5. Changing rectangles
    6. Deep copy
    7. Polymorphism
    8. Debugging
    9. Glossary
    10. Exercises
      1. Ask a virtual assistant
      2. Exercise
      3. Exercise
      4. Exercise
      5. Exercise
      6. Exercise
  18. 17. Inheritance
    1. Representing cards
    2. Card attributes
    3. Printing cards
    4. Comparing cards
    5. Decks
    6. Printing the deck
    7. Add, remove, shuffle and sort
    8. Parents and children
    9. Specialization
    10. Debugging
    11. Glossary
    12. Exercises
      1. Ask a Virtual Assistant
      2. Exercise
      3. Exercise
      4. Exercise
      5. Exercise
      6. Exercise
      7. Exercise
      8. Exercise
  19. 18. Python Extras
    1. Sets
    2. Counters
    3. defaultdict
    4. Conditional expressions
    5. List comprehensions
    6. any and all
    7. Named tuples
    8. Packing keyword arguments
    9. Debugging
    10. Glossary
    11. Exercises
      1. Ask a virtual assistant
      2. Exercise
      3. Exercise
      4. Exercise
      5. Exercise
      6. Exercise
      7. Exercise
  20. 19. Final thoughts
  21. Index
  22. About the Author

Product information

  • Title: Think Python, 3rd Edition
  • Author(s): Allen B. Downey
  • Release date: May 2024
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781098155438