Page - 30 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 30 -
Text of the Page - 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.
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