Page - 96 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 96 -
Text of the Page - 96 -
96 4 FunctionsandtheWritingofCode
will be regarded as the final version (in general, however, programs are typically
changedandimprovedagainandagain,makingithardtoreach the“final”version!).
DevelopingaComputationalPlan
To write a program, you need to plan what that program should do, and a
good plan requires a thorough understanding of the addressed problem. One
fundamentally important thing with the step-wise strategy, is that it invites
you to think through your computational problem very carefully: What bits
and pieces, or “sub-problems”, make up the whole task? Should the “sub-
problems” be solved in any particular order, i.e., do parts of the problem
depend on results from other parts? What is the best way to compute each
of the “sub-problems”?
This kind of thinking, which combines favorably with discussions among
students/colleagues, often pays off in terms of a much deeper understanding
of the problemat hand.Goodsolutionsoften requiresuchanunderstanding.
CompoundStatements
Theconstructionsmet in thischapter,and thepreviouschapter,arecharacter-
ized by a grouping of statements that generally span several lines (although
it is possible to write simpler cases on a single line, when statements are
separated with a semi-colon). Such constructions are often referred to as
compoundstatements, havingheaders (endingwitha colon)andbodies (with
indentedstatements).a
Interactive handling of compound statements is straight forward. For
example,afor loopmaybewritten (andexecuted) like
In [1]: for i in [1, 2, 3]: # write header, press enter
...: print(i) # indent comes automatically
...: # press only enter, i.e., finished
1
2
3
When the header has been typed in and we press enter, we are automatically
given the indent on the next line. We can then proceed directly by writing
print(i) and press enter again. We then want to finish our loop, which is
understoodwhenwesimplypressenter,writingnothingelse.
a https://docs.python.org/3/reference/compound_stmts.html.
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