Page - 60 - in Programming for Computations β Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 60 -
Text of the Page - 60 -
60 3 LoopsandBranching
Whenexecuted, the10 resultsareprintedquitenicelyas
1*5 = 5
2*5 = 10
...
...
With afor loop, however, the very same printout may be producedby just two (!)
linesofcode:
for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]: # Note... for, in and colon
print(β{:d}*5 = {:d}β.format(i, i*5)) # Note indent
With this construction, the loop variablei takes on each of the values 1 to 10, and
foreachvalue, theprint function is called.
Since the numbers 1 to 10 appear in square brackets, they constitute a special
structure called a list. The loop here would work equally well if the brackets had
beendropped,but then thenumberswouldbea tuple:
for i in 1, 2, 3, 4, 5, 6, 7, 8, 9, 10: # no brackets...
print(β{:d}*5 = {:d}β.format(i, i*5))
Bothlistsandtupleshavecertainproperties,whichwewillcomebacktoinSect.5.1.
3.1.2 CharacteristicsofaTypical forLoop
Loop Structure There are different ways to writefor loops, but herein, they are
typicallystructuredas
for loop_variable in some_numbers: # Loop header
<code line 1> # 1st line in loop body
<code line 2> # 2nd line in loop body
...
... # last line in loop body
# First line after the loop
whereloop_variableruns throughthenumbers1 givenbysome_numbers. In the
very first line, called the for loop header, there are two reserved words, for and
in. They are compulsory, as is the colon at the end. Also, the block of code lines
insidea loopmustbe indented.Theseindentedlinesarereferredtoas the loopbody.
Once the indent is reversed,weareoutside(andafter) the loop(ascommentedwith
# First line after the loop, seecode).Onerun-throughof the loopbodyis
called an iteration, i.e., in our example above with the 5 times table, the loop will
do10 iterations.
LoopVariable Thenamepickedfor theloop_variableisup to theprogrammer.
In our times table example, the loop variable i appeared explicitly in the print
command within the loop. Generally, however, the loop variable is not required to
enter in any of the code lines within the loop, it is just available if you need it.
1 In Python, the loop variable doesnot have tobe a number. Forexample, a header likefor name
in [βJohnβ, βPaulβ, βGeorgeβ, βRingoβ]: isfine, causing the loop variable tobe a name.
If you place print(name) inside the loop and run it, you get each of the names printed. In this
book, however, our focus willbe loopswithnumbers.
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