157
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

Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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.

1Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons

Page 2: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Computing technology has changed, and is continuing to change the world.Essentially every aspect of life has been impacted by computing. Computingrelated fields in almost all areas of study are emerging.

2Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons

Motivation

Page 3: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Computer science is fundamentally about computational problem solving.

Programming and computers are only tools in the field of computing. The fieldhas tremendous breadth and diversity. Areas of study include:

3Introduction 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?

Page 4: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Computational Problem SolvingTwo things that are needed to performcomputational problem solving:

• a representation that captures all therelevant aspects of the problem

• an algorithm that solves the problemby use of the representation

4Introduction 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 arepresentation that translates into a solution for what is being represented.

Page 5: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

The Use of Abstraction in Computational Problem Solving

A representation that leaves out detail of what is being represented isa form of abstraction.

Abstraction is prevalent in the everyday world. For example, maps areabstractions.

5Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?

Page 6: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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?

Page 7: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

7

Below is a more abstract, but topologically correct map of the London Undergroundsubway system showing the bends and curves of each track.

This map contains too much information for its purpose – to find out where eachsubway 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?

Page 8: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

8

Below is a more abstract representation of the subway system, developedby Harry Beck in 1931. The track lines are straightened out where the trackcurves are irrelevant for subway riders. This is a simpler, easier to read, andthus 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?

Page 9: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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?

Page 10: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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?

Page 11: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

11

Cans Seurat, 2007 60x92“ Copyright Chris JordanDepicts 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?

Page 12: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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?

Page 13: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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?

Page 14: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

14Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?

(Copyright Chris Jordan)

Page 15: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

15

More Images

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?

Page 16: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Abstraction in Computing

Abstraction is intrinsic to computing and computational problemsolving.

• The concept of “1s” and “0s” in digital computing is an abstraction.Digital information is actually represented as a high or low voltagelevels, magnetic particles oriented one of two ways, pits on an opticaldisk, etc.

• Programming languages are an abstraction.The instructions and data of a computer program is an abstractrepresentation of the underlying machine instructions and storage.

• Programming design involves the use of abstraction.Programs are conceptualized as various modules that work together.

16Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?

Page 17: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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?

Page 18: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

18

There is a simple algorithmic approach for solving this problem by simply trying allpossible 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? Whateverrepresentation we use, only the aspects of the problem that are relevant for itssolution need to be represented. Should we include in the representation the …

• 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 ateach step in the problem solving. Therefore, by the use of abstraction, we define arepresentation 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?

Page 19: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

19

For example, we could use a sequence to indicate where each of the objectscurrently 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 item in the sequence is the location of the cabbage, etc.

Note that the village is always on the west side of the river – it doesn’t move. Itslocation 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 ofboth the man and the boat is redundant information. The relevant, minimalrepresentation is given below (replacing “east”/”west” with single characters).

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?

Page 20: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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 thestart 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, when 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 solution is found within therepresentation used, which 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?

Page 21: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

21

For example, from the start state, there are three possible moves that can bemade, 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 STATEGoat left alone with wolf

VALID STATECabbage left alone with wolf

INVALID STATECabbage left alone with goat

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?

Page 22: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

22

We check if the new problem state is the goal state. If so, then we solved theproblem in one step! (We know that cannot be so, but the algorithmic approach thatwe 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?

Page 23: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

23

Since the man can only row across objects on the same side of the river as himself,and the only object currently with him is the goat, there are only two possiblemoves 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 STATECabbage left alone on west sideMan, Goat and Wolf on east side

VALID STATE (but unproductive)

Man and all others on east sidePreviously in this state. It is the start state. Therefore no progress made!

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?

Page 24: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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 statetranslates into a solution of the actual problem since each transition between stateshas a corresponding “real-world” action – 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?

Page 25: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

The Importance of Algorithms

25

As another example computational problem,suppose that you needed to write a programthat displays a calendar month for any givenmonth and year.

The representation of this problem is ratherstraightforward. Only a few values areneeded:

• 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?

Page 26: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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 past date andcalculate 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 doingthis. No matter how good a programmer you may be, without knowledge of theneeded 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?

Page 27: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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 isastronomical compared to the capabilities of current computing devices.

A classic problem in computer science that demonstrates this is the TravelingSalesman problem.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?

Page 28: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

The Traveling Salesman Problem

28

A salesman needs to visit aset of cities. He wants to findthe shortest route of travel,starting and ending at anycity 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 allpossible routes that can be taken, and find the shortest one – a brute forceapproach. The computational issue, therefore, is for a given set of cities, howmany possible routes are there to consider?

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?

Page 29: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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?

Page 30: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

30

Below are the number of permutations (and thus the 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?

Page 31: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

The Game of Chess

31

When a computer plays chess against a humanopponent, both have to “think ahead” to thepossible outcomes of each move it may make.

Therefore, a brute force approach can also beused for a computer playing a game of chessof “looking ahead” at all the possible movesthat can be made, each ending in a win, loss,or draw. It can then select the move each timeleading to the most number of ways ofwinning.

(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?

Page 32: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

32

There are approximately 10120 possible chess games that can be played. This isrelated 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 abrute-force approach is impractical to use, clever and more efficient problem-solving methods must be discovered that find either an exact or an approximatesolution to the problem.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?

Page 33: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science? 33

Page 34: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science? 34

Page 35: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science?

0

35

Page 36: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science? 36

Page 37: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science? 37

Page 38: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science? 38

Page 39: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.1 What is Computer Science? 39

Page 40: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

An algorithm is a finite number of clearlydescribed, unambiguous “doable” stepsthat can be systematically followed toproduce a desired result for given input ina finite amount of time (that is, iteventually terminates).

The word “algorithm” is derived from theninth-century Arab mathematician, Al-Khwarizmi who worked on “writtenprocesses to achieve some goal.”

40Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms

Computer Algorithms

Page 41: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Computer algorithms are central to computer science. They providestep-by-step methods of computation that a machine can carry out.

Having high-speed machines (computers) that can consistently follow agiven set of instructions provides a reliable and effective means ofrealizing computation. However, the computation that a given computerperforms is only as good as the underlying algorithm used.

Because computers can execute a large number of instructions veryquickly and reliably without error, algorithms and computers are aperfect 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

Page 42: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Euclid’s Algorithm is an algorithm for computing the greatest commondenominator (GCD) of two given integers. It is one of the oldestnumerical 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 AlgorithmOne of the Oldest Known Algorithms

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms

Page 43: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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

Page 44: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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

Page 45: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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, R 2

3. If R is not 0, assign M the value of N, assign N the value of R, and go to step 2.M 18, N 2.

45

Example Use

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms

Page 46: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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 = 18 / 2 = 9, 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.M 18, N 2.

46

Example Use

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms

Page 47: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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 2M/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 is 0. Therefore, proceed to step 4.

47

Example Use

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms

Page 48: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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 2M/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 is 2 (the value of N)

48

Example Use

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms

Page 49: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

49

Animation of Euclid’s Algorithm

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms

Page 50: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

50

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

Page 51: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

51Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms

Page 52: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

52

Notable Contemporary Algorithms

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms

Page 53: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

The sequencing of the human genome was dependent on thedevelopment of fast, efficient algorithms for comparing and matchingDNA sequences.

53

BLAST Algorithm

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms

Page 54: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

The RSA algorithm is the basis of public key encryption. It is requires thefactorization of large prime numbers to break, which for large enoughprimes, is considered impossible. It is the method used for secure webcommunication.

54

RSA Algorithm

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms

Page 55: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms 55

Page 56: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms 56

Page 57: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms 57

Page 58: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms 58

Page 59: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms 59

Page 60: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.2 Computer Algorithms 60

Page 61: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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 asa keyboard, monitor, mouse, and printer.

61Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Computer Hardware

Page 62: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Digital Computing: Its all About Switches

It is essential that computer hardware be reliable and error free. If thehardware gives incorrect results, then any program run on that hardwaremay give incorrect results as well.

The key to developing reliable systems is to keep the design as simpleas possible. In digital computing, all information is represented as aseries of digits, in which each digit is either “0” or “1”.

62Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 63: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Decimal Digitalization

In electronic computing, digital values are represented by discretevoltage levels. Suppose that computers represented numbers as we areused to, in base ten.

63Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 64: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

In electronic computers, each digit is represented by a different voltagelevel. The more voltage levels (digits) that the hardware must utilize anddistinguish between, the more complex the hardware becomes to design.This results in greater chance of hardware design errors.

64Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 65: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Information Theory

The Fundamental Theorem of InformationScience of Claude Shannon (known as the“Father of Information Theory”) states thatall information can be represented by theuse of only two symbols, e.g., 0 and 1.

This is referred to as binary representation.

65Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 66: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Binary DigitalizationIn a binary representation, each digit can be one of only two possiblevalues, similar to a light switch that can be either on or off. Computerhardware, therefore, is based on the use of simple electronic “on/off”switches called transistors that can be switched at essentially the speedof light.

66Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 67: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

The TransistorThe transistor, an electronic switching component developed in 1947,revolutionized electronics. Its invention it what has allowed for all of thedramatic advances in computing technology that we continue to seetoday. (For the historical development of the transistor, see Chapter 12.)

67Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 68: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Integrated Circuits

Integrated circuits (“chips”), the building blocks of computer hardware,are comprised of millions or even billions of transistors. It solved theproblem of how to “wire together” this multitude of components. (For thehistorical development of the integrated circuit, see Chapter 12.)

68Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 69: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

The Binary Number SystemFor representing numbers, any base (radix) can be used. For example, inbase 10, there are ten possible digits (0, 1, ..., 9), in which column valuesare 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

69Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 70: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

For representing numbers in base 2, there are two possible digits (0, 1)in which column values are 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

70

Although values represented in base 2 are significantly longer than those inbase 10, binary representation is used in digital computing because of theresulting simplicity of hardware design

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 71: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Bits and Bytes

Each binary digit is referred to as a bit. A group of (usually) eight bits iscalled a byte. Converting a base ten number to base two involves thesuccessive division of the number by 2.

71Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 72: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Fundamental Hardware ComponentsCentral 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 bemagnetic (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)

72Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 73: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

73Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 74: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Operating Systems

An operating system is software that manages and interactswith the hardware resources of a computer. It acts as the“middle man” between the hardware and executingapplication programs. For example, it controls the allocationof memory for the various programs that may be executingon a computer.

Because an operating system is intrinsic to the operation ofa computer, it is referred to as system software.

74Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 75: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Operating systems also provide a particular user interface.

It is the operating system installed on a computer thatdetermines the “look and feel” of the user interface and howthe user interacts with the system, and not the particularmodel computer.

75Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 76: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

76Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 77: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Limits of Integrated Circuits Technology: Moore’s Law

In 1965, Gordon E. Moore, one of the pioneers in thedevelopment of integrated circuits and cofounder of IntelCorporation, predicted that the number of transistors thatwould be able to be put on a silicon chip would doubleroughly every two years, allowing the complexity andtherefore the capabilities of integrated circuits to growexponentially. This prediction became known as Moore’sLaw.

77Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 78: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Amazingly, to this day thatprediction has held true.While this doubling ofperformance cannot go onindefinitely, it has not yetreached its limit.

78Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 79: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

79Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 80: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

80Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 81: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

81Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 82: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

82Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 83: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

83Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 84: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

84Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 85: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

85Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 86: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

86Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 87: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

87Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.3 Computer Hardware

Page 88: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

What is computer software?Computer software is a set of program instructions,including related data and documentation, that can beexecuted by computer. This can be in the form of instructionson 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.

88Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Computer Software

Page 89: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

The First Computer Programmer

The first computer programs everwritten were for a mechanicalcomputer designed by CharlesBabbage in the mid-1800s. Theperson who wrote theseprograms was a woman, AdaLovelace, who was a talentedmathematician. Thus, she isreferred to as “the first computerprogrammer.” (For more on thishistory, see Chapter 12).

89Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 90: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Syntax, Semantics andProgram Translation

Programming languages (called “artificial languages”) arelanguages just as “natural languages” such as English andMandarin (Chinese). Syntax and semantics are importantconcepts that apply to all languages.

90Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 91: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

The syntax of a language is a set of characters and the acceptablesequences (arrangements) of those characters.

English, for example, includes the letters of the alphabet,punctuation, and properly spelled words and properly punctuatedsentences. The following is a syntactically correct sentence inEnglish,

“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.

91Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Syntax

Page 92: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

The semantics of a language is the meaning associated with eachsyntactically correct sequence of characters.

Consider the following sentence:

“Colorless green ideas sleep furiously.”

This sentence is syntactically correct, but has no meaning. Thus, it issemantically incorrect.

Every language has its own syntax and semantics.

92Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Semantics

Page 93: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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 alphabetrather than Chinese characters for writing Mandarin.)

93Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 94: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Program Translation

A central processing unit (CPU) is designed to interpret andexecute a specific set of instructions represented in binaryform (i.e., 1s and 0s) called machine code. Only programs inmachine code can be executed by a CPU.

94Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 95: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

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 instructionsof such programs are not in machine code that a CPU canexecute, a translator program must be used.

There are two fundamental types of translators:

• Compiler software that translates programs into machine code to be executed by the CPU

• Interpreter software that executes programs inplace of the CPU

95Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 96: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

96

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

Page 97: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

97

Interpreter

An interpreter can immediately execute instructions as they areentered. This is referred to as interactive mode. This is a very usefulfeature for program development. Python, as we shall see, is executedby an interpreter.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 98: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

98

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 inevitableduring program development.

Syntax errors are caused by invalid syntax (for example, entering prntinstead of print).

Since a translator cannot understand instructions containing syntaxerrors, translators terminate when encountering such errors indicatingwhere in the program the problem occurred.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 99: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

99

In contrast, semantic errors (generally called logic errors ) are errors inprogram logic. Such errors cannot be automatically detected, sincetranslators cannot understand the intent of a given computation.Therefore, programs are not terminated when containing logic errors,but give incorrect results.

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 shouldbe 3 and not 2. Computers do not understand what a program is meantto do, they only follow the instructions given. It is up to theprogrammer to detect such errors.

Program debugging is not a trivial task, and constitutes much of thetime of program development.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 100: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

100

In 1947, engineers working on the Mark IIcomputer at Harvard University found amoth stuck in one of the components,causing the machine to malfunction. Theytaped the insect in their logbook andlabeled it “first actual case of bug beingfound.”

The First Actual Computer “Bug”

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

The term program “bug” (and “debugging”) has become a standard part of thelanguage of computer programmers. The log book, complete with the attached bug, ison display at the Smithsonian Institution in Washington, D.C.

Page 101: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

101

Programming languages fall into a number of programming paradigms. Thetwo major programming paradigms in use today are procedural(imperative) programming and object-oriented programming. Each providesa different way of thinking about computation.

While most programming languages only support one paradigm, Pythonsupports both procedural and object-oriented programming. We will startwith 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

Page 102: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

102Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 103: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

103Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 104: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

104Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 105: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

105Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 106: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

106Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 107: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

107Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 108: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

108Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 109: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

109Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 110: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

110Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.4 Computer Software

Page 111: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Computational problem solving does not simply involve theact of computer programming. It is a process, withprogramming only one of the steps.

Before a program is written, a design for the program mustbe developed. And before a design can be developed, theproblem to be solved must be well understood. Oncewritten, the program must be thoroughly tested.

111Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving

The Process of Computational Problem Solving

Page 112: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

112

Computational Problem Solving Steps

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving

Page 113: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

113

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

Page 114: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

114

Knowing what constitutes a solution.

For some problems, there is only one solution. For others, there may be anumber (or infinite number) of solutions. Thus, a problem may be stated asfinding either,

• 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

Page 115: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

115

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

Page 116: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

116

Table Representation of Data for theTraveling Salesman Problem

Note that only half of the table need be stored, since going from Atlanta to Boston or Boston to Atlanta, the distance is the same

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving

Page 117: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

117

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

Below are two possible ways to represent the pieces on a chess board.

Page 118: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

118

Describing the Algorithms Needed

When solving a computational problem, either suitable existingalgorithms 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

Page 119: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

119

For the MCGW problem, there are standard search algorithms thatcan 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

Page 120: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

120

Program Implementation

Design decisions provide general details of the data representationand the algorithmic approaches for solving a problem. The details,however, do not specify which programming language to use, orhow to implement the program. Those are decisions for theimplementation phase.

Since we are programming in Python, the implementation needs tobe expressed in a syntactically correct and appropriate way, usingthe 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

Page 121: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

121

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 softwaredevelopment. Testing is done incrementally as a program is beingdeveloped, when the program is complete, and when the programneeds to be updated.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.5 Computational Problem Solving

Page 122: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

122

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 befully 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

Page 123: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

123

The Python Programming Language wascreated by Guido van Rossum. It was firstreleased in the early 1990s.

Its name comes from a 1970s Britishcomedy sketch show called MontyPython’s Flying Circus. (An example oftheir work is The Argument Clinic).

Companies and organizations that usePython include YouTube, Google, Yahooand NASA.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language

The Python Programming Language

Page 124: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Python Features

124

• Simple SyntaxPython programs are clear and easy to read

• Interpreted LanguagePython instructions can be executed interactively

• Powerful Programming FeaturesCan accomplish significant computation with few instructions

• Numerous Python Modules Provide Additional Capabilities

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language

Page 125: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

The IDLE Python Development Environment

125

IDLE is an integrated development environment (IDE). An IDE is a bundledset of software tools for program development. This typically includes,

• an editor for creating and modifying programs

• a translatorfor executing programs, and

• a program debuggerfor 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

Page 126: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

The Python Shell

126

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 the promptoutputs the result 5, redisplaying 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

Page 127: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

The Python Standard Library

127

The Python Standard Library is a collection of built-in modules, eachproviding specific functionality beyond what is included in the “core” partof 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 at the end of the book.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language

Page 128: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Importing a Library Module

128

In order to utilize the capabilities of modules in a specific program, animport statement is used as shown.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language

Page 129: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

129

Because the factorial function is from the math module, the function iscalled 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

Page 130: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

A Bit of Python

130

We introduce a bit of Python, just enough to begin writing some simpleprograms.

Since all computer programs,

• input data• process the data• output results

We look at the notion of a variable, how to perform some simple arithmeticcalculations, 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

Page 131: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Variables

131

One of the most fundamental concepts in programming is that of avariable.

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, the current value of n isused, as in the following,

n + 20 (5 + 20) 25

If variable n is assigned a new value, then the same expression willproduce a different result,

n = 10n + 20 (10 + 20) 30

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language

Page 132: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Some Basic Arithmetic Operators

132

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)

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

Page 133: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Basic Input

133

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, forrequesting input from the user, is called a prompt.

The input function displays the string on the screen to prompt the user forinput,

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

Page 134: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Basic Output

134

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

Page 135: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

135

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, theoutput of the print function in this case is

Hello Charles

and not

HelloCharles

We will soon learn more about variables, operators, and input/output inPython.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language

Page 136: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Using IDLE

136

In order to become familiar with writing your own Python programs usingIDLE, we will create a simple program that asks the user for their name, andthen 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

Page 137: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Creating a New Python Program

137

To create a Python program file, select New Window from the File menu inthe Python shell,

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language

Page 138: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

138

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

Page 139: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

139

Now can begin entering lines of a program without them being immediatelyexecuted, as in the Python shell.

Note that parts of the program lines are displayed in a certain color. Sinceprint and input are predefined function names in Python, they arecolored purple. The strings in the program are colored green. The statementin red is a comment statement. Comment statements are for those readingthe 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

Page 140: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

140

When finished, save the program file by selecting Save As (under the Filemenu) 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

Page 141: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

141

To run a Python program, select Run Module from the Run menu (or simplyhit 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

Page 142: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

142

If you have entered the program code correctly, the program should executeas shown

However, if 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

Page 143: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

143

In such instances, you need to go back to the program window and make theneeded corrections, the re-save and re-execute the program. You may needto go through this process a number of times until all the syntax errors havebeen corrected.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.6 Python Programming Language

Page 144: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

144

Dr. Frank Drake conducted the first search forradio signals from extraterrestrial civilizationsin 1960. This established SETI (Search forExtraterrestrial Intelligence), a new area ofscientific inquiry.

In order to estimate the number of civilizationsthat may exist in our galaxy that we may beable to communicate with, he developed whatis 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 ProgramCalculating the Drake Equation

Page 145: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

145

The Drake equation accounts for a number of different factors. Some of thevalues 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 galaxyp, the percentage of those stars that have planetsn, the average number of planets that can potentially support life for each star with planetsf, the percentage of those planets that actually go on to develop lifeI, the percentage of those planets that go on to develop intelligent lifec, the percentage of those that have the technology communicate with us andL, 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

Page 146: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

146

The following table shows those parameters in the Drake equation that havesome consensus as to their correct value, as well as the values that Drakehimself has used.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation

Page 147: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Calculating the Drake Equation

The Problem

147

The value of 7 for R, the rate of star creation, is the least disputed value inthe Drake equation today.

Given the uncertainty of the remaining factors, develop a program thatallows a user to enter their own estimated values for the remaining sixfactors (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

Page 148: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Calculating the Drake Equation

Problem Analysis

148

The problem is very straightforward. We only need to understand theequation provided.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation

Page 149: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Calculating the Drake Equation

Program Design

149

The program design for this problem is straightforward. The data to berepresented consist of numerical values, with the Drake equation as thealgorithm.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation

Page 150: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

150Introduction 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

Page 151: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Calculating the Drake Equation

Program Implementation

151

The implementation of this program is fairly simple. The only programmingelements needed are input, assignment, and print, along with the use ofarithmetic operators.

Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation

Page 152: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

152Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation

Page 153: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

153Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation

Page 154: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Calculating the Drake Equation

Python Issues to Point Out

154

• Comment statements start with a hash (#) sign• Comments at the start of the program give an overall program description• Comments within code serve as section headers

• Print function is used for program welcome (Iines 26-29)• Empty print function calls cause 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

Page 155: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Calculating the Drake Equation

Example Execution

155Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation

Page 156: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Calculating the Drake Equation

Program Testing

156

To test the program, we can calculate the Drake equation for various othervalues 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 meetsall 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

Page 157: Chapter 1 Introductionpeople.uncw.edu/vetterr/classes/csc500-fall2020/Dierbach... · 2019. 8. 20. · Chapter 1 Introduction This chapter addresses the question “What is computer

Test Plan (with results)

157Introduction to Computer Science Using Python – Dierbach Copyright 2013 John Wiley and Sons Section 1.7 A First Program – Drake Equation