Leanpub Header

Skip to main content

Learn Python the right way

How to think like a computer scientist

Minimum price

Free!

$7.99

You pay

$0.00

Author earns

$0.00
$

...Or Buy With Credits!

You can get credits with a paid monthly or annual Reader Membership, or you can buy them here.
PDF
EPUB
WEB
About

About

About the Book

Share this book

Author

About the Author

Ritza

Contents

Table of Contents

Copyright Notice

Foreword

Preface

  1. How and why I came to use Python
  2. Finding a textbook
  3. Introducing programming with Python
  4. Building a community

Contributor List

  1. Second Edition
  2. First Edition

Chapter 1: The way of the program

  1. 1.1. The Python programming language
  2. 1.2. What is a program?
  3. 1.3. What is debugging?
  4. 1.4. Syntax errors
  5. 1.5. Runtime errors
  6. 1.6. Semantic errors
  7. 1.7. Experimental debugging
  8. 1.8. Formal and natural languages
  9. 1.9. The first program
  10. 1.10. Comments
  11. 1.11. Glossary
  12. 1.12. Exercises

Chapter 2: Variables, expressions and statements

  1. 2.1. Values and data types
  2. 2.2. Variables
  3. 2.3. Variable names and keywords
  4. 2.4. Statements
  5. 2.5. Evaluating expressions
  6. 2.6. Operators and operands
  7. 2.7. Type converter functions
  8. 2.8. Order of operations
  9. 2.9. Operations on strings
  10. 2.10. Input
  11. 2.11. Composition
  12. 2.12. The modulus operator
  13. 2.13. Glossary
  14. 2.14. Exercises

Chapter 3: Hello, little turtles!

  1. 3.1. Our first turtle program
  2. 3.2. Instances — a herd of turtles
  3. 3.3. The for loop
  4. 3.4. Flow of Execution of the for loop
  5. 3.5. The loop simplifies our turtle program
  6. 3.6. A few more turtle methods and tricks
  7. 3.7. Glossary
  8. 3.8. Exercises

Chapter 4: Functions

  1. 4.1. Functions
  2. 4.2. Functions can call other functions
  3. 4.3. Flow of execution
  4. 4.4. Functions that require arguments
  5. 4.5. Functions that return values
  6. 4.6. Variables and parameters are local
  7. 4.7. Turtles Revisited
  8. 4.8. Glossary
  9. 4.9. Exercises

Chapter 5: Conditionals

  1. 5.1. Boolean values and expressions
  2. 5.2. Logical operators
  3. 5.3. Truth Tables
  4. 5.4. Simplifying Boolean Expressions
  5. 5.5. Conditional execution
  6. 5.6. Omitting the else clause
  7. 5.7. Chained conditionals
  8. 5.8. Nested conditionals
  9. 5.9. The return statement
  10. 5.10. Logical opposites
  11. 5.11. Type conversion
  12. 5.12. A Turtle Bar Chart
  13. 5.13. Glossary
  14. 5.14. Exercises

Chapter 6: Fruitful functions

  1. 6.1. Return values
  2. 6.2. Program development
  3. 6.3. Debugging with print
  4. 6.4. Composition
  5. 6.5. Boolean functions
  6. 6.6. Programming with style
  7. 6.7. Unit testing
  8. 6.8. Glossary
  9. 6.9. Exercises

Chapter 7: Iteration

  1. 7.1. Assignment
  2. 7.2. Updating variables
  3. 7.3. The for loop revisited
  4. 7.4. The while statement
  5. 7.5. The Collatz 3n + 1 sequence
  6. 7.6. Tracing a program
  7. 7.7. Counting digits
  8. 7.8. Abbreviated assignment
  9. 7.9. Help and meta-notation
  10. 7.10. Tables
  11. 7.11. Two-dimensional tables
  12. 7.12. Encapsulation and generalization
  13. 7.13. More encapsulation
  14. 7.14. Local variables
  15. 7.15. The break statement
  16. 7.16. Other flavours of loops
  17. 7.17. An example
  18. 7.18. The continue statement
  19. 7.19. More generalization
  20. 7.20. Functions
  21. 7.21. Paired Data
  22. 7.22. Nested Loops for Nested Data
  23. 7.23. Newton’s method for finding square roots
  24. 7.24. Algorithms
  25. 7.25. Glossary
  26. 7.26. Exercises

Chapter 8: Strings

  1. 8.1. A compound data type
  2. 8.2. Working with strings as single things
  3. 8.3. Working with the parts of a string
  4. 8.4. Length
  5. 8.5. Traversal and the for loop
  6. 8.6. Slices
  7. 8.7. String comparison
  8. 8.8. Strings are immutable
  9. 8.9. The in and not in operators
  10. 8.10. A find function
  11. 8.11. Looping and counting
  12. 8.12. Optional parameters
  13. 8.13. The built-in find method
  14. 8.14. The split method
  15. 8.15. Cleaning up your strings
  16. 8.16. The string format method
  17. 8.17. Summary
  18. 8.18. Glossary
  19. 8.19. Exercises

Chapter 9: Tuples

  1. 9.1. Tuples are used for grouping data
  2. 9.2. Tuple assignment
  3. 9.3. Tuples as return values
  4. 9.4. Composability of Data Structures
  5. 9.5. Glossary
  6. 9.6. Exercises

Chapter 10: Event handling

  1. 10.1. Event-driven programming
  2. 10.2. Keypress events
  3. 10.3. Mouse events
  4. 10.4. Automatic events from a timer
  5. 10.5. An example: state machines
  6. 10.6. Glossary
  7. 10.7. Exercises

Chapter 11: Lists

  1. 11.1. List values
  2. 11.2. Accessing elements
  3. 11.3. List length
  4. 11.4. List membership
  5. 11.5. List operations
  6. 11.6. List slices
  7. 11.7. Lists are mutable
  8. 11.8. List deletion
  9. 11.9. Objects and references
  10. 11.10. Aliasing
  11. 11.11. Cloning lists
  12. 11.12. Lists and for loops
  13. 11.13. List parameters
  14. 11.14. List methods
  15. 11.15. Pure functions and modifiers
  16. 11.16. Functions that produce lists
  17. 11.17. Strings and lists
  18. 11.18. list and range
  19. 11.19. Nested lists
  20. 11.20. Matrices
  21. 11.21. Glossary
  22. 11.22. Exercises

Chapter 12: Modules

  1. 12.1. Random numbers
  2. 12.2. The time module
  3. 12.3. The math module
  4. 12.4. Creating your own modules
  5. 12.5. Namespaces
  6. 12.6. Scope and lookup rules
  7. 12.7. Attributes and the dot operator
  8. 12.8. Three import statement variants
  9. 12.9. Turn your unit tester into a module
  10. 12.10. Glossary
  11. 12.11. Exercises

Chapter 13: Files

  1. 13.1. About files
  2. 13.2. Writing our first file
  3. 13.3. Reading a file line-at-a-time
  4. 13.4. Turning a file into a list of lines
  5. 13.5. Reading the whole file at once
  6. 13.6. Working with binary files
  7. 13.7. An example
  8. 13.8. Directories
  9. 13.9. What about fetching something from the web?
  10. 13.10. Glossary
  11. 13.11. Exercises

Chapter 14: List Algorithms

  1. 14.1. Test-driven development
  2. 14.2. The linear search algorithm
  3. 14.3. A more realistic problem
  4. 14.4. Binary Search
  5. 14.5. Removing adjacent duplicates from a list
  6. 14.6. Merging sorted lists
  7. 14.7. Alice in Wonderland, again!
  8. 14.8. Eight Queens puzzle, part 1
  9. 14.9. Eight Queens puzzle, part 2
  10. 14.10. Glossary
  11. 14.11. Exercises

Chapter 15: Classes and Objects — the Basics

  1. 15.1. Object-oriented programming
  2. 15.2. User-defined compound data types
  3. 15.3. Attributes
  4. 15.4. Improving our initializer
  5. 15.5. Adding other methods to our class
  6. 15.6. Instances as arguments and parameters
  7. 15.7. Converting an instance to a string
  8. 15.8. Instances as return values
  9. 15.9. A change of perspective
  10. 15.10. Objects can have state
  11. 15.11. Glossary
  12. 15.12. Exercises

Chapter 16: Classes and Objects — Digging a little deeper

  1. 16.1. Rectangles
  2. 16.2. Objects are mutable
  3. 16.3. Sameness
  4. 16.4. Copying
  5. 16.5. Glossary
  6. 16.6. Exercises

Chapter 17: PyGame

  1. 17.1. The game loop
  2. 17.2. Displaying images and text
  3. 17.3. Drawing a board for the N queens puzzle
  4. 17.4. Sprites
  5. 17.5. Events
  6. 17.6. A wave of animation
  7. 17.7. Aliens - a case study
  8. 17.8. Reflections
  9. 17.9. Glossary
  10. 17.10. Exercises

Chapter 18: Recursion

  1. 18.1. Drawing Fractals
  2. 18.2. Recursive data structures
  3. 18.3. Processing recursive number lists
  4. 18.4. Case study: Fibonacci numbers
  5. 18.5. Example with recursive directories and files
  6. 18.6. An animated fractal, using PyGame
  7. 18.7. Glossary
  8. 18.8. Exercises

Chapter 19: Exceptions

  1. 19.1. Catching exceptions
  2. 19.2. Raising our own exceptions
  3. 19.3. Revisiting an earlier example
  4. 19.4. The finally clause of the try statement
  5. 19.5. Glossary
  6. 19.6. Exercises

Chapter 20: Dictionaries

  1. 20.1. Dictionary operations
  2. 20.2. Dictionary methods
  3. 20.3. Aliasing and copying
  4. 20.4. Sparse matrices
  5. 20.5. Memoization
  6. 20.6. Counting letters
  7. 20.7. Glossary
  8. 20.8. Exercises

Chapter 21: A Case Study: Indexing your files

  1. 21.1. The Crawler
  2. 21.2. Saving the dictionary to disk
  3. 21.3. The Query Program
  4. 21.4. Compressing the serialized dictionary
  5. 21.5. Glossary

Chapter 22: Even more OOP

  1. 22.1. MyTime
  2. 22.2. Pure functions
  3. 22.3. Modifiers
  4. 22.4. Converting increment to a method
  5. 22.5. An “Aha!” insight
  6. 22.6. Generalization
  7. 22.7. Another example
  8. 22.8. Operator overloading
  9. 22.9. Polymorphism
  10. 22.10. Glossary
  11. 22.11. Exercises

Chapter 23: Collections of objects

  1. 23.1. Composition
  2. 23.2. Card objects
  3. 23.3. Class attributes and the __str__ method
  4. 23.4. Comparing cards
  5. 23.5. Decks
  6. 23.6. Printing the deck
  7. 23.7. Shuffling the deck
  8. 23.8. Removing and dealing cards
  9. 23.9. Glossary
  10. 23.10. Exercises

Chapter 24: Inheritance

  1. 24.1. Inheritance
  2. 24.2. A hand of cards
  3. 24.3. Dealing cards
  4. 24.4. Printing a Hand
  5. 24.5. The CardGame class
  6. 24.6. OldMaidHand class
  7. 24.7. OldMaidGame class
  8. 24.8. Glossary
  9. 24.9. Exercises

Chapter 25: Linked lists

  1. 25.1. Embedded references
  2. 25.2. The Node class
  3. 25.3. Lists as collections
  4. 25.4. Lists and recursion
  5. 25.5. Infinite lists
  6. 25.6. The fundamental ambiguity theorem
  7. 25.7. Modifying lists
  8. 25.8. Wrappers and helpers
  9. 25.9. The LinkedList class
  10. 25.10. Invariants
  11. 25.11. Glossary
  12. 25.12. Exercises

Chapter 26: Stacks

  1. 26.1. Abstract data types
  2. 26.2. The Stack ADT
  3. 26.3. Implementing stacks with Python lists
  4. 26.4. Pushing and popping
  5. 26.5. Using a stack to evaluate postfix
  6. 26.6. Parsing
  7. 26.7. Evaluating postfix
  8. 26.8. Clients and providers
  9. 26.9. Glossary
  10. 26.10. Exercises

Chapter 27: Queues

  1. 27.1. The Queue ADT
  2. 27.2. Linked Queue
  3. 27.3. Performance characteristics
  4. 27.4. Improved Linked Queue
  5. 27.5. Priority queue
  6. 27.6. The Golfer class
  7. 27.7. Glossary
  8. 27.8. Exercises

Chapter 28: Trees

  1. 28.1. Building trees
  2. 28.2. Traversing trees
  3. 28.3. Expression trees
  4. 28.4. Tree traversal
  5. 28.5. Building an expression tree
  6. 28.6. Handling errors
  7. 28.7. The animal tree
  8. 28.8. Glossary
  9. 28.9. Exercises

Appendix A: Debugging

  1. A.1. Syntax errors
  2. A.2. I can’t get my program to run no matter what I do.
  3. A.3. Runtime errors
  4. A.4. My program does absolutely nothing.
  5. A.5. My program hangs.
  6. A.6. Infinite Loop
  7. A.7. Infinite Recursion
  8. A.8. Flow of Execution
  9. A.9. When I run the program I get an exception.
  10. A.10. I added so many print statements I get inundated with output.
  11. A.11. Semantic errors
  12. A.12. My program doesn’t work.
  13. A.13. I’ve got a big hairy expression and it doesn’t do what I expect.
  14. A.14. I’ve got a function or method that doesn’t return what I expect.
  15. A.15. I’m really, really stuck and I need help.
  16. A.16. No, I really need help.

Appendix B: An odds-and-ends Workbook

  1. B.1. The Five Strands of Proficiency
  2. B.2. Sending Email
  3. B.3. Write your own Web Server
  4. B.4. Using a Database

Appendix C: Configuring Ubuntu for Python Development

  1. C.1. Vim
  2. C.2. $HOME environment
  3. C.3. Making a Python script executable and runnable from anywhere

Appendix D: Customizing and Contributing to the Book

  1. D.1. Getting the Source
  2. D.2. Making the HTML Version

Appendix E: Some Tips, Tricks, and Common Errors

  1. E.1. Functions
  2. E.2. Problems with logic and flow of control
  3. E.3. Local variables
  4. E.4. Event handler functions
  5. E.5. String handling
  6. 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 earned over $14 million writing, 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