{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Arrays in python\n", "## some examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In python coding, either in a script or in a notebook, arrays are very frequent tools to store data and carry out mathematical operations. Arrays in numpy can be used to create vectors and matrices. They can be of various dimensions, 1D, 2D, 3D etc... \n", "In this notebook, we will cover different aspects linked to arrays in Numpy. Specifically we will investigate how to combine arrays with `for` loops and how to carry out array slicing." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We start by importing the necessary module for this notebook." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "import numpy as np" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first example is based on a simple vector (1D array). This array contains 5 values linearly distributed in between 1.0 and 5.0:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = np.array([1.0, 2.0, 3.0, 4.0, 5.0])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we print the content of x we see that we get the 5 values we wanted:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(x)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When we use an array we also have the possibility to access specific entries, here rows since we deal with a vector.\n", "To access the first and second value in the array we simply request the row numbers 0 and 1 via:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(x[0])\n", "print(x[1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here we observe a specific part of python, the index of an array starts from 0 and not 1. Consequently if we want to access the last value of our array, in which we have 5 entries, requesting `x[5]` will lead to an error:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(x[5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To access the last value of an array, we will need to request `x[4]`. The following cell prints this value and we do see that `x[4] = 5.0` as expected." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(x[4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In python, we can access the values in the array \"backward\", meaning here relative to the last value in the array. To do this we need to specify `-i` as the index of the array.\n", "The next cell illustrates this technique:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(x[-1])\n", "print(x[-2])\n", "print(x[-3])\n", "print(x[-4])\n", "print(x[-5])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "By accessing the row -1, we obtain the last value of our array. On the contrary to what we observed earlier, here accessing the row -5 is allowed and returns the first value of our array. When counting backward, from end to start, python uses a \"regular\" indexing." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in range(0,5):\n", " print(x[i],i)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1. 2. 3. 4. 5.]\n" ] } ], "source": [ "print(x[0:])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1. 2. 3. 4. 5.]\n" ] } ], "source": [ "print(x[0:5])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "A = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(A)\n", "print(np.shape(A))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.0" } }, "nbformat": 4, "nbformat_minor": 4 }