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

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

Image of the Page - 103 -

Image of the Page - 103 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition

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