How to Think Like a Computer Scientist: Learning with ...
How to Think Like a Computer Scientist: Learning with Python 3
Documentation
Release 3rd Edition
Peter Wentworth, Jeffrey Elkner, Allen B. Downey and Chris Meyers
Apr 17, 2020
1 The way of the program 2 Variables, expressions and statements 3 Program Flow 4 Functions 5 Data Types 6 Numpy 7 Files 8 Modules 9 More datatypes 10 Recursion 11 Classes and Objects 12 Exceptions 13 Fitting 14 PyGame 15 Plotting data with matplotlib 16 Copyright Notice 17 Contributions A Modules B More datatypes C Recursion
Contents
3 11 23 63 91 133 139 145 157 161 175 213 219 225 245 257 259 263 275 279
i
D Classes and Objects
293
E Exceptions
331
F Fitting
337
G PyGame
343
H Plotting data with matplotlib
363
Index
375
ii
How to Think Like a Computer Scientist: Learning with Python 3 Documentation, Release 3rd Edition
3rd Edition (Using Python 3.x) by Jeffrey Elkner, Peter Wentworth, Allen B. Downey, and Chris Meyers illustrated by Dario Mitchell
? Copyright Notice ? Contributor List ? Chapter 1 The way of the program ? Chapter 2 Variables, expressions, and statements ? Chapter 3 Program Flow ? Chapter 4 Functions ? Chapter 5 Datatypes ? Chapter 6 Numpy ? Chapter 7 File I/O ? Appendix A Writing Your Own Modules ? Appendix B More datatypes ? Appendix C Recursion ? Appendix D Object Oriented Programming ? Appendix E Exceptions ? Appendix F Fitting and Scientific Data Handling ? Appendix G PyGame ? Appendix H Plotting with matplotlib ? GNU Free Document License
Contents
1
How to Think Like a Computer Scientist: Learning with Python 3 Documentation, Release 3rd Edition
2
Contents
1 CHAPTER
The way of the program
The goal of this book is to teach you to think like a computer scientist. This way of thinking combines some of the best features of mathematics, engineering, and natural science. Like mathematicians, computer scientists use formal languages to denote ideas (specifically computations). Like engineers, they design things, assembling components into systems and evaluating tradeoffs among alternatives. Like scientists, they observe the behavior of complex systems, form hypotheses, and test predictions. The single most important skill for a computer scientist is problem solving. Problem solving means the ability to formulate problems, think creatively about solutions, and express a solution clearly and accurately. As it turns out, the process of learning to program is an excellent opportunity to practice problem-solving skills. That's why this chapter is called, The way of the program. On one level, you will be learning to program, a useful skill by itself. On another level, you will use programming as a means to an end. As we go along, that end will become clearer.
1.1 The Python programming language
The programming language you will be learning is Python. Python is an example of a high-level language; other high-level languages you might have heard of are C++, PHP, Pascal, C#, and Java. As you might infer from the name high-level language, there are also low-level languages, sometimes referred to as machine languages or assembly languages. Loosely speaking, computers can only execute programs written in lowlevel languages. Thus, programs written in a high-level language have to be translated into something more suitable before they can run. Almost all programs are written in high-level languages because of their advantages. It is much easier to program in a high-level language so programs take less time to write, they are shorter and easier to read, and they are more likely to be correct. Second, high-level languages are portable, meaning that they can run on different kinds of computers with few or no modifications. The engine that translates and runs Python is called the Python Interpreter: There are two ways to use it: immediate mode and script mode. In immediate mode, you type Python expressions into the Python Interpreter window, and the interpreter immediately shows the result:
3
How to Think Like a Computer Scientist: Learning with Python 3 Documentation, Release 3rd Edition
The >>> is called the Python prompt. The interpreter uses the prompt to indicate that it is ready for instructions. We typed 2 + 2, and the interpreter evaluated our expression, and replied 4, and on the next line it gave a new prompt, indicating that it is ready for more input.
Alternatively, you can write a program in a file and use the interpreter to execute the contents of the file. Such a file is called a script. Scripts have the advantage that they can be saved to disk, printed, and so on.
Working directly in the interpreter is convenient for testing short bits of code because you get immediate feedback. Think of it as scratch paper used to help you work out problems. Anything longer than a few lines should be put into a script.
When you are writing a script you need something like a text editor. With this we mean a program on your computer that changes text files, not something that also does layout like a word processor such as Microsoft Word or LibreOffice Writer. A few examples of text editors are Notepad, Notepad++, vim, emacs and sublime.
For Python (and many other programming languages) there are programs that include both a text editor and a way to interact with the interpreter. We call these development environments (sometimes Integrated Development Environment or IDE). For Python these can include (among many others) Spyder, Thonny or IDLE. There are also development environments that run in your browser. One example of this is Jupyter Notebook.
The choice of development environment is quite personal, but if you are following a course based on this book the teacher will probably recommend one. That recommendation is handy to follow if you need help using the environment.
The important thing to remember is that Python itself does not care in what editor you write your code. As long as you write correct syntax (with the right tabs and spaces) Python can run your program. The editor is only there to help you.
1.2 What is a program?
A program is a sequence of instructions that specifies how to perform a computation. The computation might be something mathematical, such as solving a system of equations or finding the roots of a polynomial, but it can also be a symbolic computation, such as searching and replacing text in a document or (strangely enough) compiling a program. The details look different in different languages, but a few basic instructions appear in just about every language: input Get data from the keyboard, a file, or some other device such as a sensor. output Display data on the screen or send data to a file or other device such as a motor. math Perform basic mathematical operations like addition and multiplication. conditional execution Check for certain conditions and execute the appropriate sequence of statements. repetition Perform some action repeatedly, usually with some variation. Believe it or not, that's pretty much all there is to it. Every program you've ever used, no matter how complicated, is made up of instructions that look more or less like these. Thus, we can describe programming as the process of
4
Chapter 1. The way of the program
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- aruba cx 8325 switch series data sheet
- how to think like a computer scientist learning with
- pyqt rxjs ggplot2 python data persistence caffe2
- how to think like a computer scientist
- aruba cx 8320 switch series data sheet
- black hat python linux security linux server
- conda cheat sheet
- 7150 series 1 10 gbe sfp ultra low latency switch
- amazon web services tutorial rxjs ggplot2 python data
Related searches
- how to think of a good username
- how to clean like a professional housekeeper
- how to think more strategically
- how to shut down my computer safely
- how to think about yield to maturity
- how to think of a creative title
- how to do screenshot of computer screen
- think like a champion pdf
- how to think for myself
- how to think clearly
- how to think properly
- how to figure out your computer specs