Python_Crashkurs/README.md
2026-03-06 04:44:19 +01:00

125 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Python_Crashkurs
## Aufgabe 1 Truthiness von Listen
Erstelle ein Python-Programm, das mit der Truthiness von Listen arbeitet.
### Schritt 1
Erstelle eine Liste:
```python
numbers = [0, 1, 2, 3, 4, 5]
```
### Schritt 2
Prüfe mit einer `if`-Abfrage, ob die Liste **nicht leer** ist.
Wenn sie Elemente enthält, soll folgendes ausgegeben werden:
```
List contains elements
```
### Schritt 3
Setze anschließend die Liste auf eine leere Liste:
```python
numbers = []
```
### Schritt 4
Prüfe erneut, ob die Liste leer ist.
Wenn sie leer ist, soll folgendes ausgegeben werden:
```
List is empty
```
## Zusatzfrage
Was gibt folgendes Programm aus?
```python
x = []
if x:
print("A")
else:
print("B")
```
Begründe kurz warum.
## Aufgabe 2 Negative Indizes
Python erlaubt es, auf Elemente von Listen **vom Ende der Liste aus** zuzugreifen. Dafür werden **negative Indizes** verwendet.
### Beispiel
```python
numbers = [10, 20, 30, 40, 50]
print(numbers[-1])
```
Ausgabe:
```
50
```
Der Index `-1` greift auf das **letzte Element** der Liste zu.
`-2` auf das **vorletzte**, usw.
### Schritt 1
Erstelle folgende Liste:
```python
numbers = [10, 20, 30, 40, 50]
```
### Schritt 2
Gib das **letzte Element** der Liste aus, indem du einen **negativen Index** verwendest.
Die Ausgabe soll sein:
```
50
```
### Schritt 3
Gib zusätzlich das **vorletzte Element** der Liste aus.
Die Ausgabe soll sein:
```
40
```
### Schritt 4
Gib zusätzlich das **dritte Element von hinten** aus.
Die Ausgabe soll sein:
```
30
```
## Zusatzfrage
Was gibt folgendes Programm aus?
```python
numbers = [10, 20, 30, 40, 50]
print(numbers[-3])
```
Begründe kurz warum.
## Zusatzfrage 2
Was passiert hier?
```python
numbers = [10, 20, 30, 40, 50]
print(numbers[-6])
```
- Wird ein Wert ausgegeben?
- Oder entsteht ein Fehler?
Begründe kurz warum.