0% found this document useful (0 votes)
624 views

Think Python Chapter 1

This chapter introduces fundamental concepts of computer programming including: - Programming involves problem solving by breaking problems into steps and expressing the solution as code. - Python is an interpreted programming language that can be used interactively or in scripts. Interpreters process code line-by-line while compilers convert entire programs at once. - All programs involve basic elements like input, output, math operations, conditional execution, and repetition. Debugging is the process of finding and fixing errors. - Formal languages like programming languages have strict rules about syntax that differ from natural languages.

Uploaded by

Joe Meyer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
624 views

Think Python Chapter 1

This chapter introduces fundamental concepts of computer programming including: - Programming involves problem solving by breaking problems into steps and expressing the solution as code. - Python is an interpreted programming language that can be used interactively or in scripts. Interpreters process code line-by-line while compilers convert entire programs at once. - All programs involve basic elements like input, output, math operations, conditional execution, and repetition. Debugging is the process of finding and fixing errors. - Formal languages like programming languages have strict rules about syntax that differ from natural languages.

Uploaded by

Joe Meyer
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Think Python, Chapter 1

Think Python:
How to Think Like a Computer Scientist
Allen B. Downey

http://www.greenteapress.com/thinkpython/thinkpython.pdf

CS 1400: Fundamentals of Computing

Prof. Cindy Marling

Chapter 1: The Way of the Program

This book aims to teach you to think like a computer scientist


These thought processes draw from mathematics, engineering, and
natural sciences
Problem solving is the most important skill
Computer scientists must formulate problems, find creative
solutions to problems, and express these solutions clearly
Writing computer programs requires problem solving
Programming is a useful practical skill
Programming helps people become better problem solvers

CS 1400: Fundamentals of Computing

Prof. Cindy Marling

The Python Programming Language

Python is a high-level programming language


Most computer programs are written in high-level
programming languages and then converted into lowlevel forms that computers can execute
This conversion is accomplished by other computer
programs, interpreters and compilers
Python is an interpreted language
An interpreter reads a Python program and does
what the program says
It processes the program a little at a time,
executing the lines of the program as it reads
them

CS 1400: Fundamentals of Computing

Prof. Cindy Marling

More on Python

Languages can be interpreted, like Python, or compiled


A compiler reads and converts a whole program before it starts running

Interpreters allow more flexibility, while compilers optimize speed


Python can be used in interactive mode or in script mode
In interactive mode, you type Python programs directly into the
interpreter, and the interpreter shows the results
>>> 1 + 1
2
The interpreter displays a prompt, >>>, to show that it is ready to
process what you type

CS 1400: Fundamentals of Computing

Prof. Cindy Marling

Pythons Script Mode

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!

CS 1400: Fundamentals of Computing

Prof. Cindy Marling

What is a Program?

A program is a sequence of instructions that specifies how to perform a


computation
In other words, when you write a program, you tell the computer
what to do
The computer can do mathematical computations, computations
that create and display presentation slides (like these),
computations that play music, plot maps, or whatever else you can
figure out how to tell it to do!

CS 1400: Fundamentals of Computing

Prof. Cindy Marling

Basic Instructions in Computer Programs

Different programming languages have different details, but the basic


instructions are similar in all of them. These are:
Input: Get data from a computer keyboard, file, or other device
Output: Display data on a screen, or send it to a file or device
Math: Perform operations like addition and subtraction
Conditional Execution: Check for certain conditions and execute the
code that applies to the current condition
Example: If severe weather is expected, display an alert on a cell
phone. (But dont display alerts on bright, sunny days.)
Repetition: Perform some instruction repeatedly, usually with some
variation
Example: Send a welcome message to every new freshman, but
vary the email address for each message

CS 1400: Fundamentals of Computing

Prof. Cindy Marling

What is Debugging?

Debugging is the process of finding and fixing errors in programs


It would be great if we all wrote error-free code, but even the best
programmers run up against bugs!
The first computer bug was an actual moth, found in a US Navy
computer by Admiral Grace Hoppers team, in 1947. This moth is in the
Smithsonian Institutions National Museum of American History.

CS 1400: Fundamentals of Computing

Prof. Cindy Marling

Three Kinds of Bugs

Syntax Errors are mistakes in the form of the program


These are like spelling or punctuation mistakes in essays
The interpreter will display an error message instead of running
your program
Runtime Errors are exceptions that occur after the program starts
running
The syntax was OK, but perhaps the program cant access a
resource it needs, like a file or a database
The program will start running, but it will be unable to finish and
will crash
Semantic Errors occur when the program gives the wrong answer or
does something it shouldnt do
The computer does what you say, not what you mean
The program will run to completion, but you will not get the
results you want
CS 1400: Fundamentals of Computing

Prof. Cindy Marling

The Debugging Process

Debugging is something like detective work


Look for clues to figure out why the program doesnt work
Debugging is something like experimental science
Develop a hypothesis about what went wrong, modify the program,
and try again
If the program still doesnt work, come up with a new hypothesis,
and start again
Debugging may seem very different from other intellectual pursuits
It can bring out strong emotions
It is natural for people get frustrated, angry, sad, or embarrassed
The computer may seem like an opponent, rather than a tool
When this happens:
Try to stay calm (Take a break, if you have to)
Practice your problem-solving skills
Experience satisfaction once bugs are found and fixed
CS 1400: Fundamentals of Computing

Prof. Cindy Marling

10

Formal Languages vs. Natural Languages

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

Prof. Cindy Marling

11

More on Language Differences

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.

CS 1400: Fundamentals of Computing

Prof. Cindy Marling

12

And Still More on Languages

Natural language may have degrees of ambiguity, redundancy, and


literalness consider Poetry vs. Prose
Poetry
Words are used for sound as well as meaning
Creative understandings are more important than literal meanings
Poems may be intentionally ambiguous to evoke a response
Prose
The literal meaning of words is more important than in poetry
The structure contributes more to the meaning than in poetry
Prose is easier to analyze than poetry, but there may still be
multiple understandings
Programs
Unambiguous and literal
Can be understood entirely by analyzing tokens and structure

CS 1400: Fundamentals of Computing

Prof. Cindy Marling

13

The First Program

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!

CS 1400: Fundamentals of Computing

Prof. Cindy Marling

14

About the Glossary

There is a glossary of important vocabulary terms at the end of each


chapter in Think Python
Be sure to review the glossary!
A common vocabulary helps us to communicate more effectively
Terms from the glossary will appear on quizzes and exams

CS 1400: Fundamentals of Computing

Prof. Cindy Marling

15

You might also like