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

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

Bild der Seite - 30 -

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

Text der Seite - 30 -

30 1 TheFirstFewSteps In thefirst call toprint, print(’real={:.3f}, integer={:d}, string={:s}’.format(r, i, s)) :.3f tells Python that the floating point numberr is to be written as compactly as possible in decimal notation with three decimals,:d tells Python that the integeri is tobewrittenascompactlyaspossible, and:s tellsPython towrite thestrings. In thesecondcall toprint, print(’real={:9.3e}, integer={:5d}, string={:s}’.format(r, i, s)) theinterpretationis thesame,exceptthatrandinowshouldbeformattedaccording to:9.3eand:5d, respectively.Forr, thismeans that itsfloat valuewill bewritten in scientific notation (the e) with 3 decimals, in a field that has a width of 9 characters.As fori, its integervaluewillbewritten inafieldofwidth5characters. Other ways of formatting the numbers would also have been possible.20 For example, specifying the printing ofr rather as:9.3f (i.e.,f instead ofe), would givedecimalnotation,whilewith:g,Pythonitselfwouldchoosebetweenscientific anddecimalnotation,automaticallychoosingtheoneresulting in themostcompact output. Typically, scientific notation is appropriate for very small and very large numbersanddecimalnotationfor the intermediate range. Printingwithold string formatting Thereisanotherolderstringformattingthat,whenusedwithprint,gives the same printout as the string format method. Since you will meet it frequently in Python programs found elsewhere, you better know about it. With this formatting, thecalls toprint in thepreviousexamplewould rather read print(’real=%.3f, integer=%d, string=%s’ % (r, i, s)) print(’real=%9.3e, integer=%5d, string=%s’ % (r, i, s)) As you might guess, the overall “structure” of the argument toprint is the sameaswith thestringformatmethod,but,essentially,% isused insteadof{} (with: inside)and.format. An Example: Printing Nicely Aligned Columns A typical example of when formattedprintingisrequired,ariseswhennicelyalignedcolumnsofnumbersareto be printed. Supposewe want to print a columnof t values togetherwith associated functionvaluesg(t)= t sin(t) in a secondcolumn. We could achieve this in the following way (note that, repeating the same set of statements multiple times, like we do in the following code, is not good programming practice—one should use a loop. You will learn about loops in Chap.3.) from math import sin t0 = 2 20 https://docs.python.org/3/library/string.html#format-string-syntax.
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