Web-Books
in the Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Page - 64 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

Page - 64 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Image of the Page - 64 -

Image of the Page - 64 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

Text of the Page - 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.
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition"
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
Web-Books
Library
Privacy
Imprint
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python