Skip to content

Commit 555b5a1

Browse files
author
dodo
committed
Merge branch 'master' of github.com:pythonfoo/pythonfooLite
2 parents 802943e + e13e478 commit 555b5a1

12 files changed

Lines changed: 122 additions & 15 deletions

File tree

Level_01/integer.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
z = -3
55
basis = 2
66
exponent = 5
7+
print(x, "in binär:", bin(x), "in hexadezimal:", hex(x))
78

89
# Daten andere Typen in Integer umwandeln
910
# Achtung: Dies kann fehlschlagen!
1011
y = int("6")
12+
y = int("110", 2) # 110 ist 6 in binär.
1113

1214
# Operatoren auf Integer anwenden:
1315
# (für mehr Informationen siehe die Wiki-Seite zu Operatoren)

Level_01/strings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,6 @@
3838

3939
# Den ersten Buchstaben groß:
4040
print(h.capitalize()) # OUT: Hamster
41+
42+
# Länge
43+
print(len(h)) # OUT: 7

Level_03/dictionaries.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
# Ein Dictionary wird über geschweifte Klammern
1111
# definiert:
1212
dictionary = {"Eins": "one", "Zwei": "two"} # type: dict
13+
dictionary = dict([("Eins", "one"), ("Zwei", "two")])
1314
print(dictionary)
1415

1516
# Auf einen value wird mit Hilfe des keys zu-
1617
# gegriffen:
1718
print(dictionary["Eins"])
19+
# dictionary["nicht da"]: schlägt fehl
20+
dictionary.get("nicht da")
1821

1922
# Ein neues Key-Value-Paar wird erstellt,
2023
# indem auf ein nicht-existierenden value zu-
@@ -23,6 +26,8 @@
2326
print(dictionary)
2427
# Als keys geeignet sind zum Beispiel: Integer, Strings, Tupel, Boolean
2528

29+
# Einträge löschen
30+
del dictionary["Wasser"]
2631

2732
# Mit len() lässt sich die Länge ausgeben:
2833
print(len(dictionary))

Level_03/for.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
print(key)
2020
print(Dictionary[key])
2121

22+
# besser:
23+
for key, value in Dictionary.items():
24+
print(key, value)
2225

2326
# Ebenso kann ein Tupel oder eine Liste durchlaufen werden
2427

Level_04/dateien.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121

2222
# eine Datei auslesen
2323

24+
# schneller: print(Path("loremipsum.txt").read_text())
2425
lorem_ipsum = Path("loremipsum.txt").open("r")
2526
print(lorem_ipsum.read())
2627
lorem_ipsum.close()
2728

2829
# eine Datei schreiben
2930

31+
# schneller: (test_dir / Path("test.txt")).write_text("total toller Text")
3032
test = (test_dir / Path("test.txt")).open("w")
3133
test.write("total toller Text") # type: int
3234
# OUT: 17

Level_04/loremipsum.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#!/usr/bin/env python3
22

3-
lorem_ipsum = open("loremipsum.txt", "r")
4-
text = lorem_ipsum.read() # type: str
5-
lorem_ipsum.close()
3+
from pathlib import Path
4+
5+
lorem_ipsum = Path("loremipsum.txt")
6+
text = lorem_ipsum.read_text() # type: str
67

78
orig = text
89
text = text.upper()
@@ -14,6 +15,5 @@
1415
print("****************")
1516
print(text)
1617

17-
lorem_ipsvm = open("loremipsvm.txt", "w")
18-
lorem_ipsvm.write(text)
19-
lorem_ipsvm.close()
18+
lorem_ipsvm = Path("loremipsvm.txt")
19+
lorem_ipsvm.write_text(text)

Level_04/quine.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22

3-
we = open(__file__, "r")
4-
print(we.read())
5-
we.close()
3+
from pathlib import Path
4+
5+
we = Path(__file__)
6+
print(we.read_text())

Level_05/strings_erweitert.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
# String einzubauen
66
print("Tab:\t#")
77

8+
# Zeichen über ihren Namen einbinden:
9+
print("Wal: \N{WHALE}")
10+
811
# Das Zeichen hinter einem \ wird entweder Steuerzeichen interpretiert oder ignoriert
912
print("\aabc")
1013

Level_06/calc.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ def op_root(a: int, b: int) -> float:
3535

3636

3737
parser = ArgumentParser(description=__doc__)
38-
subparsers = parser.add_subparsers(help="die auszuführende Rechenoperation")
38+
subparsers = parser.add_subparsers(
39+
dest="command",
40+
help="die auszuführende Rechenoperation"
41+
)
42+
subparsers.required = True
3943

4044
for function in (
4145
op_add,
@@ -57,8 +61,5 @@ def op_root(a: int, b: int) -> float:
5761
# Verarbeiten der Argumente
5862
args = parser.parse_args()
5963
# die tatsächliche Funktion aufrufen
60-
if args.func:
61-
res = args.func(args.a, args.b)
62-
print(res)
63-
else:
64-
print("Bitte Anweisung angeben.")
64+
res = args.func(args.a, args.b)
65+
print(res)

Level_10/gravatar/fallback.jpg

1.29 KB
Loading

0 commit comments

Comments
 (0)