You are previewing Think Python, 2nd Edition.
O'Reilly logo
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.

Table of Contents

  1. Preface
    1. The strange history of this book
    2. Acknowledgments
    3. 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. Debugging
    11. Interface and implementation
    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. Debugging
    10. Data encapsulation
    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