Module 1 — What is a List?
An ordered collection of values.
Definition
One List stores multiple values under a single variable. The values are in [ … ], separated by commas. A list can hold any type — numbers, strings, even other lists.
Syntax:
farben = ["rot", "blau", "gruen"] zahlen = [1, 2, 3, 4] gemischt = ["Anna", 7, True]
The order is preserved - unlike a set (set). With len(liste) you get the number of elements.
Example 1.1 — Creating and outputting a list
Loading Python…Tests
Module 2 — Index access
Read individual elements — front and back.
Counting from 0
Every element in a list has one Index. The counting starts at
0:
liste[0]— first elementliste[1]— second elementliste[-1]— last elementliste[-2]— penultimate element
If you access an index that doesn't exist, an error occurs IndexError.
Example 2.1 — grab at the front
Loading Python…Tests
Example 2.2 — access from behind
Loading Python…Tests
Module 3 — Modify and append elements
Lists are changeable.
Overwrite element
With an assignment to an index replaced you the element at this point. The list length does not change.
namen = ["Anna", "Ben", "Clara"] namen[1] = "Tom" # Ben → Tom
Example 3.1 — replace second entry
Loading Python…Tests
Attach with append()
The method liste.append(wert) hangs a new value to the end
on. This makes the list one longer.
tiere = ["Hund", "Katze", "Maus"]
tiere.append("Vogel")
Example 3.2 — Append element to end
Loading Python…Tests
Module 4 — Calculating with lists
Link values from the list.
Combine multiple elements
You can access several elements of a list via their indices and e.g. B. sum or compare.
zahlen = [5, 10, 15, 20] summe = zahlen[0] + zahlen[1] + zahlen[2]
Example 4.1 — sum the first three values
Loading Python…Tests
TipRules of Procedure
- You write an empty list as
[]. - With
len(liste)you get the number of elements. - With
liste[-1]you reach always the last element — no matter how long the list is. - Negative indices count from behind:
liste[-2]is the penultimate element.
There are two types of parts in the programming tasks: The Place-exercises
You create the list yourself (you choose the values) - you don't need one here input(). At the Calculator-exercises your program reads the values input() a (known from the “Variables” module); the check feeds in the entries automatically.
Sources & further literature
Where the knowledge imparted here is officially documented.
Official Python documentation
- Python tutorial — 5.1. More on Lists: docs.python.org/3/tutorial/datastructures.html#more-on-lists
- Python Library Reference — Lists: docs.python.org/3/library/stdtypes.html#lists
Tutorials & Manuals
- Real Python — Lists and Tuples in Python: realpython.com/python-lists-tuples
- Theis, Thomas: Getting started with Python. 7. Edition, Rheinwerk Verlag, 2023 — Chapters on lists.
- Klein, Bernd: Introduction to Python 3. 4. Edition, Hanser Verlag, 2021 — “Lists” section.