Seite - 37 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python
Bild der Seite - 37 -
Text der Seite - 37 -
2.3 ForLoops 37
Wenotice that there is someoverhead in functioncalls. The impact of theover-
headreducesquicklywiththeamountofcomputationalworkinsidethefunction.
2.3 ForLoops
Manycomputationsare repetitive bynature andprogramming languageshavecer-
tain loop structures to dealwith this. Herewewill presentwhat is referred to as
a for loop(anotherkindof loopisawhile loop, tobepresentedafterwards).Assume
youwant to calculate the square of each integer from3 to 7. This could be done
with the following two-lineprogram.
for i in [3, 4, 5, 6, 7]:
print i**2
Note thecolonand indentationagain!
What happenswhen Python interprets your code here? First of all, the word
for is a reservedwordsignalling toPython that afor loop iswanted. Python then
sticks to the rules covering such constructions andunderstands that, in thepresent
example, the loopshould run5successive times (i.e., 5 iterations shouldbedone),
letting thevariablei takeon thenumbers3;4;5;6;7 in turn.Duringeach iteration,
the statement inside the loop(i.e. print i**2) is carriedout.After each iteration,
i is automatically (behind the scene) updated. When the last number is reached,
the last iteration isperformedandthe loopisfinished.Whenexecuted, theprogram
will therefore print out9;16;25;36 and 49. The variablei is often referred to as
a loop index, and itsname(herei) is achoiceof theprogrammer.
Note that, had there been several statementswithin the loop, theywould all be
executedwith the same value ofi (beforei changed in the next iteration). Make
sureyouunderstandhowprogramexecutionflowshere, it is important.
In Python, integer values specified for the loop variable are often produced by
the built-in functionrange. The functionrangemaybe called in differentways,
that either explicitly, or implicitly, specify the start, stop and step (i.e., change) of
the loopvariable.Generally, a call torange reads
range(start, stop, step)
This call makes range return the integers from (and including) start, up to
(but excluding!) stop, in steps ofstep. Note here thatstop-1 is the last integer
included.Withrange, thepreviousexamplewould rather read
for i in range(3, 8, 1):
print i**2
By default, if range is calledwith only two parameters, these are taken to be
startandstop, inwhichcaseastepof1 isunderstood. Ifonlyasingleparameter
is used in thecall torange, this parameter is taken tobestop. Thedefault stepof
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2016
- Sprache
- englisch
- Lizenz
- CC BY-NC 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 248
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik