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 mathematics ,computer use formal languages to denote
ideas .(specifically computations). like engineers ,they design things ,
assembling components into systems and evaluating tradeoffs among alternatives .
like scientist ,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.
express a solutions clearly and accurately. as it turns out. the process of program
is an excellent opportunity to practice problem-solving skills. that’s way this Chapter
is called .”the way of the program”
on the 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
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 something graphical. like processing an image
or playing a video.
the detail look different in different languages. but a few basic instructions appear in just about
get data from keyboard , a file , the network or some other device.
Display data on the screen, save it in a file , send it over the network , etc.
Perform basic mathematical operations like addition and multiplication.
check for certain conditions and run the appropriate code.
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,it made up of instructions that look pretty
much like these . so you can think of programming as the process of breaking a large.
complex task into smaller subtasks until the subtasks are simple enough to be performed
with one of these basic instructions.
one of the challenges of getting started with python is that you might have to install python
and related software on your computer.if you are familiar with your operating system . and
especially if you are comfortable with the command-line interface. you will have no trouble installing
python .But for beginners,it can be painful to learn about system administration and programming
at the same time.
To avoid that problem , i recommend that you start out running Python in a browser. Later, when you
are comfortable with python , i’ll make suggestions for installing python on your computer.
there are a number of web pages you can use to run Python. if you already have a favorite ,go ahead and
use it .otherwise i recommend python Anywhere.i provide detailed instructions for getting started at http://tinyurl.computer
There are two versions of Python ,called python 2 and python 3 . They are very similar ,so if you
learn one ,it is easy to switch to the other . in fact ,there are only a few differences you will
encounter as a beginners .this book is written for python 3 , but i include some notes about python 2.
the python interface is a program that reads and executes python code .Depending on your environment.you
might start the interface by clicking on an icon. or by typing python on a command line .When it starts,
you should see output like this:
The first three lines contain information about the interpreter and the operating system it’s running on .
so it might be different for you .But you should check the version number, which is 3.4.0 in this example,begins
with 3 ,which indicates you are running python 3, if it begins with 2, you are running python 2.
the last line is a prompt the indicates that the interpreter is ready for you to enter code ,if you type a line
of code and Hit Enter.the interpreter displays the result:
Now you’re ready to get started ,From here on , I assume that you know how to start the Python interpreter and run code.
Traditionally ,the first program you write in a new languages is called “Hello World” ,because all it does is Display the words”hell,world!”
in python ,i looks like this:
this is an example of a print statement , although it doesn’t actually print anything on paper , it displays a result
on the screen . in this case ,the result is the words .
the quotation marks in the programs mark the beginning and end of the text be displayed , they don’t appear in the result.
the parentheses indicate that print is a function ,we’ll get to functions in Chapter 3.
in python 2 ,the print statement is sightly different . it is not a function .so it doesn’t use parentheses.
this distinction will make more sense soon , but that’s enough to get started .
After ‘hello,world’ ,the next step is Arithmetic . python provides Operators,which are special symbols that represent
computations like addition and multiplication.
The operate +.- and * perform addition ,subtraction and multiplication , as in the following examples:
the operate /performs division:
you might wonder why the results is 42.0 instead of 42, I’ll explain in the next section .
Finally,the operator ** performs exponentiation , that is ,it raises a number to a power .
in some other languages , ^ is used for exponentiation .but in python it is a bitwise operator called XOR . if your are
not similar with bitwise operators ,the result will surpise you.
i won’t cover bitwise operators in this book,but you can read about them at
A value is one of the basic things a program works with , like a letter or a number ,some values we have seen
so far are 2,42.0 , and ‘hello world’.
These values belong to different types: 2 is an integer ,42.0 is a floating-point number,and ‘hello world’ is a string.
so-called because the letters it contains are strung together.
if you are not sure what type a value has ,the interpreter call tell you.
in these results , the word ‘class’ is used in the sense of a category, a type is a category of values.
Not surprisingly ,integer belong to the type int , string belong to str ,and float-point numbers belongs to float.
what about values like ‘2’ and ‘42.0’? they look like numbers , but they are in quotation marks like strings.
when you type a large integer , you might be tempted to use commas between groups of digits , as in 1,000,000 .
this is not a legal integer in python ,but it is legal:
that’s not what we expected at all ! Python interpreter 1,000,000 as a comma-separated sequence of integers .we will
learn more about this kind of sequence later.
Natural languages are the languages people speak, such as English, Spanish and French ,they were not designed by people.
(although people try to impose some order on them ,) they evolved naturally.
Formal languages are languages that are designed by people for specific applications , For example ,the notation that mathematics
use is a formal language that is particularly good at denoting relationship among numbers and symbols . chemists use a formal
language to represent the chemical structure of molecules . And most importantly.