Think Python, 2nd Edition

Book description

If you want to learn how to program, working with Python is an excellent way to start. This hands-on guide takes you through the language a step at a time, beginning with basic programming concepts before moving on to functions, recursion, data structures, and object-oriented design. This second edition and its supporting code have been updated for Python 3.

Through exercises in each chapter, youâ??ll try out programming concepts as you learn them. Think Python is ideal for students at the high school or college level, as well as self-learners, home-schooled students, and professionals who need to learn programming basics. Beginners just getting their feet wet will learn how to start with Python in a browser.

  • Start with the basics, including language syntax and semantics
  • Get a clear definition of each programming concept
  • Learn about values, variables, statements, functions, and data structures in a logical progression
  • Discover how to work with files and databases
  • Understand objects, methods, and object-oriented programming
  • Use debugging techniques to fix syntax, runtime, and semantic errors
  • Explore interface design, data structures, and GUI-based programs through case studies

Publisher resources

View/Submit Errata

Table of contents

  1. Preface
    1. The Strange History of This Book
    2. Conventions Used in This Book
    3. Using Code Examples
    4. O’Reilly Online Learning
    5. How to Contact Us
    6. Acknowledgments
    7. Contributor List
  2. 1. The Way of the Program
    1. What Is a Program?
    2. Running Python
    3. The First Program
    4. Arithmetic Operators
    5. Values and Types
    6. Formal and Natural Languages
    7. Debugging
    8. Glossary
    9. Exercises
  3. 2. Variables, Expressions and Statements
    1. Assignment Statements
    2. Variable Names
    3. Expressions and Statements
    4. Script Mode
    5. Order of Operations
    6. String Operations
    7. Comments
    8. Debugging
    9. Glossary
    10. Exercises
  4. 3. Functions
    1. Function Calls
    2. Math Functions
    3. Composition
    4. Adding New Functions
    5. Definitions and Uses
    6. Flow of Execution
    7. Parameters and Arguments
    8. Variables and Parameters Are Local
    9. Stack Diagrams
    10. Fruitful Functions and Void Functions
    11. Why Functions?
    12. Debugging
    13. Glossary
    14. Exercises
  5. 4. Case Study: Interface Design
    1. The turtle Module
    2. Simple Repetition
    3. Exercises
    4. Encapsulation
    5. Generalization
    6. Interface Design
    7. Refactoring
    8. A Development Plan
    9. docstring
    10. Debugging
    11. Glossary
    12. Exercises
  6. 5. Conditionals and Recursion
    1. Floor Division and Modulus
    2. Boolean Expressions
    3. Logical Operators
    4. Conditional Execution
    5. Alternative Execution
    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. Fruitful Functions
    1. Return Values
    2. Incremental Development
    3. Composition
    4. Boolean Functions
    5. More Recursion
    6. Leap of Faith
    7. One More Example
    8. Checking Types
    9. Debugging
    10. Glossary
    11. Exercises
  8. 7. Iteration
    1. Reassignment
    2. Updating Variables
    3. The while Statement
    4. break
    5. Square Roots
    6. Algorithms
    7. Debugging
    8. Glossary
    9. Exercises
  9. 8. Strings
    1. A String Is a Sequence
    2. len
    3. Traversal with a for Loop
    4. String Slices
    5. Strings Are Immutable
    6. Searching
    7. Looping and Counting
    8. String Methods
    9. The in Operator
    10. String Comparison
    11. Debugging
    12. Glossary
    13. Exercises
  10. 9. Case Study: Word Play
    1. Reading Word Lists
    2. Exercises
    3. Search
    4. Looping with Indices
    5. Debugging
    6. Glossary
    7. Exercises
  11. 10. Lists
    1. A List Is a Sequence
    2. Lists Are Mutable
    3. Traversing a List
    4. List Operations
    5. List Slices
    6. List Methods
    7. Map, Filter and Reduce
    8. Deleting Elements
    9. Lists and Strings
    10. Objects and Values
    11. Aliasing
    12. List Arguments
    13. Debugging
    14. Glossary
    15. Exercises
  12. 11. Dictionaries
    1. A Dictionary Is a Mapping
    2. Dictionary as a Collection of Counters
    3. Looping and Dictionaries
    4. Reverse Lookup
    5. Dictionaries and Lists
    6. Memos
    7. Global Variables
    8. Debugging
    9. Glossary
    10. Exercises
  13. 12. Tuples
    1. Tuples Are Immutable
    2. Tuple Assignment
    3. Tuples as Return Values
    4. Variable-Length Argument Tuples
    5. Lists and Tuples
    6. Dictionaries and Tuples
    7. Sequences of Sequences
    8. Debugging
    9. Glossary
    10. Exercises
  14. 13. Case Study: Data Structure Selection
    1. Word Frequency Analysis
    2. Random Numbers
    3. Word Histogram
    4. Most Common Words
    5. Optional Parameters
    6. Dictionary Subtraction
    7. Random Words
    8. Markov Analysis
    9. Data Structures
    10. Debugging
    11. Glossary
    12. Exercises
  15. 14. Files
    1. Persistence
    2. Reading and Writing
    3. Format Operator
    4. Filenames and Paths
    5. Catching Exceptions
    6. Databases
    7. Pickling
    8. Pipes
    9. Writing Modules
    10. Debugging
    11. Glossary
    12. Exercises
  16. 15. Classes and Objects
    1. Programmer-Defined Types
    2. Attributes
    3. Rectangles
    4. Instances as Return Values
    5. Objects Are Mutable
    6. Copying
    7. Debugging
    8. Glossary
    9. Exercises
  17. 16. Classes and Functions
    1. Time
    2. Pure Functions
    3. Modifiers
    4. Prototyping versus Planning
    5. Debugging
    6. Glossary
    7. Exercises
  18. 17. Classes and Methods
    1. Object-Oriented Features
    2. Printing Objects
    3. Another Example
    4. A More Complicated Example
    5. The init Method
    6. The __str__ Method
    7. Operator Overloading
    8. Type-Based Dispatch
    9. Polymorphism
    10. Interface and Implementation
    11. Debugging
    12. Glossary
    13. Exercises
  19. 18. Inheritance
    1. Card Objects
    2. Class Attributes
    3. Comparing Cards
    4. Decks
    5. Printing the Deck
    6. Add, Remove, Shuffle and Sort
    7. Inheritance
    8. Class Diagrams
    9. Data Encapsulation
    10. Debugging
    11. Glossary
    12. Exercises
  20. 19. The Goodies
    1. Conditional Expressions
    2. List Comprehensions
    3. Generator Expressions
    4. any and all
    5. Sets
    6. Counters
    7. defaultdict
    8. Named Tuples
    9. Gathering Keyword Args
    10. Glossary
    11. Exercises
  21. 20. Debugging
    1. Syntax Errors
      1. I keep making changes and it makes no difference.
    2. Runtime Errors
      1. My program does absolutely nothing.
      2. My program hangs.
      3. When I run the program I get an exception.
      4. I added so many print statements I get inundated with output.
    3. Semantic Errors
      1. My program doesn’t work.
      2. I’ve got a big hairy expression and it doesn’t do what I expect.
      3. I’ve got a function that doesn’t return what I expect.
      4. I’m really, really stuck and I need help.
      5. No, I really need help.
  22. 21. Analysis of Algorithms
    1. Order of Growth
    2. Analysis of Basic Python Operations
    3. Analysis of Search Algorithms
    4. Hashtables
    5. Glossary
  23. Index
  24. About the Author

Product information

  • Title: Think Python, 2nd Edition
  • Author(s): Allen B. Downey
  • Release date: December 2015
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781491939369