Show Menu
Cheatography

Python Cheat Sheet Cheat Sheet (DRAFT) by

test of sheet for my class

This is a draft cheat sheet. It is a work in progress and is not finished yet.

while-­Sch­leifen

i = input("Zum Beenden 0 eingeben: ") 
while i != 0:
    i = input("Zum Beenden 0 eingeben: ")

Vererbung

class Banana(Fruit)

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 Gleitp­unk­tzahl (float)
3 + 5j
eine komplexe Zahl
+,-,*,/
Grundr­ech­enarten
a ** b
Potenz
math.s­qrt(a)
Wurzel
math.l­og(a, base)
Logari­thmus
a // b
ganzza­hlige Division
a % b
Rest der Division
abs(a)
Betrag
round(a, n)
Runden auf n Nachko­mma­stellen
Im math Modul sind viele weitere Funktionen

Listen

.add()

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")

Kommentare

# dies ist ein Kommentar

Variable zuweisen

x = 2
 

Iteration

range()
range

Match

 

Escape Characters

\n
neue Zeile
\"
"
\'
'
\\
\

Funktionen

def foo():
 return s

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)

Datenk­aps­elung

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 "­Hal­lo"
eine Zeiche­nke­tte­/string
'Blau' * 3
BlauBl­auBlau
'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 parent­heses ' 'to have them joined togeth­er.')
Format­ieren mit f
String methods
print('The value of i is', i)
print(a, end=',')

Wahrhe­its­wer­te/­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 Wahrhe­itswert
or
Logisches Oder
and
Logisches Und
a in sequenze