Cheatography
https://cheatography.com
test of sheet for my class
This is a draft cheat sheet. It is a work in progress and is not finished yet.
while-Schleifen
i = input("Zum Beenden 0 eingeben: ")
while i != 0:
i = input("Zum Beenden 0 eingeben: ")
|
continue & break
break
beendet Schleife
continue
springt zum Schleifenanfang und fährt mit der nächsten Iteration fort
break und else
for value in values:
if value == 5:
print("Gefunden!")
break
else:
# wird Ausgeführt, wenn Schleife nicht mit break beendet wird
print("Nicht gefunden :-(")
|
For loops
# Strategy: Iterate over a copy
for user, status in users.copy().items():
if status == 'inactive':
del users[user]
# Strategy: Create a new collection
active_users = {}
for user, status in users.items():
if status == 'active':
active_users[user] = status
|
Zahlen
3 |
eine ganze Zahl (integer oder int) |
3.2 |
eine Gleitpunktzahl (float) |
3 + 5j |
eine komplexe Zahl |
+,-,*,/ |
Grundrechenarten |
a ** b |
Potenz |
math.sqrt(a) |
Wurzel |
math.log(a, base) |
Logarithmus |
a // b |
ganzzahlige Division |
a % b |
Rest der Division |
abs(a) |
Betrag |
round(a, n) |
Runden auf n Nachkommastellen |
Im math Modul sind viele weitere Funktionen
if...else
if n < 2:
# Block wird ausgeführt, falls n < 2
print("Fall 1")
elif n < 4:
# Block wird ausgeführt, falls 2 <= n < 4
print("Fall 2")
else:
# Block wird ausgeführt, falls n >= 4
print("Fall 3")
|
|
|
Escape Characters
\n |
neue Zeile |
\" |
" |
\' |
' |
\\ |
\ |
Klassen
class Robot:
def __init__(name, color, weight):
self._name = name
self._color = color
self._weight = weight
def introduce_self(self):
print("Ich heiße " + self.name)
r1 = Robot("Tom", red, 30)
r2 = Robot("Terry", blue, 40)
|
Datenkapselung
Attribute einer Klasse sind normalerweise protected getter/setter verwenden
# Getter Methode als Property
@property
def x(self):
return self._x
# Setter Methode als Property
@x.setter
def x(self, value):
if value < 1000:
self._x = value
|
Strings
'Hallo' oder "Hallo" |
eine Zeichenkette/string |
'Blau' * 3 |
BlauBlauBlau |
'Blau' + 'beere' |
Blaubeere |
len(a) |
Länge von string a |
int('123') |
casten zu int |
float('123') |
casten zu float |
str('123') |
casten zu string |
print("""\ Usage: thingy [OPTIONS] -h Display this usage message -H hostname Hostname to connect to """) |
Multiline String |
text = ('Put several strings within parentheses ' 'to have them joined together.') |
Formatieren mit f
String methods
print('The value of i is', i)
print(a, end=',')
Wahrheitswerte/Boolean
True / False |
bool |
a > b |
True wenn a kleiner als b, sonst False |
a <= b |
kleiner gleich |
a > b |
größer |
a >= b |
größer gleich |
a == b |
gleich |
a != b |
ungleich |
not |
negiert Wahrheitswert |
or |
Logisches Oder |
and |
Logisches Und |
a in sequenze |
|