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 - 28 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 28 -

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

Text der Seite - 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.
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