How to Think Like a Computer Scientist: Learning with Python 3 Documentation

Categories:

Recommended

Chapter One – 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 low-level 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:

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.

In this Rhodes Local Edition of the textbook, we use a program development environment called PyScripter. (It is available at http://code.google.com/p/pyscripter.) There are various other development environments. If you’re using one of the others, you might be better off working with the authors’ original book rather than this edition.

For example, we created a file named firstprogram.py using PyScripter. By convention, files that contain Python programs have names that end with .py

To execute the program, we can click the Run button in PyScripter:

Most programs are more interesting than this one.

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.

Category:

Attribution

Peter Wentworth, Jeffrey Elkner, Allen B. Downey, and Chris Meyers (2012), How to Think Like a Computer Scientist: Learning with Python 3 Documentation, URL: https://www.apexcalculus.com/

This work is licensed under GNU Free Documentation License:  (https://www.gnu.org/licenses/fdl-1.3.html).

VP Flipbook Maker

Share your work as digital flipbook with Visual Paradigm Online Flipbook Maker! You can convert your work in other format to flipbook with the tool. You can also choose to create from scratch with the design tool. Try it now!