Think Python Chapter 1
Think Python Chapter 1
Think Python:
How to Think Like a Computer Scientist
Allen B. Downey
http://www.greenteapress.com/thinkpython/thinkpython.pdf
More on Python
When programs grow longer than a few lines, it is handy to keep them
in files, called scripts
You can open a script in the interpreter to use it without typing it all in
Our Python environment, IDLE, has a File drop-down menu, which
allows you to Open a Python script, much as you would open a file in
Microsoft Word (or the software package of your choice)
It has a Run drop-down menu, with a Run Module option, that tells the
interpreter to execute the script that you opened
This will become Extremely Clear shortly!
What is a Program?
What is Debugging?
10
Natural languages are the ones people naturally speak, like English and
Chinese
Formal languages are designed by people for specific purposes, like
notations for expressing mathematical formulas or representations of
the chemical structure of molecules
Programming languages are formal languages designed to express
computations
Formal languages have strict rules about syntax
3 + 3 = 6 and H20 are syntactically valid
3 + = @ 6 and 2HO are not
Syntax includes tokens (basic elements) and structure (arrangement)
3, +, =, and 6 are valid mathematical tokens, but @ is not
2HO has invalid structure, because subscripts must come after, not
before, the names of elements in chemical formulas
Figuring out the structure of a statement, in a formal language, or a
sentence, in a natural language, is called parsing
CS 1400: Fundamentals of Computing
11
People who grew up speaking natural languages (all of us!) may have
trouble adjusting to formal languages
Here are some differences to keep in mind
Ambiguity: Natural language has ambiguity, which people deal with
through context. Example: Ill get it. Ill understand it? Ill obtain it?
Ill answer it? In a formal language, a statement must have only one
meaning.
Redundancy: To make up for ambiguity, natural languages may
employ redundancy, or be verbose. Formal languages are more
concise. It takes longer to read and understand a program than the
same amount of text in a natural language.
Literalness: Natural languages use idiom and metaphor. Examples:
Raining cats and dogs; Costs an arm and a leg. Formal languages
mean exactly what they say.
12
13
Traditionally, the first program you write in any language is the Hello,
World! program
This program writes the words Hello, World! to the screen
Here it is in Python:
>>> print 'Hello, World!'
Hello, World!
14
15