PIntroduction
Solution and Answer Guide For All Chapter
OP OP OP OP OP OP
s
,SolutionOPandOPAnswerOPGuideOPForOPAllOPChapters:OPLambert,OPFundamentalsOPofOPPython:OPFirstOPPrograms,OP3e,OPCY24,OP9780357881019;OPChapterOP1,O
PIntroduction
TABLE OF CONTENTS OP OP
Exercise Solutions ....................................................................................................................................... 1
OP
Exercise 1.1 .............................................................................................................................................. 1
OP
Exercise 1.2 .............................................................................................................................................. 2
OP
Exercise 1.3 .............................................................................................................................................. 3
OP
Review Questions Answers ....................................................................................................................... 4
OP OP
Programming Exercises Solutions............................................................................................................ 8
OP OP
Debugging Exercises Solutions ................................................................................................................. 8
OP OP
EXERCISE SOLUTIONS OP
EXERCISE 1.1 OP
1. List three common types of computing agents.
OP OP OP OP OP OP
Solution:
Human beings, desktop computers, cell phones
OP OP OP OP OP
2. Write an algorithm that describes the second part of the process of making change (counting out
OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP
the coins and bills).
OP OP OP
Solution:
There are various ways to do this, but here is one:
OP OP OP OP OP OP OP OP OP OP
Repeat
Select the largest unit of money that is less than or equal to the remaining
OP OP OP OP OP OP OP OP OP OP OP OP OP OP
change Subtract this unit from the remaining change
OP OP OP OP OP OP OP OP
Until the remaining change is 0
OP OP OP OP OP
The collection of units selected represent the change
OP OP OP OP OP OP OP
3. Write an algorithm that describes a common task, such as baking a cake.
OP OP OP OP OP OP OP OP OP OP OP OP
,SolutionOPandOPAnswerOPGuideOPForOPAllOPChapters:OPLambert,OPFundamentalsOPofOPPython:OPFirstOPPrograms,OP3e,OPCY24,OP9780357881019;OPChapterOP1,O
PIntroduction
Solution:
There are various ways to do this, but here is one:
OP OP OP OP OP OP OP OP OP OP
Preheat an oven to 375 degrees
OP OP OP OP OP
Add 1 cup of water and 1 egg to a mixing bowl
OP OP OP OP OP OP OP OP OP OP OP
Beat the liquid mixture in the bowl until the ingredients are blen
OP OP OP OP OP OP OP OP OP OP OP
ded Add the contents of a boxed cake mix to the mixing bow
OP OP OP OP OP OP OP OP OP OP OP OP
l
Beat the mixture in the bowl until the ingredients are blended
OP OP OP OP OP OP OP OP OP OP
Pour the contents of the mixing bowl into a lightly greased cak
OP OP OP OP OP OP OP OP OP OP OP
e pan Bake the cake in the oven for 45 minutes
OP OP OP OP OP OP OP OP OP OP
4. Describe an instruction that is not well defined and thus could not be included as a step in an
OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP O
algorithm. Give an example of such an instruction.
P OP OP OP OP OP OP OP
Solution:
Attempting to divide a number by 0 OP OP OP OP OP OP
5. In what sense is a laptop computer a general-purpose problem-solving machine?
OP OP OP OP OP OP OP OP OP OP
Solution:
A laptop computer is a general-purpose problem-
OP OP OP OP OP OP
solving machine because it is programmable and can solve any problem for which there is an al
OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP
gorithm.
6. List four devices that use computers and describe the information that they process. (Hint: Thi
OP OP OP OP OP OP OP OP OP OP OP OP OP OP
nk of the inputs and outputs of the devices.)
OP OP OP OP OP OP OP OP
Solution:
Digital camera—images, music player—sound, cell phone—text, ATM—numbers
OP OP OP OP OP OP
EXERCISE 1.2 OP
1. List two examples of input devices and two examples of output devices.
OP OP OP OP OP OP OP OP OP OP OP
Solution:
Input devices—keyboard and mouse, output devices—monitor and speakers
OP OP OP OP OP OP OP
2. What does the central processing unit (CPU) do?
OP OP OP OP OP OP OP
Solution:
The CPU fetches, decodes, and executes instructions.
OP OP OP OP OP OP
3. How is information represented in hardware memory?
OP OP OP OP OP OP
Solution:
Information is represented using binary notation, which in hardware is a pattern of voltage levels.
OP OP OP OP OP OP OP OP OP OP OP OP OP OP
, SolutionOPandOPAnswerOPGuideOPForOPAllOPChapters:OPLambert,OPFundamentalsOPofOPPython:OPFirstOPPrograms,OP3e,OPCY24,OP9780357881019;OPChapterOP1,O
PIntroduction
4. What is the difference between a terminal-based interface and a graphical user interface?
OP OP OP OP OP OP OP OP OP OP OP OP
Solution:
A terminal-
OP
based interface supports only the input and output of text with a keyboard and monitor. A graphi
OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP
cal user interface supports the output of images and the manipulation of them with a pointing devi
OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP
ce, the mouse.
OP OP
5. What role do translators play in the programming process?
OP OP OP OP OP OP OP OP
Solution:
A translator converts a program written in a high-
OP OP OP OP OP OP OP OP
level language (human readable and writable) to an equivalent program in a low-
OP OP OP OP OP OP OP OP OP OP OP OP
level language (machine readable and executable).
OP OP OP OP OP
EXERCISE 1.3 OP
1. Describe what happens when the programmer enters the string "Greetings!" in the Python shell.
OP OP OP OP OP OP OP OP OP OP OP OP OP
Solution:
Python reads the string "Greetings!", evaluates it, and displays this string (including single
OP OP OP OP OP OP OP OP OP OP OP OP OP
quotes) in the shell. OP OP OP
2. Write a line of code that prompts the user for their name and saves the user’s input in a variable calle
OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP
d
name.
Solution:
name= input("Enter your name: ")
P
O OP OP OP OP
3. What is a Python script?
OP OP OP OP
Solution:
A Python script is a complete Python program that can be run from a computer’s operating system.
OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP
4. Explain what goes on behind the scenes when your computer runs a Python program.
OP OP OP OP OP OP OP OP OP OP OP OP OP
Solution:
If the program has not already been translated, Python’s compiler translates it to byte code. The P
OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP OP
ython virtual machine then executes this code.
OP OP OP OP OP OP