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 - 51 -
  • User
  • Version
    • full version
    • text only version
  • Language
    • Deutsch - German
    • English

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

Image of the Page - 51 -

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

Text of the Page - 51 -

2.3 NumericalPythonArrays 51 In [12]: y[0] = 10.0 In [13]: y Out[13]: array([ 10., 1., 2.]) # ...as expected In [14]: x Out[14]: array([ 10., 1., 2.]) # ...x has changed too! Intuitively, it mayseem verystrange that changingan element inycausesa similar changeinx!Thethingis,however,thatourassignmenty = xdoesnotmakeacopy of thexarray.Rather,Pythoncreatesanother reference,namedy, to thesamearray object thatx refers to. That is, there is one array object with two names (x andy). Therefore,changingeitherxory, simultaneouslychanges“theother”(notethat this behaviordiffers from what we found in Sect.2.2.3 for single integer,float or string objects). To really get a copy that is decoupled from the original array, you may use the copy functionfromnumpy, In [15]: from numpy import copy In [16]: x = linspace(0, 2, 3) # x becomes array([ 0., 1., 2.]) In [17]: y = copy(x) In [18]: y Out[18]: array([ 0., 1., 2.]) In [19]: y[0] = 10.0 In [20]: y Out[20]: array([ 10., 1., 2.]) # ...changed In [21]: x Out[21]: array([ 0., 1., 2.]) # ...unchanged 2.3.5 SlicinganArray By use of a colon, you may work with a slice of an array. For example, by writing x[i:j], we address all elements from index i (inclusive) to j (exclusive) in an arrayx.An interactivesession illustrates this, In [1]: from numpy import linspace In [2]: x = linspace(11, 16, 6) In [3]: x Out[3]: array([ 11., 12., 13., 14., 15., 16.]) In [4]: y = x[1:5] In [5]: y Out[5]: array([ 12., 13., 14., 15.])
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