int und float in Kombination (V)

7.2.4. int und float in Kombination (V)#

Da wir bei mathematischen Operationen wie +, -, *, /, // und ** ganze Zahlen int und Fließkommazahlen float vermischen können, müssen wir darauf achten welcher Datentyp am Ende herauskommt. Sobald eine Fließkommazahl ein Teil der Berechnung ist, ist das Ergebnis vom Typ float.

x = 3 * 1.0     # int * float -> float   
print(type(x))
print(x)
<class 'float'>
3.0
large_number = 10.0**100     # float ** int -> float  
print(type(large_number))
print(large_number)
<class 'float'>
1e+100

Auch für die ganzzahligen Division // erhalten wir als Datentyp eine Fließkommazahl sofern eine Fließkommazahl bei der Operation teilnimmt:

x = 3.0 // 2     # float // int -> float   
print(type(x))
print(x)
<class 'float'>
1.0
x = 3 // 2.0     # int // float -> float   
print(type(x))
print(x)
<class 'float'>
1.0
x = 3 // 2     # int // int -> int   
print(type(x))
print(x)
<class 'int'>
1