n
Solution and Answer Guide For All Chapters
v v v v v v
LAMBERT, FUNDAMENTALS OF PYTHON: FIRST PROGRAMS, 3E, CY24, 9780357881019; CHAPTER 1, INTR
V V V V V V V V V V V
ODUCTION
TABLE OF CONTENTS V V
ExercisevSolutions ........................................................................................................................................ 1
Exercisev1.1 ............................................................................................................................................... 1
Exercisev1.2 ............................................................................................................................................... 2
Exercisev1.3 ............................................................................................................................................... 3
ReviewvQuestionsvAnswers ......................................................................................................................... 4
ProgrammingvExercisesvSolutions.............................................................................................................. 8
DebuggingvExercisesvSolutions ................................................................................................................... 8
EXERCISE SOLUTIONS V
EXERCISE 1.1 V
1. v Listvthreevcommonvtypesvofvcomputingvagents.
Solution:
Humanvbeings,vdesktopvcomputers,vcellvphones
2. Writevanvalgorithmvthatvdescribesvthevsecondvpartvofvthevprocessvofvmakingvchangev(countingvoutvthevcoins
vand vbills).
Solution:
Therevarevvariousvwaysvtovdovthis,vbutvherevisvone:
Repeat
Selectvthevlargestvunitvofvmoneyvthatvisvlessvthanvorvequalvtovthevremainingvchange
vSubtractvthisvunitvfromvthevremainingvchange
Untilvthevremainingvchangevisv0
Thevcollectionvofvunitsvselectedvrepresentvthevchange
3. Writevanvalgorithmvthatvdescribesvavcommonvtask,vsuchvasvbakingvavcake.
©v2024vCengage.vAllvRightsvReserved.vMayvnotvbevscanned,vcopiedvorvduplicated,vorvpostedvtovavpubliclyvaccessib 1
le
website,vinvwholevorvinvpart.
,SolutionvandvAnswervGuidevForvAllvChapters:vLambert,vFundamentalsvofvPython:vFirstvPrograms,v3e,vCY24,v9780357881019;vChapterv1,vIntroductio
n
Solution:
Therevarevvariousvwaysvtovdovthis,vbutvherevisvone:
Preheatvanvovenvtov375vdegrees
Addv1vcupvofvwatervandv1veggvtovavmixingvbowl
BeatvthevliquidvmixturevinvthevbowlvuntilvthevingredientsvarevblendedvAd
dvthevcontentsvofvavboxedvcakevmixvtovthevmixingvbowl
Beatvthevmixturevinvthevbowlvuntilvthevingredientsvarevblended
Pourvthevcontentsvofvthevmixingvbowlvintovavlightlyvgreasedvcakevpanv
Bakevthevcakevinvthevovenvforv45vminutes
4. Describevanvinstructionvthatvisvnotvwellvdefinedvandvthusvcouldvnotvbevincludedvasvavstepvinvanvalgorithm.
vGivevanvexample vofvsuchvanvinstruction.
Solution:
Attemptingvtovdividevavnumbervbyv0
5. Invwhatvsensevisvavlaptopvcomputervavgeneral-purposevproblem-solvingvmachine?
Solution:
Avlaptopvcomputervisvavgeneral-purposevproblem-
solvingvmachinevbecausevitvisvprogrammablevandvcanvsolvevanyvproblemvforvwhichvtherevisvanvalgorithm.
6. Listvfourvdevicesvthatvusevcomputersvandvdescribevthevinformationvthatvtheyvprocess.v(Hint:vThinkvofvthev
inputsvandvoutputsvofvthevdevices.)
Solution:
Digitalvcamera—images,vmusicvplayer—sound,vcellvphone—text,vATM—numbers
EXERCISE 1.2 V
1. Listvtwovexamplesvofvinputvdevicesvandvtwovexamplesvofvoutputvdevices.
Solution:
Inputvdevices—keyboardvandvmouse,voutputvdevices—monitorvandvspeakers
2. Whatvdoesvthevcentralvprocessingvunitv(CPU)vdo?
Solution:
ThevCPUvfetches,vdecodes,vandvexecutesvinstructions.
3. Howvisvinformationvrepresentedvinvhardwarevmemory?
Solution:
Informationvisvrepresentedvusingvbinaryvnotation,vwhichvinvhardwarevisvavpatternvofvvoltagevlevels.
©v2024vCengage.vAllvRightsvReserved.vMayvnotvbevscanned,vcopiedvorvduplicated,vorvpostedvtovavpubliclyvaccessib 2
le
website,vinvwholevorvinvpart.
,SolutionvandvAnswervGuidevForvAllvChapters:vLambert,vFundamentalsvofvPython:vFirstvPrograms,v3e,vCY24,v9780357881019;vChapterv1,vIntroductio
n
4. Whatvisvthevdifferencevbetweenvavterminal-basedvinterfacevandvavgraphicalvuservinterface?
Solution:
Avterminal-
basedvinterfacevsupportsvonlyvthevinputvandvoutputvofvtextvwithvavkeyboardvandvmonitor.vAvgraphicalvuservint
erfacevsupportsvthevoutputvofvimagesvandvthevmanipulationvofvthemvwithvavpointingvdevice,vthevmouse.
5. Whatvrolevdovtranslatorsvplayvinvthevprogrammingvprocess?
Solution:
Avtranslatorvconvertsvavprogramvwrittenvinvavhigh-
levelvlanguagev(humanvreadablevandvwritable)vtovanvequivalentvprogramvinvavlow-
levelvlanguagev(machinevreadablevandvexecutable).
EXERCISE 1.3 V
1. Describevwhatvhappensvwhenvthevprogrammerventersvthevstringv"Greetings!"vinvthevPythonvshell.
Solution:
Pythonvreadsvthevstringv"Greetings!",vevaluatesvit,vandvdisplaysvthisvstringv(includingvsinglevquotes)vi
nvthevshell.
2. Writevavlinevofvcodevthatvpromptsvthevuservforvtheirvnamevandvsavesvthevuser’svinputvinvavvariablevcalled
name.
Solution:
namev=vinput("Entervyourvname:v")
3. WhatvisvavPythonvscript?
Solution:
AvPythonvscriptvisvavcompletevPythonvprogramvthatvcanvbevrunvfromvavcomputer’svoperatingvsystem.
4. ExplainvwhatvgoesvonvbehindvthevscenesvwhenvyourvcomputervrunsvavPythonvprogram.
Solution:
Ifvthevprogramvhasvnotvalreadyvbeenvtranslated,vPython’svcompilervtranslatesvitvtovbytevcode.vThevPythonvvir
tualvmachinevthenvexecutesvthisvcode.
©v2024vCengage.vAllvRightsvReserved.vMayvnotvbevscanned,vcopiedvorvduplicated,vorvpostedvtovavpubliclyvaccessib 3
le
website,vinvwholevorvinvpart.
, SolutionvandvAnswervGuidevForvAllvChapters:vLambert,vFundamentalsvofvPython:vFirstvPrograms,v3e,vCY24,v9780357881019;vChapterv1,vIntroductio
n
REVIEW QUESTIONS ANSWERS
V V
1. Whichvofvthevfollowingvisvanv examplevofvanvalgorithm?
a. Avdictionary
b. Avrecipe
c. Avshoppingvlist
d. Thevspellingvcheckervofvavwordvprocessor
Answer:vb
Feedback:
a. Incorrect.vAvdictionaryvisvavdatavstructure.
b. Correct.vAvrecipevisvavsetvofvinstructionsvthatvdescribesvavprocessvthatvhaltsvwithvavsolutionvtovavproblem.
c. Incorrect.vAvshoppingvlistvisvavdatavstructure.
d. Incorrect.v Avwordvprocessorvisvavprogramvthatvconsistsvofvinstructionsvandvdata.
2. Whichvofvthevfollowingvcontainsvinformation?
a. AnvaudiovCD
b. Avrefrigerator
c. Anvautomobile
d. Avstereovspeaker
Answer:vavFeedbac
v
k:
a. Correct.vThevinformationvonvanvaudiovCDvrepresentsvsound.
b. Incorrect.vAvrefrigeratorvcontainsvjustvfood,vifvit’svnotvempty.
c. Incorrect.vAnvautomobilevcontainsvavsteeringvwheel,vengine,vexhaustvpipe,vandvsovon.
d. Incorrect.vAvstereovspeakervcontainsvavmagneticvcoil,vwires,vandvsovon.
3. Whichvofvthevfollowingvisvavgeneral-purposevcomputingvdevice?
a. Avsmartphone
b. Avportablevmusicvplayer
c. Avmicrowavevoven
d. Avprogrammablevthermostat
©v2024vCengage.vAllvRightsvReserved.vMayvnotvbevscanned,vcopiedvorvduplicated,vorvpostedvtovavpubliclyvaccessib 4
le
website,vinvwholevorvinvpart.