Deutsch English |
YOU LEARN HERE... |
how to store multiple values in a list or tuple and access this data agai |
EXAMPLES |
Instead of storing values in different variables a, b, c individually, Python provides data types that can be used as containers for any number of data. In other programming languages, arrays are often used for this purpose. In contrast to arrays, lists do not have a fixed, predefined length, i.e. their length is automatically adapted to the number of stored elements (dynamic data structure). The elements of a list are written in square brackets, e.g. a list of color names: colors = [“red”, “blue, ‘green’] The values of a tuple are written in round brackets, e.g. x, y coordinates of a point: pt = (10, 20) In contrast to lists, the values of tuples can no longer be changed after the program has been started. Lists and tuples can also be combined, e.g. a list with coordinates of corner points of a figure: corners = [(2, 3), (5, 8), 7, 1)]] Example 1: Accessing the elements of a list
Program:
Example 2: Drawing a polygon with the list of corner points
Program:
Example 3: Creating the elements of a list with mouse clicks
Program:
Example 4: Create points with mouse clicks and draw all connecting lines
Program:
|
REMEMBER YOU... |
You can store any amount of data under a name in a list and access this data with an index. Square brackets are used for the list notation. If you want to run through all elements of the list, the easiest way is to use a for loop for x in list: List is a dynamic data structure; elements can be added or removed during program execution. Values of a tuple are written in round brackets. In contrast to the list, these cannot be changed after the program has started. |
TO SOLVE BY YOURSELF |
1) |
Then output the list sorted by size with the print() command. |
2) |
|
3) |
|
4) |
As in example 5 in the previous section, you must first check whether the left or right mouse button has been pressed. def onMousePressed(x, y): if isLeftMouseButton(): .... if isRightMouseButton(): ..... |
ADDITIONAL INFORMATION: |
The most important operations with lists
|