Page - 29 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 29 -
Text of the Page - 29 -
1.6 Plotting,PrintingandInputData 29
So, make sure the order of the arguments is correct. An alternative is to name the
arguments.
Naming the Arguments If we name the arguments (v1 andv2 are arguments to
format),we get thecorrectprintoutwhetherwe callprintas
print(’v1 is {v1}, \nv2 is {v2}’.format(v1=v1, v2=v2))
or, switching theorder,
print(’v1 is {v1}, \nv2 is {v2}’.format(v2=v2, v1=v1))
Note that the names introduced do not have to be the same as the variable names,
i.e., “any” names would do. Thus, if we (for the sake of demonstration) rather use
the namesa andb, anyof the followingcalls toprintwould work just asfine (try
it!):
print(’v1 is {a}, \nv2 is {b}’.format(a=v1, b=v2))
or
print(’v1 is {a}, \nv2 is {b}’.format(b=v2, a=v1))
Controlling the printout like we have demonstrated this far, may be sufficient
in many cases. However, as we will see next, even more printing details can be
controlled.
Formatting More Details Often, we want to control how numbers are formatted.
For example, we may want to write 1/3 as0.33 or 3.3333e-01 (3.3333 ·10−1),
andas thefollowingexamplewilldemonstrate,suchdetailsmayindeedbespecified
in the argument to print. The essential new thing then, is that we supply the
placeholders{}withsomeextra informationin between thebrackets.
Suppose we have a real number 12.89643, an integer 42, and a text ’some
message’ thatwe want to writeout in the following twodifferentways:
real=12.896, integer=42, string=some message
real=1.290e+01, integer= 42, string=some message
The real number is first to be written in decimal notation with three decimals, as
12.896,butafterwardsinscientificnotationas1.290e+01.Theintegershouldfirst
be written as compactly as possible, while the second time,42 should be placed in
afivecharacterwide textfield.
The followingprogram,formatted_print.py,produces the requestedoutput:
r = 12.89643 # real number
i = 42 # integer
s = ’some message’ # string (equivalent: s = "some message")
print(’real={:.3f}, integer={:d}, string={:s}’.format(r, i, s))
print(’real={:9.3e}, integer={:5d}, string={:s}’.format(r, i, s))
Here,eachplaceholdercarriesaspecificationofwhatobject type thatwillenterin
thecorrespondingplace,withf symbolizingafloat (realnumber),d symbolizing
anint (integer), ands symbolizingastr (string). Also, there is a specification of
how each number is to be printed. Note the colon within the brackets, it must be
there!
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