Spickzettel

############## Operatoren #################################

3 / 4, 3 // 4, 3 % 4, 3 ** 4 #==> 0.75, 0, 3, 81
x += 1 # x = x+1 (Kein "x++" in Python)
x, y = y, x # Mehrfachzuweisung 4 > 3, 4 >= 3, 3 == 3.0, 3 != 4, 3 <= 4 #==> True, True, True, True, True
3 < x < 5 # entspricht "(3 < x) and (x < 5)"
not True, False or True, False and True #==> False, True, False


############ Kontrollstrukturen ########################### if booleanCondition: x # indent the body block x # every line by the same amount elif anotherCondition: # can do zero, one, or several elif blocks x # multi-line block else: # optional x # multi-line block while booleanCondition: x # the body block for zahl in range(von, bis): print(zahl) #==> von, von+1, ..., bis-1 for zeichen in 'Hallo':
print(zeichen) # Druckt nacheinander H, a, l, l, o

############### Funktionen ################################
def nameOfNewFunction(argument1, argument2): x # the body block return y # (optional; if you don't return, it returns None)
# Eingebaute Funktionen / Built-in functions
min(3, 4), max(3, 4), abs(-10) #==> 3, 4, 10
sum([1, 2, 3]) # [1, 2, 3] is a list #==> 6
ord("A"), chr(66) #==> 65, 'B'

id("Hallo") # ID eines Objektes
int("4"+"0"), float(3), str(1 / 2) #==> 40, 3.0, '0.5'
type(3), type(3.0), type("myVariable") #==> class 'int'/'float'/'str'

eingabetext = input() #==> Lese Benutzereingabe print("takes", 0, "or more arguments") #==> takes 0 or more arguments print("using", "custom", "sep", sep=".") #==> using.custom.sep print("no", "newline", end="bye") #==> no newlinebye

# Funktionen importieren
import math # import Variante 1 print(math.sqrt(2)) from math import sqrt # import Variante 2 print(sqrt(2)) # auch im math Modul: pi, log(), sin(), cos(), ...


############## Zeichenketten ##############################

string = "hello" # Zeichenketten in "
string = 'hello' # oder '
# die folgenden Ausdrücke lassen sich auch auf Listen anwenden
len(string) #==> 5
string[0], string[4] #==> "h", "o"
string[1:3] #==> "el"
string[:2], string[2:] #==> "he", "llo"
string[-1], string[-2:] #==> "o", "lo"
"con" + "cat" + "enation " + str(123) #==> "concatenation 123"
"boo" * 2 #==> "booboo"
"string" in "superstring" #==> True
"superstring".index("string") #==> 5
# Einige Methoden von Zeichenketten: capitalize(), lower/upper(),
# isalpha/isdigit(), center/ljust/rjust(width, fillChar), strip(), split(),
# splitlines(), endswith/startswith(string)


############### Listen ####################################
list = ['zero', 'one', 'two']
list[0] #==> 'zero'
list[:2] #==> ['zero', 'one']
list[1:] #==> ['one', 'two']
list[:] #==> ['zero', 'one', 'two']
list[-1] #==> 'two' list.index('one') #==> 1 list.index('three') #==> causes an error 'three' in list, 'zero' in list #==> False, True list.count('two') #==> 1
list = list + [3] # liste enthält ['zero', 'one', 'two', 3]

# Weitere Listenmethoden: append(item), insert(item, index), extend(list),
# remove(value), pop(), pop(index), reverse(), sort(), ...

myList = [11, 99]
actuallyTheSameList = myList # Keine Kopie, da gleiche Referenz!
myList is actuallyTheSameList #==> True
realCopy = myList[:] # oder z.B. mit list(myList)
realCopy is myList #==> False