Learn Python the right way
Free!
Minimum price
$7.99
Minimum paid price

Learn Python the right way

How to think like a computer scientist

About the Book

About the Author

Table of Contents

  • Copyright Notice
  • Foreword
  • Preface
    • How and why I came to use Python
    • Finding a textbook
    • Introducing programming with Python
    • Building a community
  • Contributor List
    • Second Edition
    • First Edition
  • Chapter 1: The way of the program
    • 1.1. The Python programming language
    • 1.2. What is a program?
    • 1.3. What is debugging?
    • 1.4. Syntax errors
    • 1.5. Runtime errors
    • 1.6. Semantic errors
    • 1.7. Experimental debugging
    • 1.8. Formal and natural languages
    • 1.9. The first program
    • 1.10. Comments
    • 1.11. Glossary
    • 1.12. Exercises
  • Chapter 2: Variables, expressions and statements
    • 2.1. Values and data types
    • 2.2. Variables
    • 2.3. Variable names and keywords
    • 2.4. Statements
    • 2.5. Evaluating expressions
    • 2.6. Operators and operands
    • 2.7. Type converter functions
    • 2.8. Order of operations
    • 2.9. Operations on strings
    • 2.10. Input
    • 2.11. Composition
    • 2.12. The modulus operator
    • 2.13. Glossary
    • 2.14. Exercises
  • Chapter 3: Hello, little turtles!
    • 3.1. Our first turtle program
    • 3.2. Instances — a herd of turtles
    • 3.3. The for loop
    • 3.4. Flow of Execution of the for loop
    • 3.5. The loop simplifies our turtle program
    • 3.6. A few more turtle methods and tricks
    • 3.7. Glossary
    • 3.8. Exercises
  • Chapter 4: Functions
    • 4.1. Functions
    • 4.2. Functions can call other functions
    • 4.3. Flow of execution
    • 4.4. Functions that require arguments
    • 4.5. Functions that return values
    • 4.6. Variables and parameters are local
    • 4.7. Turtles Revisited
    • 4.8. Glossary
    • 4.9. Exercises
  • Chapter 5: Conditionals
    • 5.1. Boolean values and expressions
    • 5.2. Logical operators
    • 5.3. Truth Tables
    • 5.4. Simplifying Boolean Expressions
    • 5.5. Conditional execution
    • 5.6. Omitting the else clause
    • 5.7. Chained conditionals
    • 5.8. Nested conditionals
    • 5.9. The return statement
    • 5.10. Logical opposites
    • 5.11. Type conversion
    • 5.12. A Turtle Bar Chart
    • 5.13. Glossary
    • 5.14. Exercises
  • Chapter 6: Fruitful functions
    • 6.1. Return values
    • 6.2. Program development
    • 6.3. Debugging with print
    • 6.4. Composition
    • 6.5. Boolean functions
    • 6.6. Programming with style
    • 6.7. Unit testing
    • 6.8. Glossary
    • 6.9. Exercises
  • Chapter 7: Iteration
    • 7.1. Assignment
    • 7.2. Updating variables
    • 7.3. The for loop revisited
    • 7.4. The while statement
    • 7.5. The Collatz 3n + 1 sequence
    • 7.6. Tracing a program
    • 7.7. Counting digits
    • 7.8. Abbreviated assignment
    • 7.9. Help and meta-notation
    • 7.10. Tables
    • 7.11. Two-dimensional tables
    • 7.12. Encapsulation and generalization
    • 7.13. More encapsulation
    • 7.14. Local variables
    • 7.15. The break statement
    • 7.16. Other flavours of loops
    • 7.17. An example
    • 7.18. The continue statement
    • 7.19. More generalization
    • 7.20. Functions
    • 7.21. Paired Data
    • 7.22. Nested Loops for Nested Data
    • 7.23. Newton’s method for finding square roots
    • 7.24. Algorithms
    • 7.25. Glossary
    • 7.26. Exercises
  • Chapter 8: Strings
    • 8.1. A compound data type
    • 8.2. Working with strings as single things
    • 8.3. Working with the parts of a string
    • 8.4. Length
    • 8.5. Traversal and the for loop
    • 8.6. Slices
    • 8.7. String comparison
    • 8.8. Strings are immutable
    • 8.9. The in and not in operators
    • 8.10. A find function
    • 8.11. Looping and counting
    • 8.12. Optional parameters
    • 8.13. The built-in find method
    • 8.14. The split method
    • 8.15. Cleaning up your strings
    • 8.16. The string format method
    • 8.17. Summary
    • 8.18. Glossary
    • 8.19. Exercises
  • Chapter 9: Tuples
    • 9.1. Tuples are used for grouping data
    • 9.2. Tuple assignment
    • 9.3. Tuples as return values
    • 9.4. Composability of Data Structures
    • 9.5. Glossary
    • 9.6. Exercises
  • Chapter 10: Event handling
    • 10.1. Event-driven programming
    • 10.2. Keypress events
    • 10.3. Mouse events
    • 10.4. Automatic events from a timer
    • 10.5. An example: state machines
    • 10.6. Glossary
    • 10.7. Exercises
  • Chapter 11: Lists
    • 11.1. List values
    • 11.2. Accessing elements
    • 11.3. List length
    • 11.4. List membership
    • 11.5. List operations
    • 11.6. List slices
    • 11.7. Lists are mutable
    • 11.8. List deletion
    • 11.9. Objects and references
    • 11.10. Aliasing
    • 11.11. Cloning lists
    • 11.12. Lists and for loops
    • 11.13. List parameters
    • 11.14. List methods
    • 11.15. Pure functions and modifiers
    • 11.16. Functions that produce lists
    • 11.17. Strings and lists
    • 11.18. list and range
    • 11.19. Nested lists
    • 11.20. Matrices
    • 11.21. Glossary
    • 11.22. Exercises
  • Chapter 12: Modules
    • 12.1. Random numbers
    • 12.2. The time module
    • 12.3. The math module
    • 12.4. Creating your own modules
    • 12.5. Namespaces
    • 12.6. Scope and lookup rules
    • 12.7. Attributes and the dot operator
    • 12.8. Three import statement variants
    • 12.9. Turn your unit tester into a module
    • 12.10. Glossary
    • 12.11. Exercises
  • Chapter 13: Files
    • 13.1. About files
    • 13.2. Writing our first file
    • 13.3. Reading a file line-at-a-time
    • 13.4. Turning a file into a list of lines
    • 13.5. Reading the whole file at once
    • 13.6. Working with binary files
    • 13.7. An example
    • 13.8. Directories
    • 13.9. What about fetching something from the web?
    • 13.10. Glossary
    • 13.11. Exercises
  • Chapter 14: List Algorithms
    • 14.1. Test-driven development
    • 14.2. The linear search algorithm
    • 14.3. A more realistic problem
    • 14.4. Binary Search
    • 14.5. Removing adjacent duplicates from a list
    • 14.6. Merging sorted lists
    • 14.7. Alice in Wonderland, again!
    • 14.8. Eight Queens puzzle, part 1
    • 14.9. Eight Queens puzzle, part 2
    • 14.10. Glossary
    • 14.11. Exercises
  • Chapter 15: Classes and Objects — the Basics
    • 15.1. Object-oriented programming
    • 15.2. User-defined compound data types
    • 15.3. Attributes
    • 15.4. Improving our initializer
    • 15.5. Adding other methods to our class
    • 15.6. Instances as arguments and parameters
    • 15.7. Converting an instance to a string
    • 15.8. Instances as return values
    • 15.9. A change of perspective
    • 15.10. Objects can have state
    • 15.11. Glossary
    • 15.12. Exercises
  • Chapter 16: Classes and Objects — Digging a little deeper
    • 16.1. Rectangles
    • 16.2. Objects are mutable
    • 16.3. Sameness
    • 16.4. Copying
    • 16.5. Glossary
    • 16.6. Exercises
  • Chapter 17: PyGame
    • 17.1. The game loop
    • 17.2. Displaying images and text
    • 17.3. Drawing a board for the N queens puzzle
    • 17.4. Sprites
    • 17.5. Events
    • 17.6. A wave of animation
    • 17.7. Aliens - a case study
    • 17.8. Reflections
    • 17.9. Glossary
    • 17.10. Exercises
  • Chapter 18: Recursion
    • 18.1. Drawing Fractals
    • 18.2. Recursive data structures
    • 18.3. Processing recursive number lists
    • 18.4. Case study: Fibonacci numbers
    • 18.5. Example with recursive directories and files
    • 18.6. An animated fractal, using PyGame
    • 18.7. Glossary
    • 18.8. Exercises
  • Chapter 19: Exceptions
    • 19.1. Catching exceptions
    • 19.2. Raising our own exceptions
    • 19.3. Revisiting an earlier example
    • 19.4. The finally clause of the try statement
    • 19.5. Glossary
    • 19.6. Exercises
  • Chapter 20: Dictionaries
    • 20.1. Dictionary operations
    • 20.2. Dictionary methods
    • 20.3. Aliasing and copying
    • 20.4. Sparse matrices
    • 20.5. Memoization
    • 20.6. Counting letters
    • 20.7. Glossary
    • 20.8. Exercises
  • Chapter 21: A Case Study: Indexing your files
    • 21.1. The Crawler
    • 21.2. Saving the dictionary to disk
    • 21.3. The Query Program
    • 21.4. Compressing the serialized dictionary
    • 21.5. Glossary
  • Chapter 22: Even more OOP
    • 22.1. MyTime
    • 22.2. Pure functions
    • 22.3. Modifiers
    • 22.4. Converting increment to a method
    • 22.5. An “Aha!” insight
    • 22.6. Generalization
    • 22.7. Another example
    • 22.8. Operator overloading
    • 22.9. Polymorphism
    • 22.10. Glossary
    • 22.11. Exercises
  • Chapter 23: Collections of objects
    • 23.1. Composition
    • 23.2. Card objects
    • 23.3. Class attributes and the __str__ method
    • 23.4. Comparing cards
    • 23.5. Decks
    • 23.6. Printing the deck
    • 23.7. Shuffling the deck
    • 23.8. Removing and dealing cards
    • 23.9. Glossary
    • 23.10. Exercises
  • Chapter 24: Inheritance
    • 24.1. Inheritance
    • 24.2. A hand of cards
    • 24.3. Dealing cards
    • 24.4. Printing a Hand
    • 24.5. The CardGame class
    • 24.6. OldMaidHand class
    • 24.7. OldMaidGame class
    • 24.8. Glossary
    • 24.9. Exercises
  • Chapter 25: Linked lists
    • 25.1. Embedded references
    • 25.2. The Node class
    • 25.3. Lists as collections
    • 25.4. Lists and recursion
    • 25.5. Infinite lists
    • 25.6. The fundamental ambiguity theorem
    • 25.7. Modifying lists
    • 25.8. Wrappers and helpers
    • 25.9. The LinkedList class
    • 25.10. Invariants
    • 25.11. Glossary
    • 25.12. Exercises
  • Chapter 26: Stacks
    • 26.1. Abstract data types
    • 26.2. The Stack ADT
    • 26.3. Implementing stacks with Python lists
    • 26.4. Pushing and popping
    • 26.5. Using a stack to evaluate postfix
    • 26.6. Parsing
    • 26.7. Evaluating postfix
    • 26.8. Clients and providers
    • 26.9. Glossary
    • 26.10. Exercises
  • Chapter 27: Queues
    • 27.1. The Queue ADT
    • 27.2. Linked Queue
    • 27.3. Performance characteristics
    • 27.4. Improved Linked Queue
    • 27.5. Priority queue
    • 27.6. The Golfer class
    • 27.7. Glossary
    • 27.8. Exercises
  • Chapter 28: Trees
    • 28.1. Building trees
    • 28.2. Traversing trees
    • 28.3. Expression trees
    • 28.4. Tree traversal
    • 28.5. Building an expression tree
    • 28.6. Handling errors
    • 28.7. The animal tree
    • 28.8. Glossary
    • 28.9. Exercises
  • Appendix A: Debugging
    • A.1. Syntax errors
    • A.2. I can’t get my program to run no matter what I do.
    • A.3. Runtime errors
    • A.4. My program does absolutely nothing.
    • A.5. My program hangs.
    • A.6. Infinite Loop
    • A.7. Infinite Recursion
    • A.8. Flow of Execution
    • A.9. When I run the program I get an exception.
    • A.10. I added so many print statements I get inundated with output.
    • A.11. Semantic errors
    • A.12. My program doesn’t work.
    • A.13. I’ve got a big hairy expression and it doesn’t do what I expect.
    • A.14. I’ve got a function or method that doesn’t return what I expect.
    • A.15. I’m really, really stuck and I need help.
    • A.16. No, I really need help.
  • Appendix B: An odds-and-ends Workbook
    • B.1. The Five Strands of Proficiency
    • B.2. Sending Email
    • B.3. Write your own Web Server
    • B.4. Using a Database
  • Appendix C: Configuring Ubuntu for Python Development
    • C.1. Vim
    • C.2. $HOME environment
    • C.3. Making a Python script executable and runnable from anywhere
  • Appendix D: Customizing and Contributing to the Book
    • D.1. Getting the Source
    • D.2. Making the HTML Version
  • Appendix E: Some Tips, Tricks, and Common Errors
    • E.1. Functions
    • E.2. Problems with logic and flow of control
    • E.3. Local variables
    • E.4. Event handler functions
    • E.5. String handling
    • E.6. Looping and lists

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earnedover $13 millionwriting, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub