Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Seite - 86 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

Seite - 86 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Bild der Seite - 86 -

Bild der Seite - 86 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 86 -

86 4 FunctionsandtheWritingofCode Alternatively, with default values for all the parameters, and again aiming for the sameprintout,ourprogram(ball_position_xy.py)appearsas def xy(v0x=2.0, v0y=5.0, t=0.6): """Computes horizontal and vertical positions at time t""" g = 9.81 # acceleration of gravity return v0x*t, v0y*t - 0.5*g*t**2 x, y = xy() print(’Horizontal position: {:g} , Vertical position: {:g}’.format(x, y)) Running thecode,we get theprintout Horizontal position: 1.2 , Vertical position: 1.2342 Some,orall, of thedefaultvaluesmayalsobeoverridden,e.g., like x, y = xy(v0y=6.0) # override default value for v0y only whichmeansthatxywill runwithdefaultvaluesforv0xandt,whilev0ywillhave thevalue6.0, as specified in the functioncall. Theoutput then,becomes Horizontal position: 1.2 , Vertical position: 1.8342 which is reasonable, since the initial velocity for the vertical direction was higher than in thepreviouscase. 4.1.8 AFunctionwithAnotherFunctionasInputArgument Functions are straightforwardly passed as arguments to other functions. This is illustrated by the following scriptfunction_as_argument.py,where a function sumsupfunctionvaluesofanother function: def f(x): return x def g(x): return x**2 def sum_function_values(f, start, stop): """Sum up function values for integer arguments as f(start) + f(start+1) + f(start+2) + ... + f(stop)""" S = 0 for i in range(start, stop+1, 1): S = S + f(i) return S print(’Sum with f becomes {:g}’.format(sum_function_values(f, 1, 3))) print(’Sum with g becomes {:g}’.format(sum_function_values(g, 1, 3))) We note that the function sum_function_values takes a function as its first argument and repeatedly calls that function without (of course) knowing what that functiondoes, it justgets the functionvaluesbackandsums themup! Remember that the argument f in the function header sum_funcion_values (f, start, stop) and the function defined by the namef, is not the same. The
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python 3.6
Band
Second Edition
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2020
Sprache
englisch
Lizenz
CC BY 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
356
Schlagwörter
Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
Kategorie
Informatik
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python