Web-Books
im Austria-Forum
Austria-Forum
Web-Books
Informatik
Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition
Seite - 60 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

Seite - 60 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Bild der Seite - 60 -

Bild der Seite - 60 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition

Text der Seite - 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.
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Band Second Edition"
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
Web-Books
Bibliothek
Datenschutz
Impressum
Austria-Forum
Austria-Forum
Web-Books
Programming for Computations – Python