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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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
  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. 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
  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
  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
  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
  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
  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
  20. 19. Final Thoughts
  21. Index
  22. About the Author

Product information

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