Seite - 64 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Bild der Seite - 64 -
Text der Seite - 64 -
64 3 LoopsandBranching
In [4]: list(x) # convert to list
Out[4]: [0, 1, 2, 3, 4]
AGeneralCall torange Witha header like
for loop_variable in range(start, stop, step):
and a step> 0,loop_variablewill run through the numbersstart, start +
1*step, start + 2*step, ...., start + n*step,wherestart+n∗step<
stop<= start+ (n+1)∗ step. So, the final number is as close as we can get to
the specifiedstopwithout equalling, or passing, it. For a negativestep (step< 0,
example given below), the same thing applies, meaning that the final number can
not equal, or be more negative, than the argumentstop. Note that an integerstep
different from1and−1 isperfectly legal.
Different Ways of Calling range The functionrange is most often used in for
loopstoproducearequiredsequenceof integers,but thefunctionisnot restricted to
for loops only, of course. It may be called in different ways, e.g., utilizing default
values.Someexamplesare
In [1]: list(range(1, 11, 1))
Out[1]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [2]: list(range(1, 11)) # step not given, default 1
Out[2]: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [3]: list(range(11)) # start not given either, default 0
Out[3]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
In [4]: list(range(1, -11, -1))
Out[4]: [1, 0, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10]
Whencallingrange, there isnoargumentspecifyinghowmanynumbersshouldbe
produced, like we saw with thelinspace function ofnumpy. Withrange, this is
impliedby the functionargumentsstart,stopandstep.
Computer Memory Considerations Note that range does not return all the
requested numbers at once. Rather, the call to rangewill cause the numbers to
be provided one by one during loop execution, giving one number per iteration.
Thisway, simultaneousstoringofall (loopvariable)numbers incomputermemory
is avoided, which may be very important when there is a large number of loop
iterations to make.
3.1.5 Usingbreakandcontinue
It is possible to break out of a loop, i.e., to jump directly to the first code line after
the loop, by use of a break statement. This might be desirable, for example, if a
certainconditionismetduringloopexecution,whichmakestheremainingiterations
redundant.
With loops, it may also become relevant to skip any remaining statements of
an ongoing iteration, and rather proceed directly with the next iteration. That is,
contrary to the “loop stop” caused by abreak statement, the loop continues to run
afteracontinuestatement,unless the endof the loophasbeen reached.
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
- Titel
- Programming for Computations – Python
- Untertitel
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Band
- Second Edition
- Autoren
- Svein Linge
- Hans Petter Langtangen
- Verlag
- Springer Open
- Datum
- 2020
- Sprache
- englisch
- Lizenz
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Abmessungen
- 17.8 x 25.4 cm
- Seiten
- 356
- Schlagwörter
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Kategorie
- Informatik