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

Seite - 41 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Bild der Seite - 41 -

Bild der Seite - 41 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python

Text der Seite - 41 -

2.5 ListsandTuples–Alternatives toArrays 41 Ifyou typeand run thisprogramyoushouldget y=0 at 0.917417417417 Thenew thinghere is thewhile looponly. The loop (note colonand indentation) will run as long as thebooleanexpressiony[i] > 0 evaluates toTrue. Note that theprogrammer introducedavariable (the loop index)by thenamei, initialized it (i = 0)before the loop,andupdated it (i += 1) in the loop. Soforeach iteration, i isexplicitly increasedby1,allowingacheckofsuccessiveelementsin thearrayy. Compared to afor loop, the programmerdoes not have to specify the number of iterationswhencodingawhile loop. It simplyrunsuntil thebooleanexpression becomesFalse. Thus,a loopindex(aswehave inafor loop) isnot required. Fur- thermore, if a loop index is used inawhile loop, it is not increasedautomatically; itmust be done explicitly by the programmer. Of course, just as infor loops and ifblocks, theremight be (arbitrarily)manycode lines in awhile loop. Anyfor loopmayalsobe implementedas awhile loop,butwhile loopsaremoregeneral sonot all of themcanbeexpressedasafor loop. Aproblem to be aware of, iswhat is usually referred to as an infinite loop. In those unintentional (erroneous) cases, the boolean expression of the while test never evaluates to False, and the program can not escape the loop. This is one of themost frequent errorsyouwill experienceas abeginningprogrammer. If you accidentallyenteran infinite loopandtheprogramjusthangsforever,pressCtrl+c to stop theprogram. 2.5 ListsandTuples–AlternativestoArrays Wehave seen that a groupofnumbersmaybe stored in anarray thatwemay treat as a whole, or element by element. In Python, there is another way of organiz- ing data that actually ismuch used, at least in non-numerical contexts, and that is aconstructioncalled list. A list is quite similar to an array in manyways, but there are pros and cons to consider. For example, the number of elements in a list is allowed to change, whereas arrayshaveafixed length thatmustbeknownat the timeofmemoryallo- cation. Elements in a list can beof different type, i.e youmaymix integers, floats andstrings,whereaselements inanarraymustbeof thesametype. Ingeneral, lists providemoreflexibility thandoarrays. On theother hand, arraysgive faster com- putations than lists,makingarrays theprimechoiceunless theflexibility of lists is needed. Arraysalso require lessmemoryuseand there is a lot of ready-madecode forvariousmathematicaloperations.Vectorization requiresarrays tobeused. Therange()functionthatweusedaboveinourfor loopactually returnsa list. If you for example write range(5) at the prompt in ipython, you get [0, 1, 2, 3, 4] in return, i.e., a list with 5 numbers. In a for loop, the linefor i in range[5]makes i take on each of the numbers 0;1;2;3;4 in turn, as we saw above. Writing, e.g.,x = range(5), gives a list by the namex, containing those fivenumbers. Thesenumbersmaynowbeaccessed (e.g., asx[2],whichcontains thenumber2)andused incomputations just aswesawforarrayelements. Aswith arrays, indices run from0ton 1,whenn is thenumberofelements ina list. You mayconverta list to anarraybyx = array(L).
zurück zum  Buch Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python"
Programming for Computations – Python A Gentle Introduction to Numerical Simulations with Python
Titel
Programming for Computations – Python
Untertitel
A Gentle Introduction to Numerical Simulations with Python
Autoren
Svein Linge
Hans Petter Langtangen
Verlag
Springer Open
Datum
2016
Sprache
englisch
Lizenz
CC BY-NC 4.0
ISBN
978-3-319-32428-9
Abmessungen
17.8 x 25.4 cm
Seiten
248
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