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

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

Bild der Seite - 103 -

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

Text der Seite - 103 -

5SomeMorePythonEssentials 5.1 ListsandTuples:AlternativestoArrays We have seen that a group of numbersmay be stored in an array that we may treat as a whole, or element by element. In Python, there is another way of organizing data that actually is much used, at least in non-numerical contexts, and that is a constructioncalled list. Some Properties of Lists A list is quite similar to an array in many ways, but thereareprosandcons toconsider.Forexample, thenumberofelements ina list is allowedtochange,whereasarrayshaveafixedlengththatmustbeknownat thetime of memory allocation. Elements in a list can be of different type, so you may mix, e.g., integers, floats and strings, whereas elements in an array must be of the same type. In general, lists provide more flexibility than do arrays. On the other hand, arrays give faster computations than lists, making arrays our prime choice unless the flexibility of lists is needed. Arraysalso require less memory use and there is a lot of ready-madecode forvariousmathematicaloperations.Vectorizationrequires arrays tobe used. Alisthaselements thatwemayuseforcomputations, just likewecanwitharray elements. As with an array, we may find the number of elements in a list with the function len (i.e., we find the “length” of the list), and with the array function fromnumpy,we maycreatean arrayfromanexisting list: In [1]: x = list(range(6, 11, 1)) In [2]: x Out[2]: [6, 7, 8, 9, 10] In [3]: x[0] Out[3]: 6 In [4]: x[4] Out[4]: 10 In [5]: x[0] + x[1] Out[5]: 13 ©The Author(s) 2020 S.Linge, H.P. Langtangen, Programming forComputations -Python, Texts in Computational Science and Engineering 15, https://doi.org/10.1007/978-3-030-16877-3_5 103
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