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

Page - 28 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 28 -

Image of the Page - 28 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 28 -

28 1 TheFirstFewSteps OneVariableandTextCombined Assumethereisavariablev1 inyourprogram, andthatv1has thevalue10.0, forexample. Ifyouwantyourcodetoprint thevalue ofv1, so that theprintout reads v1 is 10.0 youcanachieve thatwith the followingline in yourprogram: print(’v1 is {}’.format(v1)) This isacall to the functionprintwith“anargumentcomposedof twoparts”.The first part reads v1 is {} enclosed in single quotes (note the single quotes, they must be there!), while the second part is .format(v1). The single quotes of the first partmeans that it is a string (alternatively,doublequotesmaybe used).19 That stringcontainsapairofcurlybrackets{},whichactsasaplaceholder.Thebrackets tell Python where to place the value of, in this case,v1, as specified in the second part.format(v1). So, the formatting creates the stringv1 is 10.0, which then getsprintedbyprint. Several Variables and Text Combined Often, we have more variables to print, andwith twovariablesv1andv2,wecouldprint themby print(’v1 is {}, v2 is {}’.format(v1, v2)) In thiscase, thereare twoplaceholders{}, and—notethefollowing: theorderofv1 andv2given in.format(v1, v2)will dictate theorder in whichvaluesare filled into the preceding string. That is, reading the string from left to right, the value of v1 is placed where the first{} is found, while the value ofv2 is placed where the second{} is located. So, ifv1andv2havevalues10.0and20.0, respectively, theprintoutwill read v1 is 10.0, v2 is 20.0 When printing the values of several variables, it is often natural to use one line for each.Thismaybeachievedbyusing\nas print(’v1 is {} \nv2 is {}’.format(v1, v2)) # \n gives new line whichwill produce v1 is 10.0 v2 is 20.0 We couldprint thevaluesofmorevariablesbyastraight forwardextensionofwhat wasshownhere. Note that, if wehadaccidentallyswitched theorderof thevariablesas print(’v1 is {}, \nv2 is {}’.format(v2, v1)) where.format(v2, v1) is used instead of.format(v1, v2)), we would have gotnoerrormessage, just anerroneousprintoutwhere thevaluesare switched: v1 is 20.0 v2 is 10.0 19 Previously, we have met objectsof type int and float.A string is an object of type str.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python 3.6
Volume
Second Edition
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2020
Language
English
License
CC BY 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
356
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