Think Python Chaper 1: New terms + notes

NOTES on Chapter 1: The way of the program

1.1) Python programming language:

Low-level language sometimes referred to as “machine” or “assembly” languages
Disadvantages: Can run on only one kind of computer and have to be re-written to run on another.

Source code -> Interpreter -> Output

High-level language. Example: Python, C, C++, Java
Advantages: Easier to program, take less time to write, shorter and easier to read, are portable (meaning they can run on different kinds of computers)

Source code -> Compiler -> Object code -> Executor -> Output

Python is considered an interpreted language because Python programs are executed by an interpreter.

There are 2 ways to use the interpreter: interactive mode and script mode.

Interactive mode: You type Python programs and the interpreter displays the result. Example:
>>> 1 + 1 (you type)
2 (the interpreter replies)

Store code in a file -> Use the interpreter to execute the contents of the file

Content of a file = Script
By convention, Python scripts have names that end with .py

To execute the script, you have to tell the interpreter the name of the file. If you have   a script named mba.py and you are working in a UNIX command window, you type: python mba.py
In other development environments, the details of executing scripts are different.
For help and instructions: http://python.org

Program:

  • Input
  • Output
  • Math
  • Conditional execution
  • Repetition

Programing is the process of breaking a large, complex task into smaller and smaller substasks until the subtasks are simple enough to be performed with one of these basic instructions.

1.3) What is debugging?

– Syntax errors
Example: “1+2” is correct. However, “+7^” is a syntax error.

English language can tolerate syntax errors, think of some poems (e.e. cummings)
Python is not so forgiving. If there is a single syntax error anywhere in your program, Python will display an error message and quit, and you will not be able to run your program.

– Runtime errors: Do not appear until after the program has started running. These errors are also called exceptions. Are rare (… and bad).

– Semantic errors:
If there is a semantic error in your program, it will run successfully in the sense that the computer will not generate any error messages, but it will not do the right thing. It will do something else.

The problem is that the program you wrote is not the program you wanted to write. The meaning of the program (its semantics) is wrong.

Identifying semantic errors can be tricky because it requires you to work backward by looking at the output of the program and trying to figure out what it is doing.

1.3.4) Experimental debugging

In some ways, debugging is like detective work. You are confronted with clues, and you have to infer the process and events that led to the results you see.

Debugging is also like experimental science: Hypothesis (you have an idea of what is going wrong)  – Modification (or experiment) – Your hypothesis is either correct (in that case you can predict the result) or wrong (and you have to come with a new idea).

1.4) Formal and natural languages

Natural languages (such as English, Spanish, etc.): evolve naturally, are full of ambiguity (which people deal with by using contractual clues and other information), employ lots of redundancy (as a result, they are often verbose), are full of idiom and metaphor,

Formal languages: designed by people for specific applications, have strict rules about syntax, are designed to be nearly or completely unambiguous (which means that any statement has exactly one meaning, regardless of context), are less redundant and more precise, formal languages mean exactly what the say,

Syntax rules come in two categories: tokens (basic elements of the language such as words, numbers, chemical elements, etc.) and structure of a statement (the way the tokens are arranged).

Exemple 1.1:

– Hte huose is rde.

– Red the is house.

Parsing: When you read a sentence in English or a statement in a formal language, you have to figure out what the structure of the sentence is.

For instance: The penny dropped

The penny = the subject
dropped = the predicate

Programming languages are formal languages that have been designed to express computations.

The chevron, >>>, is the prompt the interpreter uses to indicate that it is ready.

1.5) The first program

I downloaded Python. However, since I never saw what a Python window or interface looked like, I wandered a bit.

At this step, I did not know how to open Python.

Here is how I opened it:

MAC / Utilities / Terminal

Once the Terminal opened; I typed python. A python session started. Bingo

Python 2:

>>>
>>>print ‘Hello, World!’

Python 3:
>>>print (‘Hello World!’)
The parentheses indicate that  print  is a function.

print statement = displays a value on the screen

Exercise 1.2: use print in the Python search window

Excersise 1.3:
>>>help ()
Welcome to Python 2.7!  This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type “quit”.

To get a list of available modules, keywords, or topics, type “modules”,
“keywords”, or “topics”.  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as “spam”, type “modules spam”.

Excersise 1.4: If you run a 10 kilometer race in 43 minutes 30 seconds, what is your average time per mile? What is your average speed in miles per hour? (Hint: there are 1.61 kilometers in a mile).

10km —> 43.5 minutes (… it reminds me of the state diagram)

Step 1)

If,         1.61 km = 1 mile

Then,    10 km = Variable x is the number of miles that correspond to 10km

Variable x = 10 Km * 1 mile
                         1.61 km

In Python:
>>>(10*1)/1.61
6.211180124223602

Therefore, 10 km = 6.21 miles

Step 2)

It is equivalent to say that the average speed in miles/minutes is:

6.21 miles —-> 43.5 minutes

or

6.21 miles —-> 0.725 hour (43.5 minutes/60 minutes per hour)

In Python:

>>>43.5/60
0.725

If we want to know the average speed in miles per hour, then:

6.21 miles —–> 0.725 hour

Variable y ——> 1 hour

Variable y is the miles ran in one hour.

(1 Hour * 6.21 miles)/0.725 hour =

In Python:

>>> (1*6.21)/0.725
8.565517241379311

Rounded to two decimals is 8.57 miles

ANSWER: The average speed in miles per hour is 8.57 miles/hour

Let me know if you find mistakes in my logic.

 

***

Acknowledgments :

These notes represent my understanding from the book Think Python: How to Think Like a Computer Scientist written by Allen B. Downey.

Part of the chapter is transcribed and all the quotes unless specified otherwise come directly from his book.

Thank you Professor Downey for making this knowledge available.

 

Also, I would like to thank the open source community for their valuable contribution in making resources on programming available.

Thank you

One thought on “Think Python Chaper 1: New terms + notes

  1. Pingback: Chapter 8: Strings | Python Project

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s