Seite - 45 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 45 -
Text der Seite - 45 -
2.7 Exercises 45
c) Now let the statement v0 = 5 inside the function y have an indent of three
spaces (while the remaining two linesof the functionhave four).
d) Removethe left parenthesis in thefirst statementdef y(t):
e) Change the first line of the functiondefinition fromdef y(t): todef y():,
i.e., removetheparametert.
f) Change thefirstoccurrenceof thestatementprint y(time) toprint y().
Filename:errors_colon_indent_etc.py.
Exercise2.2:Compare integersaandb
Explainbriefly, inyourownwords,what the followingprogramdoes.
a = input(’Give an integer a: ’)
b = input(’Give an integer b: ’)
if a < b:
print "a is the smallest of the two numbers"
elif a == b:
print "a and b are equal"
else:
print "a is the largest of the two numbers"
Proceedbywriting theprogram,andthenrunita fewtimeswithdifferentvalues
foraandb to confirmthat itworksas intended. Inparticular, choosecombinations
foraandb so that all threebranchesof theifconstructionget tested.
Filename:compare_a_and_b.py.
Exercise2.3: Functions forcircumferenceandareaofacircle
Writeaprogramthattakesacircleradiusrasinputfromtheuserandthencomputes
the circumferenceCandareaAof the circle. Implement the computationsofC and
A as two separate functions that each takesr as input parameter. PrintC andA to
thescreenalongwithanappropriate text. Runtheprogramwith r D1andconfirm
thatyouget the right answer.
Filename:functions_circumference_area.py.
Exercise2.4: Function forareaofarectangle
Write a program that computes the areaA D bc of a rectangle. The values of b
and c should be user input to the program. Also, write the area computation as
a function that takes b and c as input parameters and returns the computed area.
Let the programprint the result to screen alongwith an appropriate text. Run the
programwithbD2andcD3 to confirmcorrectprogrambehavior.
Filename:function_area_rectangle.py.
Exercise2.5:Areaofapolygon
One of themost importantmathematical problems through all times has been to
find the areaof apolygon, especially because real estate areas oftenhad the shape
of polygons, and it was necessary to pay tax for the area. We have a polygon as
depictedbelow.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2016
- Sprache
- englisch
- Lizenz
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 248
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik