Page - 27 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python
Image of the Page - 27 -
Text of the Page - 27 -
1.6 Exercises 27
Exercise1.2:Volumeofacube
Writeaprogramthat computes thevolumeV ofacubewith sidesof lengthLD4
cmandprints the result to the screen. BothV andL shouldbedefinedas 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:Areaandcircumferenceofacircle
Writeaprogramthatcomputesboth thecircumferenceC and theareaAofacircle
with radius r D 2 cm. Let the results beprinted to the screenona single linewith
an appropriate text. The variablesC,A and r should all be defined as a separate
variables in theprogram. Run theprogramandconfirm that the correct results are
printed.
Filename:circumference_and_area.py.
Exercise1.4:Volumesof threecubes
Weare interested in thevolumeV ofacubewith lengthL:V DL3, computed for
threedifferentvaluesofL.
a) Use thelinspacefunctiontocompute threevaluesofL, equallyspacedon the
interval Ε1;3 .
b) CarryoutbyhandthecomputationV DL3 ifL isanarraywith threeelements.
That is, computeV foreachvalueofL.
c) In aprogram,write out the resultVofV = L**3whenL is an arraywith three
elements as computedbylinspace in a). Compare the resultingvolumeswith
yourhandcalculations.
d) MakeaplotofVversusL.
Filename:volume3cubes.py.
Exercise1.5:Averageof integers
Write a program that stores the sum 1C2C3C4C5 in one variable and then
createsanothervariablewith theaverageof thesefivenumbers. Print theaverageto
the screenandcheck that the result is correct.
Filename:average_int.py.
Exercise1.6: Interactivecomputingofvolumeandarea
a) Compute the volume inExercise 1.2 byusingPython interactively, i.e., do the
computationsat thecommandprompt (inaPythonshell aswealso say). Com-
parewithwhatyougotpreviously fromthewrittenprogram.
b) Do the samealso forExercise1.3.
back to the
book Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations β Python
A Gentle Introduction to Numerical Simulations with Python
- Title
- Programming for Computations β Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2016
- Language
- English
- License
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 248
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik