Python: create and use arrays - how it works
When programming with Python, you quickly stumble across arrays. This Python Guide shows you how to create and use them. Because programming with Python is not that difficult.
Create lists in Python
The classic "arrays" like in Java do not exist in Python. However, you can create so-called lists that work in a similar way. You can also retrofit arrays per module, which we will show you in the second paragraph.
- You can create a list with the command "a = [1, 2, 3]", for example. This list contains three integer values.
- For example, you can use the "print (a [0])" command to output a specific value. In this case, the number 1 would be output because the computer starts counting from 0. You can also change list entries in the same way: "a [0] = 15".
- You can also create multi-dimensional lists. An example of this is the command "b = [[1, 2, 3], [4, 5, 6]]". The command "print (b [1] [0])" would output the number 4.
- You can print out certain areas of the list with "print (a [2: 5])" or redefine several list entries at the same time (command: "a [0: 2] = [0, 1]").
- Lists in cooperation with for loops are very practical. In the "for i in a:" loop, i assumes each value in the list once when iterated (one after the other). It is changed after each run. But be careful: when iterating over a two-dimensional list with a for loop, the iterable is also a list. Here you need a for loop in a for loop.
- In addition to the lists, there is another way to use arrays in Python. To do this, you must first install the appropriate module:
Arrays in Python: First install the NumPy module
Before you start creating the arrays, you must first install the NumPy module. Because this is usually not pre-installed. Here's how it works on Windows:
- Open the command prompt on your PC with the key combination [Windows key] + [R] and the command "CMD".
- Then change to the "Scripts" subfolder of your Python system folder with a change directory command: To do this, enter "cd C: \ Users \ yourusername \ AppData \ Local \ Programs \ Python \ Python36 \ Scripts". However, note that the path to this subfolder may vary depending on the version of the operating system and the Python version. So check beforehand where the folder is.
- Then run the "pip install numpy" command to install NumPy.
Python guide: create arrays
Now you can easily create an array with the NumPy module:
- First you have to import the NumPy module with the command "import numpy as np" (without quotation marks).
- Now you can create a first array with the command "x = np.array ([1, 2, 3, 4])". In this case the array is saved under the variable "x".
- You can easily have this array output for testing with the "print (x)" command.
- You can also create arrays with multiple "dimensions". An example of this is: "y = np.array ([[1, 2, 3], [4, 5, 6], [7, 8, 9]])". If you print this array with "print (y)", you will see that a 3x3 field is printed. In the first line the numbers 1, 2 and 3 are in the second 4, 5 and 6 and finally in the last 7, 8 and 9.
- If you want, you can use the np.arange (10) command to create an array that contains all integers from 0 to 9.
- The command "np.arange (2, 10, dtype = np.float)" in turn creates an array that contains all integers from 2 to 9 as a floating point number ("float"). So this contains the numbers 2.0, 3.0, 4.0, etc.
- For example, you can use the "np.arange (2, 3, 0.1)" command to create an array that contains numbers from 2.0 to 2.9. The values are created in steps of 0.1, i.e. 2.0, 2.1, 2.2, etc.
- Another practical command is "np.linspace (1, 2, 3)". This command outputs an array that contains three numbers between one and two. The special thing here is that the numbers are always the same distance apart. In this case, the numbers 1.0, 1.5 and 2.0 would be output.
- Finally there is the command "np.indices ((4, 3))". This creates an array with many dimensions, each dimension being a variation of the previous one. In this case two dimensions are created, with four rows each. Both dimensions have three numbers per row.
More tips about Python in our CHIP guides
Tip: On our topic page on the Python programming language you will find further tutorials on programming. There we show you, for example, how you can use range and xrange.