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

Seite - 32 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Bild der Seite - 32 -

Bild der Seite - 32 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text der Seite - 32 -

32 2 BasicConstructions will be understoodbyPython asfirst compute the expression, then send the result back (i.e., return) to where the functionwascalled from. Bothdefandreturnare reservedwords. Thefunctiondependsont, i.e.,onevariable(orwesaythat it takes oneargument or input parameter), the valueofwhichmust be providedwhen the function is called. What actually happenswhen Pythonmeets this code? The def line just tells Python thathere is a functionwithnameyand it hasoneargumentt. Pythondoes not look into the function at this stage (except that it checks the code for syntax errors). WhenPython later onmeets the statementprint y(time), it recognizes a function cally(time) and recalls that there is a functiony definedwith one ar- gument. The value of time is then transferred to the y(t) function such that t = timebecomes thefirst action in they function. ThenPython executes one line at a time in the y function. In the final line, the arithmetic expression v0*t - 0.5*g*t**2 is computed, resulting in a number, and this number (ormore pre- cisely, thePythonobject representing thenumber)replaces thecally(time) in the callingcodesuchthat thewordprintnowprecedesanumberratherthanafunction call. Pythonproceedswith thenext lineandsetstime toanewvalue. Thenextprint statement triggers a new call to y(t), this time t is set to 0.9, the computations are done line by line in the y function, and the returned result replaces y(time). Insteadofwritingprint y(time),wecouldalternativelyhavestored thereturned result fromthey function inavariable, h = y(time) print h Note thatwhena functioncontainsif-elif-elseconstructions,returnmay bedone fromwithin anyof thebranches. Thismaybe illustratedby the following functioncontaining threereturnstatements: def check_sign(x): if x > 0: return ’x is positive’ elif x < 0: return ’x is negative’ else: return ’x is zero’ Remember that only one of the branches is executed for a single call on check_ sign, so depending on the number x, the returnmay take place from any of the three returnalternatives. Toreturnattheendornot Programmersdisagreewhether it is agood idea tousereturn insidea function where youwant, or if there should only be one singlereturn statement at the endof the function. Theauthorsof thisbookemphasize readablecodeand think thatreturncanbeuseful inbranchesas intheexampleabovewhenthefunction is short. For longer ormore complicated functions, it might be better to have
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
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
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python