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

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

Bild der Seite - 104 -

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

Text der Seite - 104 -

104 5 SomeMorePythonEssentials In [6]: import numpy as np In [7]: y = np.array(x) # create array y In [8]: y Out[8]: array([ 6, 7, 8, 9, 10]) In [9]: x[0] = -1 In [10]: x Out[10]: [-1, 7, 8, 9, 10] # x is changed In [11]: y Out[11]: array([ 6, 7, 8, 9, 10]) # y is not changed In [12]: len(x) Out[12]: 5 In [13]: len(y) Out[13]: 5 Alist mayalso becreatedbysimplywriting,e.g., x = [’hello’, 4, 3.14, 6] givinga listwherex[0]contains thestringhello,x[1]contains the integer4, etc. We mayaddanddeleteelementsanywhere in a list: x = [’hello’, 4, 3.14, 6] x.insert(0, -2) # x then becomes [-2, ’hello’, 4, 3.14, 6] del x[3] # x then becomes [-2, ’hello’, 4, 6] x.append(3.14) # x then becomes [-2, ’hello’, 4, 6, 3.14] Notethewaysofwriting thedifferentoperationshere.Usingappend()willalways increase the list at the end. If you like, you may create an empty list as x = [] before you enter a loop which appends element by element. Note that there are manymoreoperationson lists possible thanshownhere. List and for Loops Previously, we saw how a for loop may run over array elements.When we want to do the same with a list in Python,we maydo it simply like: x = [’hello’, 4, 3.14, 6] print(’The elements of the list x:\n’) for e in x: print(e) We observe that e runs over the elements of x directly, avoiding the need for indexing. Be aware, however, that when loops are written like this, you can not change any element inxby “changing”e. That is, writinge += 2will not change anything in x, since e can only be used to read (as opposed to overwrite) the list elements.Running thecodegives the output
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