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

Page - 37 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Image of the Page - 37 -

Image of the Page - 37 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text of the Page - 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
back to the  book Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Title
Programming for Computations – Python
Subtitle
A Gentle Introduction to Numerical Simulations with Python
Authors
Svein Linge
Hans Petter Langtangen
Publisher
Springer Open
Date
2016
Language
English
License
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Size
17.8 x 25.4 cm
Pages
248
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