SlideShare a Scribd company logo
Chapter 1: Introduction
This chapter addresses the question “What is computer science?” We begin by introducing
the essence of computational problem solving via some classic examples. Next, computer
algorithms, the heart of computational problem solving, are discussed. This is followed by a
look at computer hardware (and the related issues of binary representation and operating
systems) and computer software (and the related issues of syntax, semantics, and program
translation). The chapter finishes by presenting the process of computational problem
solving, with an introduction to the Python programming language.
1
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons
Computing technology has changed—and is continuing to change—the world.
Essentially every aspect of life has been impacted by computing. Computing
related fields in almost all areas of study are emerging.
2
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons
Motivation
Computer science is fundamentally about computational problem solving.
Programming and computers are only tools in the field of computing. The field
has tremendous breadth and diversity. Areas of study include:
3
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
• Software Engineering
• Database Management / Data Mining
• Computer Networks
• Computer Graphics
• Computer Simulation
• Information Security
• Programming Language Design
• Systems Programming
• Computer Architecture
• Human–Computer Interaction
• Robotics
• Artificial Intelligence
What is Computer Science?
Computational Problem Solving
Two things that are needed to perform
computational problem solving:
• a representation that captures all the
relevant aspects of the problem
• an algorithm that solves the problem
by use of the representation
4
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
Thus, computational problem solving finds a solution within a
representation that translates into a solution for what is being represented.
The Use of Abstraction in
Computational Problem Solving
A representation that leaves out detail of what is being represented is
a form of abstraction.
Abstraction is prevalent in the everyday world. For example, maps are
abstract representations.
5
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
6
Below is the original 1908 map of the London Underground (Subway).
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
7
Below is a more abstract, but topologically correct, map of the London Underground
subway system. It shows the bends and curves of each track.
This map contains too much information for its purpose—that is, to find out where
each subway line leads, and where the connections are between lines.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
8
Below is a more abstract representation of the subway system, developed
by Harry Beck in 1931. The track lines are straightened out where the track
curves are irrelevant for subway riders. This is a simpler, easier to read, and
thus a better representation for its purpose.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
9
This particular abstraction is still in use today.
Washington D.C. Metro Map
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
10
An interesting form of abstraction is the creative work of Chris Jordan,
which allows the viewer to control the degree of abstraction of the work.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
11
Cans Seurat, 2007 60x92“ Copyright Chris Jordan
Depicts 106,000 aluminum cans, the number used in the U.S. every thirty seconds.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
12
Clicking on the image causes the picture to zoom in, showing more detail.
(Copyright Chris Jordan)
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
13
Zooming in closer …
(Copyright Chris Jordan)
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
14
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
(Copyright Chris Jordan)
15
www.chrisjordan.com/current_set2.php
More
Images
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
Abstraction in Computing
Abstraction is intrinsic to computing and computational problem
solving.
• The concept of “1s” and “0s” in digital computing is an abstraction
digital information is actually represented as high or low voltage
levels, magnetic particles oriented one of two ways, pits on an optical
disk, etc.
• Programming languages are an abstraction
the instructions and data of a computer program is an abstract
representation of the underlying machine instructions and storage
• Programming design involves the use of abstraction
programs are conceptualized as various modules that work together
16
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
Man, Cabbage, Goat, Wolf Problem
17
A man lives on the east side of a river. He wishes
to bring a cabbage, a goat, and a wolf to a village
on the west side of the river to sell. However, his
boat is only big enough to hold himself, and
either the cabbage, goat, or wolf. In addition,
the man cannot leave the goat alone with the cabbage because the goat will eat
the cabbage, and he cannot leave the wolf alone with the goat because the wolf
will eat the goat. How does the man solve his problem?
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
18
There is a simple algorithmic approach for solving this problem by simply trying all
possible combinations of items that may be rowed back and forth across the river.
Trying all possible solutions is referred to as a brute force approach.
What would be an appropriate representation for this problem? Whatever
representation we use, only the aspects of the problem that are relevant for its
solution need to be represented.
• Color of the boat?
• Name of the man?
• Width of the river?
The only information relevant for this problem is where each particular item is at
each step in the problem solving. Therefore, by the use of abstraction, we define a
representation that captures only this needed information.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
19
For example, we could use a sequence to indicate where each of the objects
currently are,
man cabbage goat wolf boat village
[ e a s t , w e s t , e a s t , w e s t , e a s t , w e s t ]
where it is understood that the first item in the sequence is the location of the man,
the second the location of the cabbage, etc.
Note that the village is always on the west side of the river—it doesn’t move! Its
location is fixed and therefore does not need to be represented.
Also, the boat is always in the same place as the man. So representing the location of
both the man and the boat is redundant information. The relevant, minimal
representation is given below,
man cabbage goat wolf
[ E , W , E , E ]
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
20
The actual problem is to determine how the man can row objects across the river,
with certain constraints on which pairs of objects cannot be left alone.
The computational problem is to find a way to convert the representation of the
start state of the problem, when all the object are on the east side of the river,
man cabbage goat wolf
[ E , E , E , E ]
to the goal state with all objects on the west side of the river,
man cabbage goat wolf
[ W , W , W , W
with the constraint that certain invalid states should never be used.
Thus, in a computational problem solving approach, a problem is solved within the
representation used, in which the solution within the representation must
translate into a solution of the actual problem.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
21
For example, from the start state, there are three possible moves that can be
made, only one of which results in a valid state.
man cabbage goat wolf
[ E , E , E , E ] START STATE
[ W , W , E , E ] [ W , E , W , E ] [ W , E , E , W ]
Man rows cabbage across Man rows goat across Man rows wolf across
INVALID STATE
Goat left alone with wolf
VALID STATE
Cabbage left alone with wolf
INVALID STATE
Cabbage left alone with goat

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
22
We check if the new problem state is the goal state. If true, then we solved the
problem in one step! (We know that cannot be so, but the algorithmic approach that
we are using does not.)
man cabbage goat wolf
[ E , E , E , E ] START STATE
[ W , E , W , E ]
Man rows goat across 
Is goal state [ W , W , W , W ] ? No
Therefore we continue searching from the current state.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
23
Since the man can only row across objects on the same side of the river, there are
only two possible moves from here,
man cabbage goat wolf INTERMEDIATE
[ W , E , W , E ] STATE
[ E , W , E , E ] [ E , E , E , E ]
Man rows back alone Man rows goat across
VALID STATE
Cabbage left alone with wolf
 VALID STATE
BUT, previously in this state. It is
the start state. No progress made!

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
24
This would continue until the goal state is reached,
man cabbage goat wolf
[ E , W , E , E ]
[ W , W , W , W ] GOAL STATE
Thus, the computational problem of generating the goal state from the start state
translates into a solution of the actual problem since each transition between
states has a corresponding action in the actual problem—of the man rowing across
the river with (or without) a particular object.
.
.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
The Importance of Algorithms
25
As another example computational problem,
suppose that you needed to write a program
that displays a calendar month for any given
month and year.
The representation of this problem is rather
straightforward. Only a few values are
needed:
• the month and year
• number of days in each month
• names of the days of the week
• day of the week that the first day of the month falls on
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
26
The month and year, number of days in a month, names of the days of the week can
be easily handled. The less obvious part is how to determine the day of the week
that a particular date falls on.
How would you do that?
Start with a known day of the week for a given year in the past and
calculate forward from there?
That would not be a very efficient way of solving the problem.
Since calendars are based on cycles, there must be a more direct method for doing
this. Thus, no matter how good a programmer you may be, without knowledge of
the needed algorithm, you could not write a program that solves the problem.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
The Limits of
Computational Problem Solving
27
Once an algorithm for a given problem is developed or found, an important question is
“Can a solution to the problem be found in a reasonable amount of time?”
“But aren’t computers very fast, and getting faster all the time?”
Yes, but some problems require an amount of time to compute a solution that is
astronomical compared to the capabilities of current computing devices.
A classic problem in computer science that demonstrates this is the Traveling
Salesman problem.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
The Traveling Salesman Problem
28
A salesman needs to visit a
set of cities. He wants to find
the shortest route of travel,
starting and ending at any
city for a given set of cities,
what route should he take?
The algorithm for solving this problem is a simple one. Determine the lengths of all
possible routes that can be taken, and find the shortest one. That is, by using a
brute force approach. The computational issue, therefore, is for a given set of
cities, how many possible routes are there?
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
29
If we consider a route to be a specific sequence of names of cities, then how many
permutations of that list are there?
New York, Boston, Chicago, San Francisco, Los Angeles, Atlanta
New York, Boston, Chicago, San Francisco, Atlanta, Loa Angeles
New York, Boston, Chicago, Los Angeles, San Francisco, Atlanta
etc.
Mathematically, the number of permutations for n entities is n! (n factorial).
How big a number is that for various number of cities?
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
30
Below are the number of permutations (and this number of routes) there are for
varies numbers of cities:
Ten Cities 10! 3,628, 800 (over three million)
Twenty Cities 20! 2,432,902,008,176,640,000
Fifty Cities 50! Over 1064
If we assume that a computer could compute the routes of one million cities per
second:
• for twenty cities, it would take 77,000 years
• for fifty cities, it would take longer than the age of the universe!
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
The Game of Chess
31
When a computer plays chess against a human
opponent, both have to “think ahead” to the
possible outcomes of each move it may make.
A brute force approach can also be used for a
computer playing a game of chess. A program
can consider all the possible moves that can be
made, each ending in a win, loss, or draw.
It can then select the move that leads to the most number of ways of winning.
(Chess masters, on the other hand, only think ahead a few moves, and
“instinctively” know the value of each outcome.)
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
32
There are approximately 10120 possible chess games that can be played. This is
related to the average number of look-ahead steps needed for deciding each move.
There are approximately,
1080 atoms in the observable universe
and an estimated
3 × 1090 grains of sand to fill the universe solid
Thus, there are more possible chess games that can be played than grains of sand
to fill the universe solid!
Therefore, for problems such as this and the Traveling Salesman problem in which a
brute-force approach is impractical to use, clever and more efficient problem-solving
methods must be discovered that find either an exact or an approximate solution to
the problem.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
0
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
An algorithm is a finite number of clearly
described, unambiguous “doable” steps
that can be systematically followed to
produce a desired result for given input in
a finite amount of time (that is, it
eventually terminates).
The word “algorithm” is derived from the
ninth-century Arab mathematician, Al-
Khwarizmi who worked on “written
processes to achieve some goal.”
40
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
Computer Algorithms
Computer algorithms are central to computer science. They provide
step-by-step methods of computation that a machine can carry out.
Having high-speed machines (computers) that can consistently follow a
given set of instructions provides a reliable and effective means of
realizing computation. However, the computation that a given computer
performs is only as good as the underlying algorithm used.
Because computers can execute a large number of instructions very
quickly and reliably without error, algorithms and computers are a
perfect match!
41
Algorithms and Computers: A Perfect Match
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
An algorithm for computing the greatest common denominator (GCD)
of two given integers. It is one of the oldest numerical algorithms still in
common use.
1. Assign M the value of the larger of the two values.
2. Divide M by N, call the remainder R.
3. If R is not 0, then assign M the value of N, assign N the value of R, and
go to step 2.
4. The greatest common divisor is N.
42
Euclid’s Algorithm
One of the Oldest Known Algorithms
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
Finding the GCD of 18 and 20
1. Assign M the value of the larger of the two values, and N the smaller.
M  20 N  18
43
Example Use
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
Finding the GCD of 18 and 20
1. Assign M the value of the larger of the two values, and N the smaller.
M  20 N  18
2. Divide M by N, call the remainder R.
M/N = 20 / 18 = 1, with R  2
44
Example Use
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
Finding the GCD of 18 and 20 (first time through, second time through)
1. Assign M the value of the larger of the two values, and N the smaller.
M  20 N  18
2. Divide M by N, call the remainder R.
M/N = 20 / 18 = 1, with R  2
M/N = 18 / 2 = 9, with R  0
3. If R is not 0, assign M the value of N, assign N the value of R, and go to step 2.
R = 2. Therefore, M  18, N  2. Go to step 2.
45
Example Use
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
Finding the GCD of 18 and 20 (first time through, second time through)
1. Assign M the value of the larger of the two values, and N the smaller.
M  20 N  18
2. Divide M by N, call the remainder R.
M/N = 20 / 18 = 1, with R  2
M/N = 18 / 2 = 9, with R  0
3. If R is not 0, assign M the value of N, assign N the value of R, and go to step 2.
R = 2. Therefore, M  18, N  2. Go to step 2.
R is 0. Therefore, proceed to step 4.
46
Example Use
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
Finding the GCD of 18 and 20 (first time through, second time through)
1. Assign M the value of the larger of the two values, and N the smaller.
M  20 N  18
2. Divide M by N, call the remainder R.
M/N = 20 / 18 = 1, with R  2
M/N = 18 / 2 = 9, with R  0
3. If R is not 0, assign M the value of N, assign N the value of R, and go to step 2.
R = 2. Therefore, M  18, N  2. Go to step 2.
R is 0. Therefore, proceed to step 4.
4. The greatest common divisor is N.
GCD = N = 2
47
Example Use
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
48
Animation of Euclid’s Algorithm
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
49
Following is an example algorithm for
determining the day of the week for any date
between January 1, 1800 and December 31, 2099
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
50
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
51
Notable Contemporary Algorithms
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
The sequencing of the human genome was dependent on the
development of fast, efficient comparing and matching of DNA
sequences.
52
BLAST Algorithm
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
The RSA algorithm is the basis of public key encryption. It requires the
factorization of large prime numbers to break, which for large enough
primes, is considered impossible. It is the method used for secure web
communication.
53
RSA Algorithm
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
Computer hardware comprises the physical part of a computer system. It
includes the all-important components of the central processing unit
(CPU) and main memory. It also includes peripheral components such as
a keyboard, monitor, mouse, and printer.
60
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
Computer Hardware
Digital Computing: It’s All About Switches
It is essential that computer hardware be reliable and error free. If the
hardware gives incorrect results, then any program run on that hardware
may give incorrect results as well.
The key to developing reliable systems is to keep the design as simple
as possible. In digital computing, all information is represented as a
series of digits.
61
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
Decimal Digitalization
In electronic computing, values are represented by discrete voltage
levels. Suppose that the computer represented numbers as we are used
to, in base ten.
62
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
Each digit is represented by a different voltage level. The more voltage
levels (digits) that the hardware must utilize and distinguish, the more
complex the hardware becomes to design. This results in greater chance
of hardware design errors.
63
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
Information Theory
The Fundamental Theorem of Information
Science of Claude Shannon states that all
information can be represented by the use
of only two symbols, 0 and 1. This is
referred to as binary representation.
Shannon is known as the “Father of
Information Theory.”
64
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
Binary Digitalization
In this representation, each digit can be one of only two possible values,
similar to a light switch that can be either on or off. Computer hardware,
therefore, is based on the use of simple electronic on/off switches called
transistors that can switched at essentially the speed of light.
65
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
The Transistor
The transistor, developed in 1947, revolutionized electronics. Its
invention is what has allowed for all of the dramatic advances in
computing technology that we continue to see today.
66
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
Integrated Circuits
Integrated circuits (chips), the building blocks of computer hardware, are
comprised of millions or even billions of transistors.
67
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
The Binary Number System
For representing numbers, any base (radix) can be used. For example, in
base 10, there are ten possible digits (0, 1, ..., 9), in which each column
value is a power of ten:
10,000,000 1,000,000 100,000 10,000 1,000 100 10 1
107 106 105 104 103 102 101 100
9 9 = 99
68
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
For representing numbers in base 2, there are two possible digits (0, 1)
in which each column value is a power of two:
128 64 32 16 8 4 2 1
27 26 25 24 23 22 21 20
0 1 1 0 0 0 1 1
0 + 64 + 32 + 0 + 0 + 0 + 2 + 1 = 99
69
Although values represented in base 2 are significantly longer than those in
base 10, binary representation is used in digital computing because of the
resulting simplicity of hardware design.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
Bits and Bytes
Each binary digit is referred to as a bit. A group of (usually) eight bits is
called a byte. Converting a base ten number to base two involves the
successive division of the number by 2.
70
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
Fundamental Hardware Components
Central processing unit (CPU) – the “brain” of a computer system.
Interprets and executes instructions.
Main memory – is where currently executing programs reside.
It is volatile, the contents are lost when the power is turned off.
Secondary memory – provides long-term storage of programs and data.
Nonvolatile, the contents are retained when power is turned off. Can be
magnetic (hard drive), optical (CD or DVD), or flash memory (USB drive).
Input/output devices – mouse, keyboard, monitor, printer, etc.
Buses – transfer data between components within a computer system.
System bus (between CPU and main memory).
71
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
72
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
Operating Systems
An operating system is software that manages and interacts
with the hardware resources of a computer.
Because an operating system is intrinsic to the operation of a
computer, it is referred to as system software.
73
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
74
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
Operating systems also provide a particular user interface.
It is the operating system installed on a given computer that
determines the “look and feel” of the user interface and how
the user interacts with the system, and not the particular
model computer.
75
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
Limits of Integrated Circuits
Technology: Moore’s Law
In 1965, Gordon E. Moore, one of the pioneers in the
development of integrated circuits and cofounder of Intel
Corporation, predicted that the number of transistors that
would be able to be put on a silicon chip would double
roughly every two years, allowing the complexity and
therefore the capabilities of integrated circuits to grow
exponentially. This prediction became known as Moore’s
Law.
76
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
Amazingly, to this day that
prediction has held true.
While this doubling of
performance cannot go on
indefinitely, it has not yet
reached its limit.
77
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
78
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
79
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
80
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
81
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
82
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
83
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
84
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
85
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
86
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
What is computer software?
Computer software is a set of program instructions,
including related data and documentation, that can be
executed by computer. This can be in the form of instructions
on paper, or in digital form.
While system software is intrinsic to a computer system,
application software fulfills users’ needs, such as a photo-
editing program.
87
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
Computer Software
The First Computer Programmer
The first computer programs ever
written were for a mechanical
computer designed by Charles
Babbage in the mid-1800s. The
person who wrote these
programs was a woman, Ada
Lovelace, who was a talented
mathematician. Thus, she is
referred to as “the first computer
programmer.”
88
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
Syntax, Semantics and
Program Translation
Programming languages (called “artificial languages”) are
languages just as “natural languages” such as English and
Mandarin (Chinese). Syntax and semantics are important
concepts that apply to all languages.
89
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
The syntax of a language is a set of characters and the acceptable
sequences (arrangements) of those characters.
English, for example, includes the letters of the alphabet,
punctuation, and properly spelled words and properly punctuated
sentences. The following is a syntactically correct sentence in
English,
“Hello there, how are you?”
The following, however, is not syntactically correct,
“Hello there, hao are you?”
The sequence of letters “hao” is not a word in the English language.
90
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
Syntax
The semantics of a language is the meaning associated with each
syntactically correct sequence of characters.
Consider the following sentence:
“Colorless green ideas sleep furiously.”
This sentence is syntactically correct, but has no meaning. Thus, it is
semantically incorrect.
Every language has its own syntax and semantics.
91
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
Semantics
For example, in English “Hao” is syntactically incorrect. In Mandarin
(Chinese), however, “Hao” is a valid word meaning “good.” (“Hao”
is from a system called pinyin, which uses the Roman alphabet
rather than Chinese characters for writing Mandarin.)
92
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
Program Translation
A central processing unit (CPU) is designed to interpret and
execute a specific set of instructions represented in binary
form (i.e., 1s and 0s) called machine code. Only programs in
machine code can be executed by a CPU.
93
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
Writing programs at this “low level” is tedious and error-
prone. Therefore, most programs are written in a “high-level”
programming language such as Python. Since the instructions
of such programs are not in machine code that a CPU can
execute, a translator program must be used.
There are two fundamental types of translators:
• Compiler translates programs into machine code
to be executed by the CPU
• Interpreter executes instructions in place of the CPU
94
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
95
Compiler
Compiled programs generally execute faster than interpreted programs.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
96
Interpreter
An interpreter can immediately execute instructions as they are
entered. This is referred to as interactive mode. This is a very useful
feature for program development. Python, as we shall see, is executed
by an interpreter.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
97
Program Debugging:
Syntax Errors vs. Semantic Errors
Program debugging is the process of finding and correcting errors
(“bugs”) in a computer program. Programming errors are inevitable
during program development.
Syntax errors are caused by invalid syntax (for example, entering prnt
instead of print).
Since a translator cannot understand instructions containing syntax
errors, translators terminate when encountering such errors indicating
where in the program the problem occurred.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
98
In contrast, semantic errors (generally called logic errors ) are errors in
program logic. Such errors cannot be automatically detected, since
translators cannot understand the intent of a given computation.
For example, if a program computed the average of three numbers as
follows,
(num1 + num2 + num3) / 2.0
a translator would have no means of determining that the divisor should
be 3 and not 2. Computers do not understand what a program is meant
to do, they only follow the instructions given. It is up to the
programmer to detect such errors.
Program debugging is not a trivial task, and constitutes much of the time
of program development.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
99
In 1947, engineers working on the Mark II
computer at Harvard University found a
moth stuck in one of the components,
causing the machine to malfunction. They
taped the insect in their logbook and
labeled it “first actual case of bug being
found.” It has become a standard part of
the language of computer programmers.
The log book, complete with the attached
bug, is on display at the Smithsonian
Institution in Washington, D.C.
The First Actual Computer “Bug”
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
100
Programming languages fall into a number of programming paradigms. The
two major programming paradigms in use today are procedural
(imperative) programming and object-oriented programming. Each provides
a different way of thinking about computation. While most programming
languages only support one paradigm, Python supports both procedural and
object-oriented programming. We will start with the procedural aspects of
Python.
Procedural vs. Object-Oriented
Programming
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
101
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
102
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
103
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
104
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
105
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
106
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
107
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
108
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
109
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
Computational problem solving does not simply involve the
act of computer programming. It is a process, with
programming being only one of the steps.
Before a program is written, a design for the program must
be developed. And before a design can be developed, the
problem to be solved must be well understood. Once
written, the program must be thoroughly tested.
110
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
The Process of
Computational Problem Solving
111
Computational Problem Solving Steps
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
112
Problem Analysis
Must understand the fundamental
computational issues involved
• For calendar month problem, can use direct
calculation for determining the day of the week for
a given date
• For MCGW problem, can use brute-force approach
of trying all of the possible rowing actions that may
be taken
• For the Traveling Salesman and Chess playing
problems, a brute-force approach is intractable.
Therefore, other more clever approaches need to
be tried
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
113
Knowing what constitutes a solution.
For some problems, there is only one solution. For others, there may be a
number (or infinite number) of solutions. Thus, a problem may be stated as
finding,
• A solution (calendar month, chess playing)
• An approximate solution
• A best solution (MCGW, Traveling Salesman Problem)
• All solutions
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
114
Describe Data and Algorithms
• For calendar month problem, need to store the month and year,
the number of days in each month, and the names of the days of
the week
• For the MCGW problem, need to store the current state of the
problem (as earlier shown)
• For Traveling Salesman need to store the distance between every
pair of cities
• For the chess playing problem, need to store the configuration of
pieces on a chess board
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
115
Table Representation of Data for the
Traveling Salesman Problem
Note that only half of the table need be stored
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
116
Representation for Chess Playing Program
Although the representation on the left is intuitive, the one on the
right is more appropriate for computational problem solving.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
117
Describing the Algorithms Needed
When solving a computational problem, either suitable existing
algorithms may be found, or new algorithms must be developed.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
118
For the MCGW problem, there are standard search algorithms that
can be used.
For the calendar month problem, a day of the week algorithm
already exists.
For the Traveling Salesman problem, there are various (nontrivial)
algorithms that can be utilized for solving problems with tens of
thousands of cities.
Finally, for the chess playing, since it is infeasible to look ahead at
the final outcomes of every possible move, there are algorithms
that make a best guess at which moves to make. Algorithms that
work well in general but are not guaranteed to give the correct
result for each specific problem are called heuristic algorithms.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
119
Program Implementation
Design decisions provide general details of the data representation
and the algorithmic approaches for solving a problem. The details,
however, do not specify which programming language to use, or
how to implement the program. That is a decision for the
implementation phase.
Since we are programming in Python, the implementation needs to
be expressed in a syntactically correct and appropriate way, using
the instructions and features available in Python.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
120
Program Testing
Writing computer programs is difficult and challenging. As a result,
programming errors are pervasive, persistent and inevitable.
Given this fact, software testing is a crucial part of software
development. Testing is done incrementally as a program is being
developed, when the program is complete, and when the program
needs to be updated.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
121
Truisms of Software Development
1. Programming errors are pervasive, persistent, and inevitable.
2. Software testing is an essential part of software development.
3. Any changes made in correcting a programming error should be
fully understood as to why the changes correct the detected error.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
122
The Python Programming Language was
created by Guido van Rossum. It was first
released in the early 1990s.
Its name comes from a 1970s British
comedy sketch show called Monty
Python’s Flying Circus (The Argument Clinic).
Companies and organizations that use
Python include YouTube, Google, Yahoo
and NASA.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
The Python Programming
Language
Python Features
123
• Simple Syntax
Python programs are clear and easy to read
• Interpreted Language
Python instructions can be executed interactively
• Powerful Programming Features
Can accomplish significant computation with few instructions
• Numerous Python Modules Provide Additional Capabilities
Capabilities that can be incorporated into a Python program
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
The IDLE Python
Development Environment
124
IDLE is an integrated development environment (IDE). An IDE is a bundled
set of software tools for program development. This typically includes,
• an editor
for creating and modifying programs
• a translator
for executing programs, and
• a program debugger
for taking control of the execution of a program
to aid in finding program errors
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
The Python Shell
125
Python can be executed interactively in the Python shell. In this mode,
executing Python is similar to using a calculator.
The >>> symbol is the shell prompt. Here, typing 2 + 3 at prompt outputs
the result 5, again displaying the prompt in wait of another instruction.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
The Python Standard Library
126
The Python Standard Library is a collection of built-in modules, each
providing specific functionality beyond what is included in the “core” part
of Python.
For example, the math module provides additional mathematical functions.
The random module provides the ability to generate random numbers,
useful in programming, as we shall see.
Other Python modules are described in the Python 3 Programmers’
Reference.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
Importing a Library Module
127
In order to utilize the capabilities of modules in a specific program, an
import statement is used as shown.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
128
Because the factorial function is from the math module, the function is
called with the name of the module prepended:
e.g., math.factorial(20)
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
A Bit of Python
129
We introduce a bit of Python, just enough to begin writing some simple
programs.
Since all computer programs,
• input data
• process the data
• output results
we look at the notion of a variable, how to perform some simple arithmetic
calculations, and how to do simple input and output.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
Variables
130
One of the most fundamental concepts in programming is that of a variable.
A variable is “a name that is assigned to a value,” as shown below,
n = 5 variable n is assigned the value 5
Thus, whenever variable n appears in a calculation, it is the current value of
n is that is used, as in the following,
n + 20 (5 + 20)
If variable n is assigned a new value, then the same expression will produce
a different result,
n = 10
n + 20 (10 + 20)
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
Some Basic Arithmetic Operators
131
The common arithmetic operators in Python are,
+ (addition) * (multiplication) ** (exponentiation)
- (subtraction) / (division)
Addition, subtraction, and division use standard mathematical notation,
10 + 20 25 - 15 20 / 10
(Also, // for truncated division, discussed later)
For multiplication and exponentiation, the asterisk (*) is used,
5 * 10 (5 times 10) 2 ** 4 (2 to the 4th power)
Multiplication is never denoted by the use of parentheses,
10 * (20 + 5) CORRECT 10(20 + 5) INCORRECT
Note that parentheses may be used to denote subexpressions.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
Basic Input
132
The programs that we will write request and get information from the user.
In Python, the input function is used for this purpose,
name = input('What is your name?: ')
Characters within quotes are called strings. This particular use of a string, for
requesting input from the user, is called a prompt.
The input function displays the string on the screen to prompt the user for
input,
What is your name?: Charles
The underline is used here to indicate the user’s input.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
Basic Output
133
The print function is used to display information on the screen in Python.
This may be used to display a message (string),
>>> print('Welcome to My First Program!')
Welcome to My First Program!
or used to output the value of a variable,
>>> n = 10
>>> print(n)
10
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
134
Can also display a combination of strings and variables,
>>> name = input('What is your name?: ')
What is your name?: Charles
>>> print('Hello', name)
Hello Charles
Note that a comma is used to separate the individual items being printed,
which causes a space to appear between each when displayed. Thus, the
output of the print function in this case is
Hello Charles
and not
HelloCharles
We will soon learn more about variables, operators, and input/output in
Python.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
Using IDLE
135
In order to become familiar with writing your own Python programs using
IDLE, we will create a simple program that asks the user for their name and
responds with a greeting. This program utilizes the following concepts:
➤ Creating and executing Python programs
➤ Input and print functions
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
Creating a New Python Program
136
To create a Python program file, select New Window from the File menu in
the Python shell,
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
137
A new, untitled window will appear,
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
138
Now can begin entering lines of a program without them being immediately
executed, as in the Python shell.
Note that parts of the program lines are displayed in a certain color. Since
print and input are predefined function names in Python, they are
colored purple. The strings in the program are colored green. The statement
in red is a comment statement. Comment statements are for those reading
the program, and are ignored when the program is executed.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
139
When finished, save the program file by selecting Save As under the File
menu, and save in the appropriate folder with the name MyFirstProgram.py.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
140
To run a Python program, select Run Module from the Run menu (or simply
hit function key F5).
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
Executing a Python Program
141
If you have entered the program code correctly, the program should execute
as shown
If, however, you have mistyped part of the program resulting in a syntax
error (such as mistyping print), you will get an error message.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
142
In such instances, you need to go back to the program window and make the
needed corrections, the re-save and re-execute the program. You may need
to go through this process a number of times until all the syntax errors have
been corrected.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
143
Dr. Frank Drake conducted the first search for
radio signals from extraterrestrial civilizations
in 1960. This established SETI (Search for
Extraterrestrial Intelligence), a new area of
scientific inquiry. In order to estimate the
number of civilizations that may exist in our
galaxy that we may be able to communicate
with, he developed what is now called the
Drake equation.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
A First Program
Calculating the Drake Equation
144
The Drake equation accounts for a number of different factors. Some of the
values are the result of scientific study, while others are only the result of an
“intelligent guess.” The factors consist of,
R, the average rate of star creation per year in our galaxy
p, the percentage of those stars that have planets
n, the average number of planets that can potentially support life for each star with planets
f, the percentage of those planets that actually go on to develop life
I, the percentage of those planets that go on to develop intelligent life
c, the percentage of those that have the technology communicate with us and
L, the expected lifetime of civilizations (the period that they can communicate).
The Drake equation is simply the multiplication of all these factors, giving N,
the estimated number of detectable civilizations there are at any given time,
N = R * p * n * f * i * c * L
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
145
The following table shows those parameters in the Drake equation that have
some consensus as to their correct value, as well as the values that Drake
himself has used.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
Calculating the Drake Equation
The Problem
146
The value of 7 for R, the rate of star creation, is the least disputed value in
the Drake equation today.
Given the uncertainty of the remaining factors, develop a program that
allows a user to enter their own estimated values for the remaining six
factors (p, n, f, i, c, and L) and displays the calculated result.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
Calculating the Drake Equation
Problem Analysis
147
The problem is very straightforward. We only need to understand the
equation provided.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
Calculating the Drake Equation
Program Design
148
The program design for this problem is straightforward. The data to be
represented consist of numerical values, with the Drake equation as the
algorithm.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
149
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
The Overall Steps of the Program
Calculating the Drake Equation
Program Implementation
150
The implementation of this program is fairly simple. The only programming
elements needed are input, assignment, and print, along with the use of
arithmetic operators.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
151
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
152
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
Executable Section of the Program
Calculating the Drake Equation
Python Issues to Point Out
153
• Comment statements are used, starting with a hash (#) sign
• Comments at the start of the program give an overall description
• Comments within code serve as section headers
• Print function is used for program welcome (Iines 26-29)
• Empty print function call causes a skipped line on the screen (line 44)
• Print function also used to display results (lines 45-47)
• Input function is used for getting factors from user for Drake Equation
• The input function always returns a string value
• int(input('……')) used to convert input string to an integer value
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
Calculating the Drake Equation
Example Execution
154
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
Calculating the Drake Equation
Program Testing
155
To test the program, we can calculate the Drake equation for various other
values using a calculator, providing a set of test cases.
A test case is a set of input values and expected output of a given program.
A test plan consists of a number of test cases to verify that a program meets
all requirements.
A good strategy is to include “average,” as well as “extreme” or “special”
cases in a test plan.
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
Test Plan (with results)
156
Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
Ad

Recommended

4CS001_Lecture_1.pptx
4CS001_Lecture_1.pptx
BishalBishal6
 
STRIP: stream learning of influence probabilities.
STRIP: stream learning of influence probabilities.
Albert Bifet
 
Artificial Intelligence for Undergrads
Artificial Intelligence for Undergrads
Jose Berengueres
 
Elements of AI Luxembourg - session 6
Elements of AI Luxembourg - session 6
Jeremie Dauphin
 
Programming quantum computers in Q# (Techorama NL 2018)
Programming quantum computers in Q# (Techorama NL 2018)
Rolf Huisman
 
Ai & ml
Ai & ml
Avilay Parekh
 
Normal Considered Harmful
Normal Considered Harmful
greenwop
 
Large Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
Suman
Suman
Yashodhar Palkuru
 
DEF CON 27 - workshop - EIGENTOURIST - hacking with monads
DEF CON 27 - workshop - EIGENTOURIST - hacking with monads
Felipe Prado
 
ENG322-PPT223fh hug dhhxhhh KB zvhcxhbxdc
ENG322-PPT223fh hug dhhxhhh KB zvhcxhbxdc
SurajLondhe10
 
Introduction-to-Roboticstentang robotika.ppt
Introduction-to-Roboticstentang robotika.ppt
FelciaBellvaMaritza
 
Int999roduct99999ion-to-Roboti9999cs.ppt
Int999roduct99999ion-to-Roboti9999cs.ppt
thevishal007
 
Introduction-to-Robotics and evolution of machines
Introduction-to-Robotics and evolution of machines
intellektrobotics
 
Lecture 1: Introduction
Lecture 1: Introduction
David Evans
 
St eng parallel universes
St eng parallel universes
Leon Pinnock
 
Tour of language landscape (code.talks)
Tour of language landscape (code.talks)
Yan Cui
 
Deep Learning with Python (PyData Seattle 2015)
Deep Learning with Python (PyData Seattle 2015)
Alexander Korbonits
 
Artificial intelligence apporoach to robotics
Artificial intelligence apporoach to robotics
Er. rahul abhishek
 
Unit 1,2,3,4
Unit 1,2,3,4
davidvilla93
 
ENIC Symbols
ENIC Symbols
Tanya Williams
 
Logic and mathematics history and overview for students
Logic and mathematics history and overview for students
Bob Marcus
 
you do sci-tech ah? - the scitech quiz
you do sci-tech ah? - the scitech quiz
Pranav Condur
 
PPT slides - MACHINE PERCEPTION LABORATORY
PPT slides - MACHINE PERCEPTION LABORATORY
butest
 
Robotics- Naved
Robotics- Naved
khwaja naved
 
Ispectrum magazine #15
Ispectrum magazine #15
Ispectrum Magazine
 
St eng parallel universes
St eng parallel universes
Leon Pinnock
 
Mathematics and Engineering.pptx
Mathematics and Engineering.pptx
Dr. Chetan Bhatt
 
Logical Design Architecture in Internet of Things
Logical Design Architecture in Internet of Things
Senthil Vit
 
Wireless sensor networks in Internet of Things
Wireless sensor networks in Internet of Things
Senthil Vit
 

More Related Content

Similar to Python programming Introduction about Python (20)

Suman
Suman
Yashodhar Palkuru
 
DEF CON 27 - workshop - EIGENTOURIST - hacking with monads
DEF CON 27 - workshop - EIGENTOURIST - hacking with monads
Felipe Prado
 
ENG322-PPT223fh hug dhhxhhh KB zvhcxhbxdc
ENG322-PPT223fh hug dhhxhhh KB zvhcxhbxdc
SurajLondhe10
 
Introduction-to-Roboticstentang robotika.ppt
Introduction-to-Roboticstentang robotika.ppt
FelciaBellvaMaritza
 
Int999roduct99999ion-to-Roboti9999cs.ppt
Int999roduct99999ion-to-Roboti9999cs.ppt
thevishal007
 
Introduction-to-Robotics and evolution of machines
Introduction-to-Robotics and evolution of machines
intellektrobotics
 
Lecture 1: Introduction
Lecture 1: Introduction
David Evans
 
St eng parallel universes
St eng parallel universes
Leon Pinnock
 
Tour of language landscape (code.talks)
Tour of language landscape (code.talks)
Yan Cui
 
Deep Learning with Python (PyData Seattle 2015)
Deep Learning with Python (PyData Seattle 2015)
Alexander Korbonits
 
Artificial intelligence apporoach to robotics
Artificial intelligence apporoach to robotics
Er. rahul abhishek
 
Unit 1,2,3,4
Unit 1,2,3,4
davidvilla93
 
ENIC Symbols
ENIC Symbols
Tanya Williams
 
Logic and mathematics history and overview for students
Logic and mathematics history and overview for students
Bob Marcus
 
you do sci-tech ah? - the scitech quiz
you do sci-tech ah? - the scitech quiz
Pranav Condur
 
PPT slides - MACHINE PERCEPTION LABORATORY
PPT slides - MACHINE PERCEPTION LABORATORY
butest
 
Robotics- Naved
Robotics- Naved
khwaja naved
 
Ispectrum magazine #15
Ispectrum magazine #15
Ispectrum Magazine
 
St eng parallel universes
St eng parallel universes
Leon Pinnock
 
Mathematics and Engineering.pptx
Mathematics and Engineering.pptx
Dr. Chetan Bhatt
 
DEF CON 27 - workshop - EIGENTOURIST - hacking with monads
DEF CON 27 - workshop - EIGENTOURIST - hacking with monads
Felipe Prado
 
ENG322-PPT223fh hug dhhxhhh KB zvhcxhbxdc
ENG322-PPT223fh hug dhhxhhh KB zvhcxhbxdc
SurajLondhe10
 
Introduction-to-Roboticstentang robotika.ppt
Introduction-to-Roboticstentang robotika.ppt
FelciaBellvaMaritza
 
Int999roduct99999ion-to-Roboti9999cs.ppt
Int999roduct99999ion-to-Roboti9999cs.ppt
thevishal007
 
Introduction-to-Robotics and evolution of machines
Introduction-to-Robotics and evolution of machines
intellektrobotics
 
Lecture 1: Introduction
Lecture 1: Introduction
David Evans
 
St eng parallel universes
St eng parallel universes
Leon Pinnock
 
Tour of language landscape (code.talks)
Tour of language landscape (code.talks)
Yan Cui
 
Deep Learning with Python (PyData Seattle 2015)
Deep Learning with Python (PyData Seattle 2015)
Alexander Korbonits
 
Artificial intelligence apporoach to robotics
Artificial intelligence apporoach to robotics
Er. rahul abhishek
 
Logic and mathematics history and overview for students
Logic and mathematics history and overview for students
Bob Marcus
 
you do sci-tech ah? - the scitech quiz
you do sci-tech ah? - the scitech quiz
Pranav Condur
 
PPT slides - MACHINE PERCEPTION LABORATORY
PPT slides - MACHINE PERCEPTION LABORATORY
butest
 
St eng parallel universes
St eng parallel universes
Leon Pinnock
 
Mathematics and Engineering.pptx
Mathematics and Engineering.pptx
Dr. Chetan Bhatt
 

More from Senthil Vit (20)

Logical Design Architecture in Internet of Things
Logical Design Architecture in Internet of Things
Senthil Vit
 
Wireless sensor networks in Internet of Things
Wireless sensor networks in Internet of Things
Senthil Vit
 
Classification Algorithm in Machine Learning
Classification Algorithm in Machine Learning
Senthil Vit
 
Decision Trees Learning in Machine Learning
Decision Trees Learning in Machine Learning
Senthil Vit
 
Operating system Virtualization_NEW.pptx
Operating system Virtualization_NEW.pptx
Senthil Vit
 
Synchronization Peterson’s Solution.pptx
Synchronization Peterson’s Solution.pptx
Senthil Vit
 
Control structures in Python programming
Control structures in Python programming
Senthil Vit
 
Data and Expressions in Python programming
Data and Expressions in Python programming
Senthil Vit
 
Switching Problems.pdf
Switching Problems.pdf
Senthil Vit
 
Big Oh.ppt
Big Oh.ppt
Senthil Vit
 
AsymptoticNotations.ppt
AsymptoticNotations.ppt
Senthil Vit
 
snort.ppt
snort.ppt
Senthil Vit
 
First Best and Worst Fit.pptx
First Best and Worst Fit.pptx
Senthil Vit
 
File Implementation Problem.pptx
File Implementation Problem.pptx
Senthil Vit
 
Design Issues of an OS.ppt
Design Issues of an OS.ppt
Senthil Vit
 
Operating Systems – Structuring Methods.pptx
Operating Systems – Structuring Methods.pptx
Senthil Vit
 
deadlock.ppt
deadlock.ppt
Senthil Vit
 
Virtualization.pptx
Virtualization.pptx
Senthil Vit
 
Traffic-Monitoring.ppt
Traffic-Monitoring.ppt
Senthil Vit
 
Lect_2.pptx
Lect_2.pptx
Senthil Vit
 
Logical Design Architecture in Internet of Things
Logical Design Architecture in Internet of Things
Senthil Vit
 
Wireless sensor networks in Internet of Things
Wireless sensor networks in Internet of Things
Senthil Vit
 
Classification Algorithm in Machine Learning
Classification Algorithm in Machine Learning
Senthil Vit
 
Decision Trees Learning in Machine Learning
Decision Trees Learning in Machine Learning
Senthil Vit
 
Operating system Virtualization_NEW.pptx
Operating system Virtualization_NEW.pptx
Senthil Vit
 
Synchronization Peterson’s Solution.pptx
Synchronization Peterson’s Solution.pptx
Senthil Vit
 
Control structures in Python programming
Control structures in Python programming
Senthil Vit
 
Data and Expressions in Python programming
Data and Expressions in Python programming
Senthil Vit
 
Switching Problems.pdf
Switching Problems.pdf
Senthil Vit
 
AsymptoticNotations.ppt
AsymptoticNotations.ppt
Senthil Vit
 
First Best and Worst Fit.pptx
First Best and Worst Fit.pptx
Senthil Vit
 
File Implementation Problem.pptx
File Implementation Problem.pptx
Senthil Vit
 
Design Issues of an OS.ppt
Design Issues of an OS.ppt
Senthil Vit
 
Operating Systems – Structuring Methods.pptx
Operating Systems – Structuring Methods.pptx
Senthil Vit
 
Virtualization.pptx
Virtualization.pptx
Senthil Vit
 
Traffic-Monitoring.ppt
Traffic-Monitoring.ppt
Senthil Vit
 
Ad

Recently uploaded (20)

The basics of hydrogenation of co2 reaction
The basics of hydrogenation of co2 reaction
kumarrahul230759
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
ElysiumPro Company Profile 2025-2026.pdf
ElysiumPro Company Profile 2025-2026.pdf
info751436
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
Impurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptx
dhanashree78
 
Cadastral Maps
Cadastral Maps
Google
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Great power lithium iron phosphate cells
Great power lithium iron phosphate cells
salmankhan835951
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
The basics of hydrogenation of co2 reaction
The basics of hydrogenation of co2 reaction
kumarrahul230759
 
How Binning Affects LED Performance & Consistency.pdf
How Binning Affects LED Performance & Consistency.pdf
Mina Anis
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
ElysiumPro Company Profile 2025-2026.pdf
ElysiumPro Company Profile 2025-2026.pdf
info751436
 
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Montreal Dreamin' 25 - Introduction to the MuleSoft AI Chain (MAC) Project
Alexandra N. Martinez
 
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
Deep Learning for Natural Language Processing_FDP on 16 June 2025 MITS.pptx
resming1
 
3. What is the principles of Teamwork_Module_V1.0.ppt
3. What is the principles of Teamwork_Module_V1.0.ppt
engaash9
 
Impurities of Water and their Significance.pptx
Impurities of Water and their Significance.pptx
dhanashree78
 
Cadastral Maps
Cadastral Maps
Google
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-Adaptaflex.pdf
djiceramil
 
Great power lithium iron phosphate cells
Great power lithium iron phosphate cells
salmankhan835951
 
machine learning is a advance technology
machine learning is a advance technology
ynancy893
 
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
Development of Portable Biomass Briquetting Machine (S, A & D)-1.pptx
aniket862935
 
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
Center Enamel can Provide Aluminum Dome Roofs for diesel tank.docx
CenterEnamel
 
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
362 Alec Data Center Solutions-Slysium Data Center-AUH-ABB Furse.pdf
djiceramil
 
60 Years and Beyond eBook 1234567891.pdf
60 Years and Beyond eBook 1234567891.pdf
waseemalazzeh
 
grade 9 science q1 quiz.pptx science quiz
grade 9 science q1 quiz.pptx science quiz
norfapangolima
 
Microwatt: Open Tiny Core, Big Possibilities
Microwatt: Open Tiny Core, Big Possibilities
IBM
 
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
Introduction to Natural Language Processing - Stages in NLP Pipeline, Challen...
resming1
 
NALCO Green Anode Plant,Compositions of CPC,Pitch
NALCO Green Anode Plant,Compositions of CPC,Pitch
arpitprachi123
 
Ad

Python programming Introduction about Python

  • 1. Chapter 1: Introduction This chapter addresses the question “What is computer science?” We begin by introducing the essence of computational problem solving via some classic examples. Next, computer algorithms, the heart of computational problem solving, are discussed. This is followed by a look at computer hardware (and the related issues of binary representation and operating systems) and computer software (and the related issues of syntax, semantics, and program translation). The chapter finishes by presenting the process of computational problem solving, with an introduction to the Python programming language. 1 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons
  • 2. Computing technology has changed—and is continuing to change—the world. Essentially every aspect of life has been impacted by computing. Computing related fields in almost all areas of study are emerging. 2 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Motivation
  • 3. Computer science is fundamentally about computational problem solving. Programming and computers are only tools in the field of computing. The field has tremendous breadth and diversity. Areas of study include: 3 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science? • Software Engineering • Database Management / Data Mining • Computer Networks • Computer Graphics • Computer Simulation • Information Security • Programming Language Design • Systems Programming • Computer Architecture • Human–Computer Interaction • Robotics • Artificial Intelligence What is Computer Science?
  • 4. Computational Problem Solving Two things that are needed to perform computational problem solving: • a representation that captures all the relevant aspects of the problem • an algorithm that solves the problem by use of the representation 4 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science? Thus, computational problem solving finds a solution within a representation that translates into a solution for what is being represented.
  • 5. The Use of Abstraction in Computational Problem Solving A representation that leaves out detail of what is being represented is a form of abstraction. Abstraction is prevalent in the everyday world. For example, maps are abstract representations. 5 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 6. 6 Below is the original 1908 map of the London Underground (Subway). Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 7. 7 Below is a more abstract, but topologically correct, map of the London Underground subway system. It shows the bends and curves of each track. This map contains too much information for its purpose—that is, to find out where each subway line leads, and where the connections are between lines. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 8. 8 Below is a more abstract representation of the subway system, developed by Harry Beck in 1931. The track lines are straightened out where the track curves are irrelevant for subway riders. This is a simpler, easier to read, and thus a better representation for its purpose. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 9. 9 This particular abstraction is still in use today. Washington D.C. Metro Map Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 10. 10 An interesting form of abstraction is the creative work of Chris Jordan, which allows the viewer to control the degree of abstraction of the work. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 11. 11 Cans Seurat, 2007 60x92“ Copyright Chris Jordan Depicts 106,000 aluminum cans, the number used in the U.S. every thirty seconds. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 12. 12 Clicking on the image causes the picture to zoom in, showing more detail. (Copyright Chris Jordan) Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 13. 13 Zooming in closer … (Copyright Chris Jordan) Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 14. 14 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science? (Copyright Chris Jordan)
  • 15. 15 www.chrisjordan.com/current_set2.php More Images Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 16. Abstraction in Computing Abstraction is intrinsic to computing and computational problem solving. • The concept of “1s” and “0s” in digital computing is an abstraction digital information is actually represented as high or low voltage levels, magnetic particles oriented one of two ways, pits on an optical disk, etc. • Programming languages are an abstraction the instructions and data of a computer program is an abstract representation of the underlying machine instructions and storage • Programming design involves the use of abstraction programs are conceptualized as various modules that work together 16 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 17. Man, Cabbage, Goat, Wolf Problem 17 A man lives on the east side of a river. He wishes to bring a cabbage, a goat, and a wolf to a village on the west side of the river to sell. However, his boat is only big enough to hold himself, and either the cabbage, goat, or wolf. In addition, the man cannot leave the goat alone with the cabbage because the goat will eat the cabbage, and he cannot leave the wolf alone with the goat because the wolf will eat the goat. How does the man solve his problem? Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 18. 18 There is a simple algorithmic approach for solving this problem by simply trying all possible combinations of items that may be rowed back and forth across the river. Trying all possible solutions is referred to as a brute force approach. What would be an appropriate representation for this problem? Whatever representation we use, only the aspects of the problem that are relevant for its solution need to be represented. • Color of the boat? • Name of the man? • Width of the river? The only information relevant for this problem is where each particular item is at each step in the problem solving. Therefore, by the use of abstraction, we define a representation that captures only this needed information. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 19. 19 For example, we could use a sequence to indicate where each of the objects currently are, man cabbage goat wolf boat village [ e a s t , w e s t , e a s t , w e s t , e a s t , w e s t ] where it is understood that the first item in the sequence is the location of the man, the second the location of the cabbage, etc. Note that the village is always on the west side of the river—it doesn’t move! Its location is fixed and therefore does not need to be represented. Also, the boat is always in the same place as the man. So representing the location of both the man and the boat is redundant information. The relevant, minimal representation is given below, man cabbage goat wolf [ E , W , E , E ] Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 20. 20 The actual problem is to determine how the man can row objects across the river, with certain constraints on which pairs of objects cannot be left alone. The computational problem is to find a way to convert the representation of the start state of the problem, when all the object are on the east side of the river, man cabbage goat wolf [ E , E , E , E ] to the goal state with all objects on the west side of the river, man cabbage goat wolf [ W , W , W , W with the constraint that certain invalid states should never be used. Thus, in a computational problem solving approach, a problem is solved within the representation used, in which the solution within the representation must translate into a solution of the actual problem. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 21. 21 For example, from the start state, there are three possible moves that can be made, only one of which results in a valid state. man cabbage goat wolf [ E , E , E , E ] START STATE [ W , W , E , E ] [ W , E , W , E ] [ W , E , E , W ] Man rows cabbage across Man rows goat across Man rows wolf across INVALID STATE Goat left alone with wolf VALID STATE Cabbage left alone with wolf INVALID STATE Cabbage left alone with goat  Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 22. 22 We check if the new problem state is the goal state. If true, then we solved the problem in one step! (We know that cannot be so, but the algorithmic approach that we are using does not.) man cabbage goat wolf [ E , E , E , E ] START STATE [ W , E , W , E ] Man rows goat across  Is goal state [ W , W , W , W ] ? No Therefore we continue searching from the current state. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 23. 23 Since the man can only row across objects on the same side of the river, there are only two possible moves from here, man cabbage goat wolf INTERMEDIATE [ W , E , W , E ] STATE [ E , W , E , E ] [ E , E , E , E ] Man rows back alone Man rows goat across VALID STATE Cabbage left alone with wolf  VALID STATE BUT, previously in this state. It is the start state. No progress made!  Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 24. 24 This would continue until the goal state is reached, man cabbage goat wolf [ E , W , E , E ] [ W , W , W , W ] GOAL STATE Thus, the computational problem of generating the goal state from the start state translates into a solution of the actual problem since each transition between states has a corresponding action in the actual problem—of the man rowing across the river with (or without) a particular object. . . Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 25. The Importance of Algorithms 25 As another example computational problem, suppose that you needed to write a program that displays a calendar month for any given month and year. The representation of this problem is rather straightforward. Only a few values are needed: • the month and year • number of days in each month • names of the days of the week • day of the week that the first day of the month falls on Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 26. 26 The month and year, number of days in a month, names of the days of the week can be easily handled. The less obvious part is how to determine the day of the week that a particular date falls on. How would you do that? Start with a known day of the week for a given year in the past and calculate forward from there? That would not be a very efficient way of solving the problem. Since calendars are based on cycles, there must be a more direct method for doing this. Thus, no matter how good a programmer you may be, without knowledge of the needed algorithm, you could not write a program that solves the problem. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 27. The Limits of Computational Problem Solving 27 Once an algorithm for a given problem is developed or found, an important question is “Can a solution to the problem be found in a reasonable amount of time?” “But aren’t computers very fast, and getting faster all the time?” Yes, but some problems require an amount of time to compute a solution that is astronomical compared to the capabilities of current computing devices. A classic problem in computer science that demonstrates this is the Traveling Salesman problem. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 28. The Traveling Salesman Problem 28 A salesman needs to visit a set of cities. He wants to find the shortest route of travel, starting and ending at any city for a given set of cities, what route should he take? The algorithm for solving this problem is a simple one. Determine the lengths of all possible routes that can be taken, and find the shortest one. That is, by using a brute force approach. The computational issue, therefore, is for a given set of cities, how many possible routes are there? Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 29. 29 If we consider a route to be a specific sequence of names of cities, then how many permutations of that list are there? New York, Boston, Chicago, San Francisco, Los Angeles, Atlanta New York, Boston, Chicago, San Francisco, Atlanta, Loa Angeles New York, Boston, Chicago, Los Angeles, San Francisco, Atlanta etc. Mathematically, the number of permutations for n entities is n! (n factorial). How big a number is that for various number of cities? Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 30. 30 Below are the number of permutations (and this number of routes) there are for varies numbers of cities: Ten Cities 10! 3,628, 800 (over three million) Twenty Cities 20! 2,432,902,008,176,640,000 Fifty Cities 50! Over 1064 If we assume that a computer could compute the routes of one million cities per second: • for twenty cities, it would take 77,000 years • for fifty cities, it would take longer than the age of the universe! Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 31. The Game of Chess 31 When a computer plays chess against a human opponent, both have to “think ahead” to the possible outcomes of each move it may make. A brute force approach can also be used for a computer playing a game of chess. A program can consider all the possible moves that can be made, each ending in a win, loss, or draw. It can then select the move that leads to the most number of ways of winning. (Chess masters, on the other hand, only think ahead a few moves, and “instinctively” know the value of each outcome.) Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 32. 32 There are approximately 10120 possible chess games that can be played. This is related to the average number of look-ahead steps needed for deciding each move. There are approximately, 1080 atoms in the observable universe and an estimated 3 × 1090 grains of sand to fill the universe solid Thus, there are more possible chess games that can be played than grains of sand to fill the universe solid! Therefore, for problems such as this and the Traveling Salesman problem in which a brute-force approach is impractical to use, clever and more efficient problem-solving methods must be discovered that find either an exact or an approximate solution to the problem. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 33. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 34. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 35. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science? 0
  • 36. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 37. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 38. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 39. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?
  • 40. An algorithm is a finite number of clearly described, unambiguous “doable” steps that can be systematically followed to produce a desired result for given input in a finite amount of time (that is, it eventually terminates). The word “algorithm” is derived from the ninth-century Arab mathematician, Al- Khwarizmi who worked on “written processes to achieve some goal.” 40 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms Computer Algorithms
  • 41. Computer algorithms are central to computer science. They provide step-by-step methods of computation that a machine can carry out. Having high-speed machines (computers) that can consistently follow a given set of instructions provides a reliable and effective means of realizing computation. However, the computation that a given computer performs is only as good as the underlying algorithm used. Because computers can execute a large number of instructions very quickly and reliably without error, algorithms and computers are a perfect match! 41 Algorithms and Computers: A Perfect Match Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 42. An algorithm for computing the greatest common denominator (GCD) of two given integers. It is one of the oldest numerical algorithms still in common use. 1. Assign M the value of the larger of the two values. 2. Divide M by N, call the remainder R. 3. If R is not 0, then assign M the value of N, assign N the value of R, and go to step 2. 4. The greatest common divisor is N. 42 Euclid’s Algorithm One of the Oldest Known Algorithms Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 43. Finding the GCD of 18 and 20 1. Assign M the value of the larger of the two values, and N the smaller. M  20 N  18 43 Example Use Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 44. Finding the GCD of 18 and 20 1. Assign M the value of the larger of the two values, and N the smaller. M  20 N  18 2. Divide M by N, call the remainder R. M/N = 20 / 18 = 1, with R  2 44 Example Use Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 45. Finding the GCD of 18 and 20 (first time through, second time through) 1. Assign M the value of the larger of the two values, and N the smaller. M  20 N  18 2. Divide M by N, call the remainder R. M/N = 20 / 18 = 1, with R  2 M/N = 18 / 2 = 9, with R  0 3. If R is not 0, assign M the value of N, assign N the value of R, and go to step 2. R = 2. Therefore, M  18, N  2. Go to step 2. 45 Example Use Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 46. Finding the GCD of 18 and 20 (first time through, second time through) 1. Assign M the value of the larger of the two values, and N the smaller. M  20 N  18 2. Divide M by N, call the remainder R. M/N = 20 / 18 = 1, with R  2 M/N = 18 / 2 = 9, with R  0 3. If R is not 0, assign M the value of N, assign N the value of R, and go to step 2. R = 2. Therefore, M  18, N  2. Go to step 2. R is 0. Therefore, proceed to step 4. 46 Example Use Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 47. Finding the GCD of 18 and 20 (first time through, second time through) 1. Assign M the value of the larger of the two values, and N the smaller. M  20 N  18 2. Divide M by N, call the remainder R. M/N = 20 / 18 = 1, with R  2 M/N = 18 / 2 = 9, with R  0 3. If R is not 0, assign M the value of N, assign N the value of R, and go to step 2. R = 2. Therefore, M  18, N  2. Go to step 2. R is 0. Therefore, proceed to step 4. 4. The greatest common divisor is N. GCD = N = 2 47 Example Use Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 48. 48 Animation of Euclid’s Algorithm Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 49. 49 Following is an example algorithm for determining the day of the week for any date between January 1, 1800 and December 31, 2099 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 50. 50 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 51. 51 Notable Contemporary Algorithms Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 52. The sequencing of the human genome was dependent on the development of fast, efficient comparing and matching of DNA sequences. 52 BLAST Algorithm Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 53. The RSA algorithm is the basis of public key encryption. It requires the factorization of large prime numbers to break, which for large enough primes, is considered impossible. It is the method used for secure web communication. 53 RSA Algorithm Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 54. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 55. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 56. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 57. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 58. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 59. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms
  • 60. Computer hardware comprises the physical part of a computer system. It includes the all-important components of the central processing unit (CPU) and main memory. It also includes peripheral components such as a keyboard, monitor, mouse, and printer. 60 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware Computer Hardware
  • 61. Digital Computing: It’s All About Switches It is essential that computer hardware be reliable and error free. If the hardware gives incorrect results, then any program run on that hardware may give incorrect results as well. The key to developing reliable systems is to keep the design as simple as possible. In digital computing, all information is represented as a series of digits. 61 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 62. Decimal Digitalization In electronic computing, values are represented by discrete voltage levels. Suppose that the computer represented numbers as we are used to, in base ten. 62 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 63. Each digit is represented by a different voltage level. The more voltage levels (digits) that the hardware must utilize and distinguish, the more complex the hardware becomes to design. This results in greater chance of hardware design errors. 63 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 64. Information Theory The Fundamental Theorem of Information Science of Claude Shannon states that all information can be represented by the use of only two symbols, 0 and 1. This is referred to as binary representation. Shannon is known as the “Father of Information Theory.” 64 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 65. Binary Digitalization In this representation, each digit can be one of only two possible values, similar to a light switch that can be either on or off. Computer hardware, therefore, is based on the use of simple electronic on/off switches called transistors that can switched at essentially the speed of light. 65 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 66. The Transistor The transistor, developed in 1947, revolutionized electronics. Its invention is what has allowed for all of the dramatic advances in computing technology that we continue to see today. 66 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 67. Integrated Circuits Integrated circuits (chips), the building blocks of computer hardware, are comprised of millions or even billions of transistors. 67 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 68. The Binary Number System For representing numbers, any base (radix) can be used. For example, in base 10, there are ten possible digits (0, 1, ..., 9), in which each column value is a power of ten: 10,000,000 1,000,000 100,000 10,000 1,000 100 10 1 107 106 105 104 103 102 101 100 9 9 = 99 68 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 69. For representing numbers in base 2, there are two possible digits (0, 1) in which each column value is a power of two: 128 64 32 16 8 4 2 1 27 26 25 24 23 22 21 20 0 1 1 0 0 0 1 1 0 + 64 + 32 + 0 + 0 + 0 + 2 + 1 = 99 69 Although values represented in base 2 are significantly longer than those in base 10, binary representation is used in digital computing because of the resulting simplicity of hardware design. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 70. Bits and Bytes Each binary digit is referred to as a bit. A group of (usually) eight bits is called a byte. Converting a base ten number to base two involves the successive division of the number by 2. 70 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 71. Fundamental Hardware Components Central processing unit (CPU) – the “brain” of a computer system. Interprets and executes instructions. Main memory – is where currently executing programs reside. It is volatile, the contents are lost when the power is turned off. Secondary memory – provides long-term storage of programs and data. Nonvolatile, the contents are retained when power is turned off. Can be magnetic (hard drive), optical (CD or DVD), or flash memory (USB drive). Input/output devices – mouse, keyboard, monitor, printer, etc. Buses – transfer data between components within a computer system. System bus (between CPU and main memory). 71 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 72. 72 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 73. Operating Systems An operating system is software that manages and interacts with the hardware resources of a computer. Because an operating system is intrinsic to the operation of a computer, it is referred to as system software. 73 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 74. 74 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 75. Operating systems also provide a particular user interface. It is the operating system installed on a given computer that determines the “look and feel” of the user interface and how the user interacts with the system, and not the particular model computer. 75 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 76. Limits of Integrated Circuits Technology: Moore’s Law In 1965, Gordon E. Moore, one of the pioneers in the development of integrated circuits and cofounder of Intel Corporation, predicted that the number of transistors that would be able to be put on a silicon chip would double roughly every two years, allowing the complexity and therefore the capabilities of integrated circuits to grow exponentially. This prediction became known as Moore’s Law. 76 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 77. Amazingly, to this day that prediction has held true. While this doubling of performance cannot go on indefinitely, it has not yet reached its limit. 77 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 78. 78 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 79. 79 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 80. 80 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 81. 81 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 82. 82 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 83. 83 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 84. 84 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 85. 85 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 86. 86 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware
  • 87. What is computer software? Computer software is a set of program instructions, including related data and documentation, that can be executed by computer. This can be in the form of instructions on paper, or in digital form. While system software is intrinsic to a computer system, application software fulfills users’ needs, such as a photo- editing program. 87 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software Computer Software
  • 88. The First Computer Programmer The first computer programs ever written were for a mechanical computer designed by Charles Babbage in the mid-1800s. The person who wrote these programs was a woman, Ada Lovelace, who was a talented mathematician. Thus, she is referred to as “the first computer programmer.” 88 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 89. Syntax, Semantics and Program Translation Programming languages (called “artificial languages”) are languages just as “natural languages” such as English and Mandarin (Chinese). Syntax and semantics are important concepts that apply to all languages. 89 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 90. The syntax of a language is a set of characters and the acceptable sequences (arrangements) of those characters. English, for example, includes the letters of the alphabet, punctuation, and properly spelled words and properly punctuated sentences. The following is a syntactically correct sentence in English, “Hello there, how are you?” The following, however, is not syntactically correct, “Hello there, hao are you?” The sequence of letters “hao” is not a word in the English language. 90 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software Syntax
  • 91. The semantics of a language is the meaning associated with each syntactically correct sequence of characters. Consider the following sentence: “Colorless green ideas sleep furiously.” This sentence is syntactically correct, but has no meaning. Thus, it is semantically incorrect. Every language has its own syntax and semantics. 91 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software Semantics
  • 92. For example, in English “Hao” is syntactically incorrect. In Mandarin (Chinese), however, “Hao” is a valid word meaning “good.” (“Hao” is from a system called pinyin, which uses the Roman alphabet rather than Chinese characters for writing Mandarin.) 92 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 93. Program Translation A central processing unit (CPU) is designed to interpret and execute a specific set of instructions represented in binary form (i.e., 1s and 0s) called machine code. Only programs in machine code can be executed by a CPU. 93 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 94. Writing programs at this “low level” is tedious and error- prone. Therefore, most programs are written in a “high-level” programming language such as Python. Since the instructions of such programs are not in machine code that a CPU can execute, a translator program must be used. There are two fundamental types of translators: • Compiler translates programs into machine code to be executed by the CPU • Interpreter executes instructions in place of the CPU 94 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 95. 95 Compiler Compiled programs generally execute faster than interpreted programs. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 96. 96 Interpreter An interpreter can immediately execute instructions as they are entered. This is referred to as interactive mode. This is a very useful feature for program development. Python, as we shall see, is executed by an interpreter. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 97. 97 Program Debugging: Syntax Errors vs. Semantic Errors Program debugging is the process of finding and correcting errors (“bugs”) in a computer program. Programming errors are inevitable during program development. Syntax errors are caused by invalid syntax (for example, entering prnt instead of print). Since a translator cannot understand instructions containing syntax errors, translators terminate when encountering such errors indicating where in the program the problem occurred. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 98. 98 In contrast, semantic errors (generally called logic errors ) are errors in program logic. Such errors cannot be automatically detected, since translators cannot understand the intent of a given computation. For example, if a program computed the average of three numbers as follows, (num1 + num2 + num3) / 2.0 a translator would have no means of determining that the divisor should be 3 and not 2. Computers do not understand what a program is meant to do, they only follow the instructions given. It is up to the programmer to detect such errors. Program debugging is not a trivial task, and constitutes much of the time of program development. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 99. 99 In 1947, engineers working on the Mark II computer at Harvard University found a moth stuck in one of the components, causing the machine to malfunction. They taped the insect in their logbook and labeled it “first actual case of bug being found.” It has become a standard part of the language of computer programmers. The log book, complete with the attached bug, is on display at the Smithsonian Institution in Washington, D.C. The First Actual Computer “Bug” Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 100. 100 Programming languages fall into a number of programming paradigms. The two major programming paradigms in use today are procedural (imperative) programming and object-oriented programming. Each provides a different way of thinking about computation. While most programming languages only support one paradigm, Python supports both procedural and object-oriented programming. We will start with the procedural aspects of Python. Procedural vs. Object-Oriented Programming Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 101. 101 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 102. 102 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 103. 103 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 104. 104 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 105. 105 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 106. 106 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 107. 107 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 108. 108 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 109. 109 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software
  • 110. Computational problem solving does not simply involve the act of computer programming. It is a process, with programming being only one of the steps. Before a program is written, a design for the program must be developed. And before a design can be developed, the problem to be solved must be well understood. Once written, the program must be thoroughly tested. 110 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving The Process of Computational Problem Solving
  • 111. 111 Computational Problem Solving Steps Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
  • 112. 112 Problem Analysis Must understand the fundamental computational issues involved • For calendar month problem, can use direct calculation for determining the day of the week for a given date • For MCGW problem, can use brute-force approach of trying all of the possible rowing actions that may be taken • For the Traveling Salesman and Chess playing problems, a brute-force approach is intractable. Therefore, other more clever approaches need to be tried Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
  • 113. 113 Knowing what constitutes a solution. For some problems, there is only one solution. For others, there may be a number (or infinite number) of solutions. Thus, a problem may be stated as finding, • A solution (calendar month, chess playing) • An approximate solution • A best solution (MCGW, Traveling Salesman Problem) • All solutions Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
  • 114. 114 Describe Data and Algorithms • For calendar month problem, need to store the month and year, the number of days in each month, and the names of the days of the week • For the MCGW problem, need to store the current state of the problem (as earlier shown) • For Traveling Salesman need to store the distance between every pair of cities • For the chess playing problem, need to store the configuration of pieces on a chess board Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
  • 115. 115 Table Representation of Data for the Traveling Salesman Problem Note that only half of the table need be stored Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
  • 116. 116 Representation for Chess Playing Program Although the representation on the left is intuitive, the one on the right is more appropriate for computational problem solving. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
  • 117. 117 Describing the Algorithms Needed When solving a computational problem, either suitable existing algorithms may be found, or new algorithms must be developed. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
  • 118. 118 For the MCGW problem, there are standard search algorithms that can be used. For the calendar month problem, a day of the week algorithm already exists. For the Traveling Salesman problem, there are various (nontrivial) algorithms that can be utilized for solving problems with tens of thousands of cities. Finally, for the chess playing, since it is infeasible to look ahead at the final outcomes of every possible move, there are algorithms that make a best guess at which moves to make. Algorithms that work well in general but are not guaranteed to give the correct result for each specific problem are called heuristic algorithms. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
  • 119. 119 Program Implementation Design decisions provide general details of the data representation and the algorithmic approaches for solving a problem. The details, however, do not specify which programming language to use, or how to implement the program. That is a decision for the implementation phase. Since we are programming in Python, the implementation needs to be expressed in a syntactically correct and appropriate way, using the instructions and features available in Python. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
  • 120. 120 Program Testing Writing computer programs is difficult and challenging. As a result, programming errors are pervasive, persistent and inevitable. Given this fact, software testing is a crucial part of software development. Testing is done incrementally as a program is being developed, when the program is complete, and when the program needs to be updated. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
  • 121. 121 Truisms of Software Development 1. Programming errors are pervasive, persistent, and inevitable. 2. Software testing is an essential part of software development. 3. Any changes made in correcting a programming error should be fully understood as to why the changes correct the detected error. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving
  • 122. 122 The Python Programming Language was created by Guido van Rossum. It was first released in the early 1990s. Its name comes from a 1970s British comedy sketch show called Monty Python’s Flying Circus (The Argument Clinic). Companies and organizations that use Python include YouTube, Google, Yahoo and NASA. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language The Python Programming Language
  • 123. Python Features 123 • Simple Syntax Python programs are clear and easy to read • Interpreted Language Python instructions can be executed interactively • Powerful Programming Features Can accomplish significant computation with few instructions • Numerous Python Modules Provide Additional Capabilities Capabilities that can be incorporated into a Python program Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 124. The IDLE Python Development Environment 124 IDLE is an integrated development environment (IDE). An IDE is a bundled set of software tools for program development. This typically includes, • an editor for creating and modifying programs • a translator for executing programs, and • a program debugger for taking control of the execution of a program to aid in finding program errors Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 125. The Python Shell 125 Python can be executed interactively in the Python shell. In this mode, executing Python is similar to using a calculator. The >>> symbol is the shell prompt. Here, typing 2 + 3 at prompt outputs the result 5, again displaying the prompt in wait of another instruction. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 126. The Python Standard Library 126 The Python Standard Library is a collection of built-in modules, each providing specific functionality beyond what is included in the “core” part of Python. For example, the math module provides additional mathematical functions. The random module provides the ability to generate random numbers, useful in programming, as we shall see. Other Python modules are described in the Python 3 Programmers’ Reference. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 127. Importing a Library Module 127 In order to utilize the capabilities of modules in a specific program, an import statement is used as shown. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 128. 128 Because the factorial function is from the math module, the function is called with the name of the module prepended: e.g., math.factorial(20) Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 129. A Bit of Python 129 We introduce a bit of Python, just enough to begin writing some simple programs. Since all computer programs, • input data • process the data • output results we look at the notion of a variable, how to perform some simple arithmetic calculations, and how to do simple input and output. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 130. Variables 130 One of the most fundamental concepts in programming is that of a variable. A variable is “a name that is assigned to a value,” as shown below, n = 5 variable n is assigned the value 5 Thus, whenever variable n appears in a calculation, it is the current value of n is that is used, as in the following, n + 20 (5 + 20) If variable n is assigned a new value, then the same expression will produce a different result, n = 10 n + 20 (10 + 20) Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 131. Some Basic Arithmetic Operators 131 The common arithmetic operators in Python are, + (addition) * (multiplication) ** (exponentiation) - (subtraction) / (division) Addition, subtraction, and division use standard mathematical notation, 10 + 20 25 - 15 20 / 10 (Also, // for truncated division, discussed later) For multiplication and exponentiation, the asterisk (*) is used, 5 * 10 (5 times 10) 2 ** 4 (2 to the 4th power) Multiplication is never denoted by the use of parentheses, 10 * (20 + 5) CORRECT 10(20 + 5) INCORRECT Note that parentheses may be used to denote subexpressions. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 132. Basic Input 132 The programs that we will write request and get information from the user. In Python, the input function is used for this purpose, name = input('What is your name?: ') Characters within quotes are called strings. This particular use of a string, for requesting input from the user, is called a prompt. The input function displays the string on the screen to prompt the user for input, What is your name?: Charles The underline is used here to indicate the user’s input. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 133. Basic Output 133 The print function is used to display information on the screen in Python. This may be used to display a message (string), >>> print('Welcome to My First Program!') Welcome to My First Program! or used to output the value of a variable, >>> n = 10 >>> print(n) 10 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 134. 134 Can also display a combination of strings and variables, >>> name = input('What is your name?: ') What is your name?: Charles >>> print('Hello', name) Hello Charles Note that a comma is used to separate the individual items being printed, which causes a space to appear between each when displayed. Thus, the output of the print function in this case is Hello Charles and not HelloCharles We will soon learn more about variables, operators, and input/output in Python. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 135. Using IDLE 135 In order to become familiar with writing your own Python programs using IDLE, we will create a simple program that asks the user for their name and responds with a greeting. This program utilizes the following concepts: ➤ Creating and executing Python programs ➤ Input and print functions Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 136. Creating a New Python Program 136 To create a Python program file, select New Window from the File menu in the Python shell, Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 137. 137 A new, untitled window will appear, Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 138. 138 Now can begin entering lines of a program without them being immediately executed, as in the Python shell. Note that parts of the program lines are displayed in a certain color. Since print and input are predefined function names in Python, they are colored purple. The strings in the program are colored green. The statement in red is a comment statement. Comment statements are for those reading the program, and are ignored when the program is executed. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 139. 139 When finished, save the program file by selecting Save As under the File menu, and save in the appropriate folder with the name MyFirstProgram.py. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 140. 140 To run a Python program, select Run Module from the Run menu (or simply hit function key F5). Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language Executing a Python Program
  • 141. 141 If you have entered the program code correctly, the program should execute as shown If, however, you have mistyped part of the program resulting in a syntax error (such as mistyping print), you will get an error message. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 142. 142 In such instances, you need to go back to the program window and make the needed corrections, the re-save and re-execute the program. You may need to go through this process a number of times until all the syntax errors have been corrected. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language
  • 143. 143 Dr. Frank Drake conducted the first search for radio signals from extraterrestrial civilizations in 1960. This established SETI (Search for Extraterrestrial Intelligence), a new area of scientific inquiry. In order to estimate the number of civilizations that may exist in our galaxy that we may be able to communicate with, he developed what is now called the Drake equation. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation A First Program Calculating the Drake Equation
  • 144. 144 The Drake equation accounts for a number of different factors. Some of the values are the result of scientific study, while others are only the result of an “intelligent guess.” The factors consist of, R, the average rate of star creation per year in our galaxy p, the percentage of those stars that have planets n, the average number of planets that can potentially support life for each star with planets f, the percentage of those planets that actually go on to develop life I, the percentage of those planets that go on to develop intelligent life c, the percentage of those that have the technology communicate with us and L, the expected lifetime of civilizations (the period that they can communicate). The Drake equation is simply the multiplication of all these factors, giving N, the estimated number of detectable civilizations there are at any given time, N = R * p * n * f * i * c * L Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
  • 145. 145 The following table shows those parameters in the Drake equation that have some consensus as to their correct value, as well as the values that Drake himself has used. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
  • 146. Calculating the Drake Equation The Problem 146 The value of 7 for R, the rate of star creation, is the least disputed value in the Drake equation today. Given the uncertainty of the remaining factors, develop a program that allows a user to enter their own estimated values for the remaining six factors (p, n, f, i, c, and L) and displays the calculated result. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
  • 147. Calculating the Drake Equation Problem Analysis 147 The problem is very straightforward. We only need to understand the equation provided. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
  • 148. Calculating the Drake Equation Program Design 148 The program design for this problem is straightforward. The data to be represented consist of numerical values, with the Drake equation as the algorithm. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
  • 149. 149 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation The Overall Steps of the Program
  • 150. Calculating the Drake Equation Program Implementation 150 The implementation of this program is fairly simple. The only programming elements needed are input, assignment, and print, along with the use of arithmetic operators. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
  • 151. 151 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
  • 152. 152 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation Executable Section of the Program
  • 153. Calculating the Drake Equation Python Issues to Point Out 153 • Comment statements are used, starting with a hash (#) sign • Comments at the start of the program give an overall description • Comments within code serve as section headers • Print function is used for program welcome (Iines 26-29) • Empty print function call causes a skipped line on the screen (line 44) • Print function also used to display results (lines 45-47) • Input function is used for getting factors from user for Drake Equation • The input function always returns a string value • int(input('……')) used to convert input string to an integer value Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
  • 154. Calculating the Drake Equation Example Execution 154 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
  • 155. Calculating the Drake Equation Program Testing 155 To test the program, we can calculate the Drake equation for various other values using a calculator, providing a set of test cases. A test case is a set of input values and expected output of a given program. A test plan consists of a number of test cases to verify that a program meets all requirements. A good strategy is to include “average,” as well as “extreme” or “special” cases in a test plan. Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation
  • 156. Test Plan (with results) 156 Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation