Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Page - 31 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

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

Image of the Page - 31 -

Image of the Page - 31 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text of the Page - 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
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python