.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\python_packages\numpy\demo_copy.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_python_packages_numpy_demo_copy.py: Copy ====== .. GENERATED FROM PYTHON SOURCE LINES 4-10 .. code-block:: Python import numpy as np a = np.array([[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11]]) .. GENERATED FROM PYTHON SOURCE LINES 12-13 No new object is created! .. GENERATED FROM PYTHON SOURCE LINES 13-17 .. code-block:: Python b = a b is a .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 18-19 unique identifier of an object .. GENERATED FROM PYTHON SOURCE LINES 19-25 .. code-block:: Python def f(x): print(id(x)) id(a) .. rst-class:: sphx-glr-script-out .. code-block:: none 3074519997520 .. GENERATED FROM PYTHON SOURCE LINES 26-28 .. code-block:: Python f(a) .. rst-class:: sphx-glr-script-out .. code-block:: none 3074519997520 .. GENERATED FROM PYTHON SOURCE LINES 29-30 **View and shallow copy** .. GENERATED FROM PYTHON SOURCE LINES 30-33 .. code-block:: Python c = a.view() c is a .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 34-35 c is a view of the data owned by a .. GENERATED FROM PYTHON SOURCE LINES 35-37 .. code-block:: Python c.base is a .. rst-class:: sphx-glr-script-out .. code-block:: none True .. GENERATED FROM PYTHON SOURCE LINES 38-40 .. code-block:: Python c.flags.owndata .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 41-42 shape of a doesn't change .. GENERATED FROM PYTHON SOURCE LINES 42-44 .. code-block:: Python c = c.reshape((2, 6)) .. GENERATED FROM PYTHON SOURCE LINES 45-46 a's data changes .. GENERATED FROM PYTHON SOURCE LINES 46-49 .. code-block:: Python c[0, 4] = 1234 a .. rst-class:: sphx-glr-script-out .. code-block:: none array([[ 0, 1, 2, 3], [1234, 5, 6, 7], [ 8, 9, 10, 11]]) .. GENERATED FROM PYTHON SOURCE LINES 50-51 Slicing an array returns a view of it .. GENERATED FROM PYTHON SOURCE LINES 51-54 .. code-block:: Python s = a[:, 1:3] s .. rst-class:: sphx-glr-script-out .. code-block:: none array([[ 1, 2], [ 5, 6], [ 9, 10]]) .. GENERATED FROM PYTHON SOURCE LINES 55-56 **Deep copy** .. GENERATED FROM PYTHON SOURCE LINES 56-59 .. code-block:: Python d = a.copy() d is a .. rst-class:: sphx-glr-script-out .. code-block:: none False .. GENERATED FROM PYTHON SOURCE LINES 60-61 .. code-block:: Python d.base is a .. rst-class:: sphx-glr-script-out .. code-block:: none False .. _sphx_glr_download_11_demos_python_packages_numpy_demo_copy.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_copy.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_copy.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_copy.zip `