Chapter 1 Notes- Think Python
Chapter 1
The way of the Program
Aim is to think like a computer scientist.
This includes some of the best features of mathematics, engineering, and natural science.
Like in math, computer scientists use formal languages to denote ideas (specifically computations)
Like engineers, they (computer scientists) 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 most important skill for a computer scientist is problem solving.
Problem solving:
ability to formulate problems
think creatively about solutions
express a solution clearly and accurately
1.1 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, or it could 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 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, the network, or some other device
output: display data on the screen, save it in a file, send it over the network, etc.
math: Perform basic mathematical operations liked addition and multiplication
Conditional Execution: check for certain conditions and run the appropriate code
repetition: Perform some action repeatedly, usually with some variation
Most programs are made up of instructions just like those (input, output, math, conditional execution, repetition)
You can think of programing as the process of breaking a large, complex task into smaller and smaller subtasks until the subtasks are simple enough to be performed with one of the basic instructions (input, output, math, conditional execution, repetition)
1.2 Running Python
A challenge with getting started with Python is that you have to install Python and related software on your computer.
To do so, you'd need to be familiar with your operating system and the command-line interface
For beginners, it can be painful to learn about system administration and programming at the same time
To begin, use PythonAnywhere, in a browser
There 2 versions of Python. They are very similar.
Python 2
Python 3
The Python Interpreter: a program that reads and executes Python code. When it starts, you should see output like this:
Python 3.4.0 (default, Jun 19 2015, 14:20:21)
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
The first three lines contain information about the interpreter and operating system it's running on (it's likely to vary per user). You should check that the version number, which is in that example "3.4.0," begins with 3, which indicates that you're using Python 3.
If you saw something like "2.4.0," the 2 would indicate that you're using Python 2
The last line is a prompt that indicates that the interpreter is ready for you to ender code. If you type a line of code and hit Enter, the interpreter displays the result:
>>> 1+1
2
1.3 The first program
Usually the first program you write in a new language is called, "Hello, World!" because all it does is display the words "Hello, World!" In Python, it looks like this:
>>> print ('Hello, World!')
This is an example of a print statement
It doesn't actually print anything on paper. It displays a result on the screen. In this case, the result is the words
Hello, World!
The quotation marks in the program mark the beginning and end of the text to be displayed
The parentheses indicate that print is a function. Functions will be discussed in Chapter 3.
In Python 2, the print statement is slightly different, it is not a function, so it doesn't use parentheses.
>>> print 'Hello, World!'
1.4 Arithmetic operators
After "Hello, World", the next step is arithmetic.
Python provides operators
the special symbols that represent computations like addition and multiplication.
The operators, +, -, and * perform addition, subtractions and multiplication as in the following examples:
>>> 40+2
42
>>>43-1
42
>>>6*7
42
>>>84/2
42.0
The operator ** performs exponentiation; That is, it raises a number to a power:
>>>6**2
42
Other languages use ^ for exponentiation, but Python in Python, ^ is a bitwise operator called XOR. Bitwise operators will not be covered in this book, but to learn more, you can read about them at:
https://wiki.python.org/moin/BitwiseOperators
1.5 Values and Types
A value is one of the basic things a program works with, like a letter or a number
There are different types of values
2 is an integer
42.0 is a floating-point number
'Hello, World!' is a string
so called because the letters it contains are strung together
The interpreter can tell you what type a value has
>>> type(2)
<class 'int'>
>>> type (42.0)
<class 'float'>
>>> type ('Hello, World!')
<class 'str'>
In these results, the word "class" is used in the sense of a category; a type is a is a category of values
integers belong to the type int, strings belong to str, and floating-point numbers belong to float
What about values like '2' and '42.0'? They look like numbers, but they are in quotation marks like strings.
>>> type('2')
<class 'str'>
>>> type ('42.0')
<class 'str'>
They're strings.
When you type a large integer, you may 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:
>>>1,000,000
(1, 0, 0)
Python interprets 1,000,000 as a comma-separated sequence of integers.
1.6 Formal and natural languages
Natural languages are the languages people speak
such as English, Spanish, Chinese
Natural languages were not designed by people, they evolved naturally
Formal languages are languages that are designed by people for specific applications
mathematicians use a formal language that is good for denoting relationships among numbers and symbols.
chemists use a formal language that helps to represent the chemical structure of molecules
programming languages are formal languages that have been designed to express computations
Formal languages tend to have strict syntax rules
Syntax rules govern the structure of statements
For math,
3+3=6
correct syntax
3+=3$6
does not have correct syntax
There are two flavors of syntax rules- pertaining tokens and structure
tokens are the basic elements of the language, such as words, numbers and chemical elements.
One of the problems with 3+=3$6 is that $ is not a legal token in mathematics
structure is how the tokens are combined
3+=3
illegal because even though each token is legal, you can't have one right after the other
When you read a language (natural or formal) you have to figure out (sometimes subconsciously) the structure
This process is called parsing
Differences between formal and natural languages
ambiguity: Natural languages are very ambiguous. Formal languages are designed to be unambiguous.
Any statement has exactly one meaning, regardless of context
redundancy: to make up for ambiguity, and reduce misunderstandings, natural languages employ a lot of redundancy. As a result of being unambiguous, formal languages are less redundant and more concise.
literalness: natural languages are full of idiom and metaphor. Formal languages mean exactly what they say.
The difference between formal and natural languages is like the difference between poetry and prose, but more so:
poetry: words are used for their sound as well for their meaning, and the whole poem together creates an effect or emotional response. Ambiguity is not only common, but often deliberate
prose: the literal meaning of words is more important, and the structure contributes more meaning. Prose is more amenable to analysis than poetry but still often ambiguous.
programs: the meaning of a computer program is unambiguous and literal, and can be understood entirely by analysis of the tokens and structure.
Formal languages are more dense than natural languages.
Also, the structure is very important in formal languages
it isn't always best to read from top to bottom, left to right.
learn to parse the program in your head, identifying the tokens and interpreting the structure
details matter
small errors in spelling and punctuation make a big difference in formal language.
1.7 Debugging
Programming errors are called bugs
The process of tracking down bugs is called debugging
1.8 Glossary
Problem solving: the process of formulating a problem, finding a solution, and expressing it
High-level language: a programming language like Python that is designed to be easy for humans to read and write
Low-level language: a programming language that is designed to be easy for a computer to run; also called "machine language" or "assembly language"
Portability: a property of a program that can run on more than one kind of computer
Interpreter: a program that reads another program and executes it
Prompt: Characters displayed by the interpreter to indicate that it is ready to take input from the user
Program: a set of instructions that specifies a computation
Print Statement: an instruction that causes the Python interpreter to display a value on the screen
Operator: a special symbol that represents a simple computation like addition, multiplication, or string concatenation
Value: one of the basic units of data, like a number or string that a program manipulates
Type: a category of values. The types we have seen so far are integers (type int), floating-point numbers (type float) and strings (type str)
Integer: a type that represents whole numbers
Floating-point: a type that represents numbers with fractional parts
String: A type that represents sequences of characters
Natural language: Any one of the languages that people speak that evolved naturally
Formal Language: Any one of the languages that people have designed for specific purposes, such as representing mathematical ideas or computer programs; all programming languages are formal languages
Token: One of the basic elements of the syntactic structure of a program, analogous to a word in a natural language
Syntax: The rules that govern the structure of a program
Parse: To examine a program and analyze the syntactic structure
Bug: An error in a program
Debugging: The process of finding and correcting bugs
So I’m bummed this took out all of my formatting. I’ll have to look into seeing if there’s a better way of posting!