Skip to content

Commit cdf67f0

Browse files
committed
feat: Reorder content of cheatsheet
1 parent 3a0de63 commit cdf67f0

1 file changed

Lines changed: 141 additions & 141 deletions

File tree

900-Cheatsheet.md

Lines changed: 141 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ numbering:
1111
Python-Code muss korrekt eingerückt sein. Es sollten pro Einrückungsebene genau 4 Leerzeichen genutzt werden.
1212
```
1313

14+
## Variablen
15+
16+
```python
17+
name = "Alice"
18+
alter = 25
19+
ist_student = True
20+
gehalt = 3500.50
21+
22+
# Mehrere Zuweisungen
23+
x, y, z = 1, 2, 3
24+
a = b = c = 0
25+
26+
_a = "Unterstrich am Anfang"
27+
a3 = "Zahlen sind möglich, wenn nicht erstes Zeichen im Namen"
28+
```
29+
1430
## Zahlen
1531

1632
### `int()`
@@ -33,19 +49,6 @@ von_string = float("2.5")
3349
von_int = float(42) # Ergebnis: 42.0
3450
```
3551

36-
## Arithmetik
37-
38-
```python
39-
addition = 5 + 3 # 8
40-
subtraktion = 10 - 4 # 6
41-
multiplikation = 6 * 7 # 42
42-
multiplikation_float = 6.0*7 # 42.0
43-
division = 15 / 3 # 5.0
44-
ganzzahldivision = 17 // 5 # 3
45-
modulo = 17 % 5 # 2
46-
potenz = 2 ** 3 # 8
47-
```
48-
4952
## Zeichenketten (`str()`)
5053

5154
```python
@@ -65,67 +68,33 @@ neue_zeile = "Zeile 1\nZeile 2"
6568
tabulator = "Text\teingerückt\n\tauch eingerückt"
6669
```
6770

68-
## Variablen
69-
70-
```python
71-
name = "Alice"
72-
alter = 25
73-
ist_student = True
74-
gehalt = 3500.50
75-
76-
# Mehrere Zuweisungen
77-
x, y, z = 1, 2, 3
78-
a = b = c = 0
79-
80-
_a = "Unterstrich am Anfang"
81-
a3 = "Zahlen sind möglich, wenn nicht erstes Zeichen im Namen"
82-
```
83-
84-
## Funktionen (`def`)
85-
86-
```python
87-
def begruessung():
88-
print("Hallo!")
89-
90-
def addiere(a, b):
91-
return a + b
92-
93-
def quadrat(zahl=1): # Standardwert
94-
return zahl ** 2
95-
96-
# Aufrufe
97-
begruessung()
98-
ergebnis = addiere(5, 3) # 8
99-
wert = quadrat(4) # 16
100-
standard = quadrat() # 1
101-
```
102-
103-
## Nützliche eingebaute Funktionen
104-
105-
### `print()`
71+
## Boolesche Werte
10672

10773
```python
108-
print("Hallo Welt") # Hallo Welt
109-
print("Name:", "Alice", "Alter:", 25) # Name: Alice Alter: 25
110-
print("Ergebnis", 42, sep=" -> ") # Ergebnis -> 42
111-
print("Ende", end="!\n") # Ende!
74+
wahr = True
75+
falsch = False
11276

113-
# Formatierung
114-
name = "Bob"
115-
alter = 30
116-
print(f"Ich bin {name} und {alter} Jahre alt")
77+
wahr = True and True
78+
wahr = True or False
11779

80+
wahr = not False
81+
falsch = not True
11882

119-
# Zugriff auf Variable ohne Wert
120-
print(nicht_existent) # Achtung, wirft `NameError()`
83+
falsch = False or False
84+
falsch = True and False
12185
```
12286

123-
### `input()`
87+
## Arithmetik
12488

12589
```python
126-
text = input("Wie heißen Sie? ")
127-
128-
zahl = float(input("Zahl > ") # Achtung, kann `ValueError()` "werfen"
90+
addition = 5 + 3 # 8
91+
subtraktion = 10 - 4 # 6
92+
multiplikation = 6 * 7 # 42
93+
multiplikation_float = 6.0*7 # 42.0
94+
division = 15 / 3 # 5.0
95+
ganzzahldivision = 17 // 5 # 3
96+
modulo = 17 % 5 # 2
97+
potenz = 2 ** 3 # 8
12998
```
13099

131100
## Bedingungen (`if`)
@@ -146,22 +115,6 @@ if alter >= 18 and alter < 65:
146115
print("Arbeitsfähig")
147116
```
148117

149-
## Boolesche Werte
150-
151-
```python
152-
wahr = True
153-
falsch = False
154-
155-
wahr = True and True
156-
wahr = True or False
157-
158-
wahr = not False
159-
falsch = not True
160-
161-
falsch = False or False
162-
falsch = True and False
163-
```
164-
165118
## Schleifen
166119

167120
### `while`
@@ -204,6 +157,25 @@ for index, wert in enumerate(fruechte):
204157
print(f"{index}: {wert}")
205158
```
206159

160+
## Funktionen (`def`)
161+
162+
```python
163+
def begruessung():
164+
print("Hallo!")
165+
166+
def addiere(a, b):
167+
return a + b
168+
169+
def quadrat(zahl=1): # Standardwert
170+
return zahl ** 2
171+
172+
# Aufrufe
173+
begruessung()
174+
ergebnis = addiere(5, 3) # 8
175+
wert = quadrat(4) # 16
176+
standard = quadrat() # 1
177+
```
178+
207179
## Importieren von Code (`import`)
208180

209181
```python
@@ -224,66 +196,6 @@ heute = dt.date.today()
224196
from math import *
225197
```
226198

227-
## Dokumentation
228-
229-
### Kommentare
230-
231-
```python
232-
kein_kommentar # Kommentar
233-
```
234-
235-
### docstrings
236-
237-
Am Anfang einer Datei, Funktion, Klasse, … kann ein String plaziert werden, welche von der Funktion `help()` für die Dokumentation genutzt wird. Es haben sich verschiedene Formatierungen entwickelt. Hier wird eine vorgestellt.
238-
239-
```python
240-
"""Lend or return media, if within allowed number of media on account.
241-
242-
Parameters:
243-
-----------
244-
account_balance (int): The current balance before the transaction is attempted.
245-
Valid values: 0 <= x <= 15
246-
247-
number_of_media (int): The number of the media to be lent or returned.
248-
A positive number signifies lending and a negative Number signifies returning.
249-
250-
Returns:
251-
--------
252-
int: The current account balance.
253-
"""
254-
```
255-
256-
## Ausführbare Skripte
257-
258-
### Shebang
259-
260-
Nur die erste Zeile des Skripts kann als Shebang benutzt werden!
261-
262-
```python
263-
#! /usr/bin/env python3
264-
```
265-
266-
### `if __name__ == "__main__":`
267-
268-
```python
269-
wird_immer_ausgeführt = 1 # bspw. beim Importieren
270-
def das_auch():
271-
print("wird ausgeführt, wenn die Funktion aufgerufen wird")
272-
...
273-
274-
if __name__ == "__main__":
275-
wird_nur_als_skript = "ausgeführt"
276-
das_auch()
277-
```
278-
279-
### Datei ausführbar machen
280-
281-
In Unix-artigen Betriebssystemen.
282-
283-
```bash
284-
$ chmod +x datei.py
285-
```
286-
287199
## Listen (`list`)
288200

289201
```python
@@ -335,6 +247,34 @@ for schluessel in person.keys():
335247
print(schluessel)
336248
```
337249

250+
## Nützliche eingebaute Funktionen
251+
252+
### `print()`
253+
254+
```python
255+
print("Hallo Welt") # Hallo Welt
256+
print("Name:", "Alice", "Alter:", 25) # Name: Alice Alter: 25
257+
print("Ergebnis", 42, sep=" -> ") # Ergebnis -> 42
258+
print("Ende", end="!\n") # Ende!
259+
260+
# Formatierung
261+
name = "Bob"
262+
alter = 30
263+
print(f"Ich bin {name} und {alter} Jahre alt")
264+
265+
266+
# Zugriff auf Variable ohne Wert
267+
print(nicht_existent) # Achtung, wirft `NameError()`
268+
```
269+
270+
### `input()`
271+
272+
```python
273+
text = input("Wie heißen Sie? ")
274+
275+
zahl = float(input("Zahl > ") # Achtung, kann `ValueError()` "werfen"
276+
```
277+
338278
## Dateien lesen und schreiben
339279

340280
```python
@@ -399,6 +339,66 @@ finally:
399339
print("Wird immer ausgeführt")
400340
```
401341

342+
## Dokumentation
343+
344+
### Kommentare
345+
346+
```python
347+
kein_kommentar # Kommentar
348+
```
349+
350+
### docstrings
351+
352+
Am Anfang einer Datei, Funktion, Klasse, … kann ein String plaziert werden, welche von der Funktion `help()` für die Dokumentation genutzt wird. Es haben sich verschiedene Formatierungen entwickelt. Hier wird eine vorgestellt.
353+
354+
```python
355+
"""Lend or return media, if within allowed number of media on account.
356+
357+
Parameters:
358+
-----------
359+
account_balance (int): The current balance before the transaction is attempted.
360+
Valid values: 0 <= x <= 15
361+
362+
number_of_media (int): The number of the media to be lent or returned.
363+
A positive number signifies lending and a negative Number signifies returning.
364+
365+
Returns:
366+
--------
367+
int: The current account balance.
368+
"""
369+
```
370+
371+
## Ausführbare Skripte
372+
373+
### Shebang
374+
375+
Nur die erste Zeile des Skripts kann als Shebang benutzt werden!
376+
377+
```python
378+
#! /usr/bin/env python3
379+
```
380+
381+
### `if __name__ == "__main__":`
382+
383+
```python
384+
wird_immer_ausgeführt = 1 # bspw. beim Importieren
385+
def das_auch():
386+
print("wird ausgeführt, wenn die Funktion aufgerufen wird")
387+
...
388+
389+
if __name__ == "__main__":
390+
wird_nur_als_skript = "ausgeführt"
391+
das_auch()
392+
```
393+
394+
### Datei ausführbar machen
395+
396+
In Unix-artigen Betriebssystemen.
397+
398+
```bash
399+
$ chmod +x datei.py
400+
```
401+
402402
## Python Code ausführen
403403

404404
```bash

0 commit comments

Comments
 (0)