Python Flashcards

(15 cards)

1
Q

Ist Python eher eine statisch oder dynamisch typisierte Sprache, und was bedeutet das?

A

Dynamisch typisiert (Typbindung erst zur Laufzeit, keine feste Typdeklaration)
Variablen können zur Laufzeit beliebige Typen annehmen
Typfehler erst beim Ausführen sichtbar
Optional: Typannotationen für bessere Tool-Unterstützung

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Wie erstellt man in Python eine Liste und wie greift man auf deren Elemente zu?

A

numbers = [10, 20, 30, 40]
names = [“Alice”, “Bob”, “Charlie”]
mixed = [1, “abc”, True]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Wie kann man in Python schnell alle Elemente einer Liste quadratisch vergrößern

A

for i in range(len(nums)):
nums[i] = nums[i] * nums[i]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Wie schreibt man eine Funktion in Python, die zwei Zahlen addiert und zurückgibt?

A

def add_numbers(a, b):
result = a + b
return result

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Standard

A

So sieht eine Funktion aus:
def addiere(a, b):
return a + b

Schleife: Gehe eine Liste durch
for zahl in [1, 2, 3]:
print(zahl)

Bedingung
if 3 > 2:
print(“Drei ist größer als zwei”)

Eine leere Liste anlegen
liste = []

Ein Wert zur Liste hinzufügen
liste.append(5)

Einen String umkehren
text = “Hallo”
umgekehrt = text[::-1] # Ergibt “ollaH”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Gib die Zahlen von 1 bis 100 aus.
Für Vielfache von 3 „Fizz“, für Vielfache von 5 „Buzz“, für beide „FizzBuzz“.

A

for i in range(1, 101):
if i % 3 == 0 and i % 5 == 0:
print(“FizzBuzz”)
elif i % 3 == 0:
print(“Fizz”)
elif i % 5 == 0:
print(“Buzz”)
else:
print(i)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Maximum in einer Liste finden

A

zahlen = [3, 7, 2, 9, 5]
max_wert = zahlen[0]
for zahl in zahlen:
if zahl > max_wert:
max_wert = zahl
print(max_wert)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Umkehren eines Strings

A

text = “Stackit”
umgekehrt = text[::-1]
print(umgekehrt)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Duplikate aus einer Liste entfernen

A

liste = [1, 2, 2, 3, 4, 4, 5]
ohne_duplikate = list(set(liste))
print(ohne_duplikate)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Zählen, wie oft ein Wort vorkommt

A

text = “Hallo Welt, Hallo Stackit”
wort = “Hallo”
anzahl = text.split().count(wort)
print(anzahl)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Summieren aller Zahlen in einer Liste

A

zahlen = [1, 2, 3, 4, 5]
summe = 0
for zahl in zahlen:
summe += zahl
print(summe)

ODER

print(sum([1, 2, 3, 4, 5]))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Gerade oder ungerade

A

zahl = 7
if zahl % 2 == 0:
print(“Gerade”)
else:
print(“Ungerade”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Prüfen, ob eine Zahl in einer Liste ist

A

zahlen = [1, 2, 3, 4, 5]
x = 3
if x in zahlen:
print(“Gefunden”)
else:
print(“Nicht gefunden”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Wörter zählen in einem Text

A

text = “Stackit ist cool und Stackit ist schnell”
woerter = text.split()
wort_zaehler = {}
for wort in woerter:
if wort in wort_zaehler:
wort_zaehler[wort] += 1
else:
wort_zaehler[wort] = 1
print(wort_zaehler)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly