FUNDAMENTALS OF PYTHON FIRST
PROGRAM 3E BY KENNETH
A.LAMBERT
,SolutionandAnswerGuideForAll Chapters si si si si si si
LAMBERT, FUNDAMENTALS OF PYTHON: FIRST PROGRAMS, 3E, CY24, 9780357881019; CHAPTER 1, INTR
S I S I S I SI S I S I S I S I SI S I SISI
ODUCTION
TABLE OF CONTENTS SI SI
Exercise Solutions ...................................................................................................................................... 1
si
Exercise 1.1.............................................................................................................................................. 1
si
Exercise 1.2.............................................................................................................................................. 2
si
Exercise 1.3.............................................................................................................................................. 3
si
Review Questions Answers ........................................................................................................................ 4
si si
Programming Exercises Solutions ............................................................................................................. 8
si si
Debugging Exercises Solutions .................................................................................................................. 8
si si
EXERCISE SOLUTIONS SI
EXERCISE1.1 SI
1. List three common types of computing agents.
si si si si si si
Solution:
Human beings, desktop computers, cell phones
si si si si si
2. Write an algorithm that describes the second part of the process of making change (counting out the coins and bi
si si si si si si si si si si si si si si si si si si si
lls).
Solution:
There are various ways to do this, but here is one:
si si si si si si si si si si
Repeat
Select the largest unit of money that is less than or equal to the remaining change Subtr
si si si si si si si si si si si si si si si si
act this unit from the remaining change
si si si si si si
Until the remaining change is 0
si si si si si
The collection of units selected represent the change
si si si si si si si
3. Write an algorithm that describes a common task, such as baking a cake.
si si si si si si si si si si si si
, Solution:
There are various ways to do this, but here is one:
si si si si si si si si si si
Preheat an oven to 375 degrees si si si si si
Add 1 cup of water and 1 egg to a mixing bowl
si si si si si si si si si si si
Beat the liquid mixture in the bowl until the ingredients are blended Add th
si si si si si si si si si si si si si
e contents of a boxed cake mix to the mixing bowl
si si si si si si si si si si
Beat the mixture in the bowl until the ingredients are blended
si si si si si si si si si si
Pour the contents ofthe mixing bowl into a lightly greased cake pan Bake
si si si si si si si si si si si si si si
the cake in the oven for 45 minutes
si si si si si si si
4. Describe an instruction that is not well defined and thus could not be included as a step in an algorithm. Give a
si si si si si si si si si si si si si si si si si si si si si
n example of such an instruction.
si si si si si
Solution:
Attempting to divide a number by0 si si si si si si
5. In what sense isa laptop computer a general-purpose problem-solving machine?
si si si si si si si si si si
Solution:
Alaptop computer is a general-purpose problem-
si si si si si si
solving machine because it is programmable and can solve any problem for which there is an algorithm.
si si si si si si si si si si si si si si si si
6. List four devices that use computers and describe the information that they process. (Hint: Think of the input
si si si si si si si si si si si si si si si si si
s and outputs of the devices.)
si si si si si
Solution:
Digitalcamera—images, musicplayer—sound, cellphone—text, ATM—numbers
si si si si si si
EXERCISE1.2 SI
1. List two examples of input devices and two examples ofoutput devices.
si si si si si si si si si si si
Solution:
Inputdevices—keyboard and mouse, outputdevices—monitor and speakers
si si si si si si si
2. What does the central processing unit (CPU) do?
si si si si si si si
Solution:
The CPU fetches, decodes, and executes instructions.
si si si si si si
3. Howisinformation represented inhardware memory?
si si si si si si
Solution:
Informationis represented using binary notation, which in hardware is a pattern of voltage levels. si si si si si si si si si si si si si si
, 4. What is the difference betweena terminal-based interface and a graphical user interface?
si si si si si si si si si si si si
Solution:
A terminal-
si
based interface supports only the input and output of text with a keyboard and monitor. A graphical user interfac
si si si si si si si si si si si si si si si si si si
e supports the output of images and the manipulation of them with a pointing device, the mouse.
si si si si si si si si si si si si si si si si
5. What role do translators playin the programming process?
si si si si si si si si
Solution:
A translator converts a program written in a high-
si si si si si si si si
level language (human readable and writable) to an equivalent program in a low-
si si si si si si si si si si si si
level language (machine readable and executable).
si si si si si
EXERCISE1.3 SI
1. Describe what happens when the programmer entersthe string "Greetings!" inthe Python shell.
si si si si si si si si si si si si si
Solution:
Python reads the string "Greetings!", evaluates it, and displays this string (including single quotes) in the
si si si si si si si si si si si si si si si si
shell.
2. Write a line of code that prompts the user for their name and saves the user’s input in a variable called
si si si si si si si si si si si si si si si si si si si si
name.
Solution:
name= input("Enter your name: ")
i
s si si si si
3. What is a Python script?
si si si si
Solution:
APython script is a complete Python programthat can be run froma computer’s operating system.
si si si si si si si si si si si si si si si si
4. Explain what goes on behind the scenes when your computer runs a Python program.
si si si si si si si si si si si si si
Solution:
If the program has not already been translated, Python’s compiler translates it to byte code. The Python virtual m
si si si si si si si si si si si si si si si si si si
achine then executes this code.
si si si si