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 - 61 -
  • Benutzer
  • Version
    • Vollversion
    • Textversion
  • Sprache
    • Deutsch
    • English - Englisch

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

Bild der Seite - 61 -

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

Text der Seite - 61 -

3.1 TheforLoop 61 This means that if we had, e.g., switched the print command inside our loop with print(’Hello!’) (i.e., so that i does not appear explicitly within the loop), i wouldstill run throughthenumbers1 to10asbefore,butHello!wouldbeprinted 10 times instead. The loopvariablei takeson the values1 to 10 in theorder listed, andanyorder wouldbeacceptable to Python.Thus, if we (for some reason)would like to reverse theorderof theprintouts,wecouldsimplyreversethe listofnumbers,writing[10, 9, 8, 7, 6, 5, 4, 3, 2, 1] instead. It shouldbenoted that the loopvariable isnot restricted to runover integers.Our nextexample includes loopingalsooverfloatingpointvalues. IndentationandNestedLoops Inoursimple times tableexampleabove, theprint command inside the loop was indented 4 spaces, which is in accordance with the official style guideofPython.2 Strictly speaking, the style guide recommends an indent of 4 spaces per indentation level. What this means, should become clear if we demonstrate how a for loop may appear within another for loop, i.e., if we show an arrangement withnested loops. for i in [1, 2, 3]: # First indentation level (4 spaces) print(’i = {:d}’.format(i)) for j in [4.0, 5.0, 6.0]: # Second indentation level (4+4 spaces) print(’ j = {:.1f}’.format(j)) # First line AFTER loop over j # First line AFTER loop over i The meaning of indentation levels should be clear from the comments (see code), and it is straight forward to use more nested loops than shown here (see, e.g., Exercise 5.7). Note that, together with the colon, indenting is part of the syntax also for other basic programmingconstructions in Python (e.g., inif-elif-else constructionsand functions). Whenexecuting thenested loopconstruction,weget thisprintout: i = 1 j = 4.0 j = 5.0 j = 6.0 i = 2 j = 4.0 j = 5.0 j = 6.0 i = 3 j = 4.0 j = 5.0 j = 6.0 From the printout, we may infer how the execution proceeds. For each value of i, the loop over j runs through all its values before i is updated (or the loop is terminated). To test your understanding of nested loops, you are recommended to doExercise5.1. 2 https://www.python.org/dev/peps/pep-0008/.
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