Page - 37 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 37 -
Text of the Page - 37 -
1.9 Exercises 37
1.9 Exercises
Exercise1.1:ErrorMessages
Saveacopyof theprogramball.pyandconfirmthat thecopyrunsas theoriginal.
Youarenowsupposedto introduceerrors in thecode,onebyone.Foreacherror in-
troduced,save andrun theprogram,andcommenthowwell Pythonβs responsecor-
respondstotheactualerror.Whenyouarefinishedwithoneerror,re-settheprogram
tocorrectbehavior (andcheck that it works!)beforemovingon to thenexterror.
a) Insert thewordhelloon theempty lineabove theassignment tov0.
b) Remove the# sign in frontof thecomment initial velocity.
c) Remove the= sign in theassignment tov0.
d) Change the reservedwordprint intopint.
e) Change the calculationofy toy = v0*t.
f) Change the lineprint(y) toprint(x).
Filename:testing_ball.py.
Exercise1.2:VolumeofaCube
Write a programthat computes the volumeV of a cube with sides of lengthL= 4
cm and prints the result to the screen. BothV andL should be defined as separate
variables in the program. Run the program and confirm that the correct result is
printed.
Hint Seeball.py in the text.
Filename:cube_volume.py.
Exercise1.3:Areaand CircumferenceofaCircle
Write a programthat computesboth the circumferenceC and the areaAofa circle
withradiusr=2cm.Let theresultsbeprintedto thescreenonasingle linewithan
appropriatetext.ThevariablesC,Aandr shouldallbedefinedasseparatevariables
in theprogram.Run theprogramandconfirmthat thecorrect resultsareprinted.
Filename:circumference_and_area.py.
Exercise1.4:VolumesofThreeCubes
We are interested in the volumeV of a cube with lengthL:V =L3, computed for
threedifferentvaluesofL.
a) Inaprogram,use thelinspacefunctiontocomputeandprint threevaluesofL,
equallyspacedon the interval [1,3].
b) Carry out, by hand, the computation V = L3 when L is an array with three
elements.That is, computeV foreachvalueofL.
c) Modifytheprogramina), so that itprintsout theresultVofV = L**3whenL is
an array with three elements as computed bylinspace.Compare the resulting
volumeswithyourhandcalculations.
d) Makea plotofVversusL.
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik