How to Think Like a Computer Scientist: Learning with Python Documentation Release 2nd Edition

Jeffrey Elkner, Allen B. Downey and Chris Meyers

February 22, 2017

CONTENTS

1 Learning with Python3

Index 295

i ii How to Think Like a Computer Scientist: Learning with Python Documentation, Release 2nd Edition

CONTENTS 1 How to Think Like a Computer Scientist: Learning with Python Documentation, Release 2nd Edition

2 CONTENTS CHAPTER ONE

LEARNING WITH PYTHON

2nd Edition (Using Python 2.x) by Jeffrey Elkner, Allen B. Downey, and Chris Meyers Last Updated: 21 April 2012 • Copyright Notice • Foreword • Preface • Contributor List • Chapter 1 The way of the program • Chapter 2 Variables, expressions, and statements • Chapter 2b A light look at lists and looping • Chapter 3 Functions • Chapter 4 Conditionals • Chapter 5 Fruitful functions • Chapter 6 Iteration • Chapter 7 Strings • Chapter 8 Case Study: Catch • Chapter 9 Lists • Chapter 10 Modules and files • Chapter 11 Recursion and exceptions • Chapter 12 Dictionaries • Chapter 13 Classes and objects • Chapter 14 Classes and functions

3 How to Think Like a Computer Scientist: Learning with Python Documentation, Release 2nd Edition

• Chapter 15 Classes and methods • Chapter 16 Sets of Objects • Chapter 17 Inheritance • Chapter 18 Linked Lists • Chapter 19 Stacks • Chapter 20 Queues • Chapter 21 Trees • Appendix A Debugging • Appendix B GASP • Appendix Configuring Ubuntu for Python Development • Appendix Customizing and Contributing to the Book • GNU Free Document License

Copyright Notice

Copyright (C) Jeffrey Elkner, Allen B. Downey and Chris Meyers. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with Invariant Sections being Foreward, Preface, and Contributor List, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled “GNU Free Documentation License”.

Foreword

By David Beazley As an educator, researcher, and book author, I am delighted to see the completion of this book. Python is a fun and extremely easy-to-use that has steadily gained in popu- larity over the last few years. Developed over ten years ago by Guido van Rossum, Python’s simple syntax and overall feel is largely derived from ABC, a teaching language that was developed in the 1980’s. However, Python was also created to solve real problems and it borrows a wide variety of features from programming languages such as C++, Java, Modula-3, and Scheme. Because of this, one of Python’s most remarkable features is its broad appeal to professional software developers, scientists, researchers, artists, and educators.

4 Chapter 1. Learning with Python How to Think Like a Computer Scientist: Learning with Python Documentation, Release 2nd Edition

Despite Python’s appeal to many different communities, you may still wonder why Python? or why teach programming with Python? Answering these questions is no simple task—especially when popular opinion is on the side of more masochistic alternatives such as C++ and Java. However, I think the most direct answer is that programming in Python is simply a lot of fun and more productive. When I teach computer science courses, I want to cover important concepts in addition to making the material interesting and engaging to students. Unfortunately, there is a tendency for introduc- tory programming courses to focus far too much attention on mathematical abstraction and for students to become frustrated with annoying problems related to low-level details of syntax, com- pilation, and the enforcement of seemingly arcane ru