Variablentypen & Stammfunktionen
Zahlen
mit type
wird der Typ eines Objektes bestimmt
>>> type(3)
<class 'int'>
>>> type(3.14)
<class 'float'>
>>> pi = 3.14
>>> type(pi)
<class 'float'>
>>> type(True)
<class 'bool'>
Mathematische Operationen
>>> 3 + 3
6
>>> 3 - 3
0
>>> 3 / 3
1.0
>>> 3 / 2
1.5
>>> 3 * 3
9
>>> 3 ** 3
27
>>> num = 3
>>> num = num - 1
>>> print(num)
2
>>> num = num + 10
>>> print(num)
12
>>> num += 10
>>> print(num)
22
>>> num -= 12
>>> print(num)
10
>>> num *= 10
>>> num
100
Modulo Division
>>> 10 % 3
1
>>> 10 % 2
0
Klammersetzung beachten
>>> (2 + 3) * 5
25
>>> 2 + 3 * 5
17
Strings
Quotes
Python unterscheided bei Strings nicht zwischen einfachen und doppelten Anführungszeichen:
>>> "string list"
'string list'
>>> 'string list'
'string list'
Escaping Quotes
>>> "I ’cant do that"
'I ’cant do that'
>>> "He said \"no\" to me"
'He said "no" to me'
"raw"-String
Bei "raw"-Strings werden Zeilenumbruche nicht umgewandelt ( r"string" )
>>> affe = "Monkey\nMonkey\n"
>>> print(affe)
Monkey
Monkey
>>> affe = r"Monkey\nMonkey\n"
>>> print(affe)
Monkey\nMonkey\n
Verbinden von Strings
>>> a = "first"
>>> b = "last"
>>> a + b
'firstlast'
Boolean Vergleichsoperatoren
gleich / nicht-gleich
>>> 8 == 8
True
>>> 8 == 12
False
>>> 8.1 == 8
False
>>> "Hallo" == "Hallo"
True
>>> "Hallo" == "Holla"
False
>>>
>>> 8 != 8
False
>>> 8 != 12
True
größer / kleiner als
>>> 10 > 10
False
>>> 10 < 11
True
>>> 10 >= 10
True
>>> 10 <= 11
True
>>> 10 <= 10 < 0
False
>>> 10 <= 10 < 11
True