.. 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_indexing.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_indexing.py: Indexing, Slicing and Iterating ================================== .. GENERATED FROM PYTHON SOURCE LINES 4-12 .. code-block:: Python import numpy as np # **One dimensional** a = np.arange(10) ** 3 a[2] .. rst-class:: sphx-glr-script-out .. code-block:: none 8 .. GENERATED FROM PYTHON SOURCE LINES 14-16 .. code-block:: Python a[2:5] .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 8, 27, 64], dtype=int32) .. GENERATED FROM PYTHON SOURCE LINES 17-19 .. code-block:: Python a[:6:2] = 1000 .. GENERATED FROM PYTHON SOURCE LINES 20-21 reverse array .. GENERATED FROM PYTHON SOURCE LINES 21-24 .. code-block:: Python a[::-1] .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 729, 512, 343, 216, 125, 1000, 27, 1000, 1, 1000], dtype=int32) .. GENERATED FROM PYTHON SOURCE LINES 25-26 **Multi dimensional** .. GENERATED FROM PYTHON SOURCE LINES 26-33 .. code-block:: Python def f(x, y): return 10 * x + y b = np.fromfunction(f, (5, 4), dtype=int) b .. rst-class:: sphx-glr-script-out .. code-block:: none array([[ 0, 1, 2, 3], [10, 11, 12, 13], [20, 21, 22, 23], [30, 31, 32, 33], [40, 41, 42, 43]]) .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python b[2, 3] .. rst-class:: sphx-glr-script-out .. code-block:: none 23 .. GENERATED FROM PYTHON SOURCE LINES 37-39 .. code-block:: Python b[0:5, 1] .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 1, 11, 21, 31, 41]) .. GENERATED FROM PYTHON SOURCE LINES 40-42 .. code-block:: Python b[:, 1] .. rst-class:: sphx-glr-script-out .. code-block:: none array([ 1, 11, 21, 31, 41]) .. GENERATED FROM PYTHON SOURCE LINES 43-45 .. code-block:: Python b[1:3, :] .. rst-class:: sphx-glr-script-out .. code-block:: none array([[10, 11, 12, 13], [20, 21, 22, 23]]) .. GENERATED FROM PYTHON SOURCE LINES 46-47 .. code-block:: Python b[-1] .. rst-class:: sphx-glr-script-out .. code-block:: none array([40, 41, 42, 43]) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.010 seconds) .. _sphx_glr_download_11_demos_python_packages_numpy_demo_indexing.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_indexing.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_indexing.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_indexing.zip `