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

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

Bild der Seite - 31 -

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

Text der Seite - 31 -

2.2 Functions 31 >=. Given theassignment totemp, youshouldgo througheachbooleanexpression belowanddetermine if it is trueor false. temp = 21 # assign value to a variable temp == 20 # temp equal to 20 temp != 20 # temp not equal to 20 temp < 20 # temp less than 20 temp > 20 # temp greater than 20 temp <= 20 # temp less than or equal to 20 temp >= 20 # temp greater than or equal to 20 2.2 Functions Functions arewidely used in programmingand is a concept that needs to bemas- tered. In the simplest case, a function in a program ismuch like amathematical function: some input number x is transformed to some output number. One ex- ample is the tanh 1.x/ function, called atan in computer code: it takes one real number as input and returns another number. Functions in Python aremore gen- eral andcan take a series ofvariables as input and returnoneormorevariables, or simplynothing.Thepurposeof functions is two-fold: 1. to group statements into separate units of code lines that naturally belong to- gether ( a strategywhichmaydramatically ease the problemsolving process), and/or 2. toparameterize a set of statements such that theycanbewrittenonlyonceand easilybe re-executedwithvariations. Exampleswill be given to illustrate how functions can bewritten in various con- texts. Ifwemodify theprogramball.py fromSect. 1.2 slightly, and include a func- tion,wecould let thisbeanewprogramball_function.pyas def y(t): v0 = 5 # Initial velocity g = 9.81 # Acceleration of gravity return v0*t - 0.5*g*t**2 time = 0.6 # Just pick one point in time print y(time) time = 0.9 # Pick another point in time print y(time) WhenPython reads and interprets this program from the top, it takes the code from the linewith def, to the linewith return, to be the definition of a function with thenamey (notecolonand indentation). The return statementof the function y, i.e. return v0*t - 0.5*g*t**2
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