Primitive Datentypen

Ganze Zahlen (int)
Ganze Zahlen werden in Python sehr speziell behandelt. Für "kleine" Zahlen ( \bgroup\color{dblue}$ \approx \pm 2 \cdot 10^9$\egroup) ist es wie in JAVA/C++: 32-bit integer.

Zusätzlich gibt es die long integers deren Länge unbegrenzt ist (bis auf System–Grenzen). Python erledigt automatisch den Übergang von 32-bit int zu long int falls erforderlich.

>>> 25**2
625          # int
>>> 25**6
244140625    # int 
>>> 25**7
6103515625L  # long
>>> 25**200
38725919148493182728180306332863518475702191920487908654877629413
44416348097685964862682234277014596908057542507554467539370836398
99235031552231805065335049200243606527053080273843203837317475409
08093676464549424001812701625789688468162611303946540886045113438
74037265777587890625L  # really long ...


Gleitkommazahlen (float)

>>> 2.6+7.8
10.4
>>> 2.5**2
6.25
>>> 2.5**(1./10)
1.0959582263852172

Entsprechen den double in JAVA/C++, d.h. 64 bit werden zur Darstellung verwendet, unterteilt in Mantisse und Exponent:

Image double


In Python teilweise implizite Typumwandlung ( int \bgroup\color{dblue}$\rightarrow$\egroup float)
aber Vorsicht ...

>>> f=2/3
>>> f
0.6666666666666666
# automatic float arithmetics, new in Python3 (in Python2: f=0)
#
# if int arithmetics desired:
>>> f=2//3
>>> f
0


Mathematische Funktionen, wie sin, cos, exp, etc., sind im Python Modul math enthalten und können über math.function angesprochen werden, zuvor import math ausführen:

>>> import math
>>> math.sin(0.5)
0.47942553860420301
>>> math.log10(65)
1.8129133566428555
>>> ...



Name Purpose
acos(x) Arc cosine of x
asin(x) Arc sine of x
atan(x) Arc tangent of x
atan2(x,y) Arc tangent of x/y
ceil(x) Ceiling of x; the largest integer equal to or greater than x
cos(x) Cosine of x
cosh(x) Hyperbolic cosine of x
exp(x) e raised to the power of x
fabs(x) Absolute value of x
floor(x) Floor of x; the largest integer equal to or less than x
fmod(x) x modulo y
frexp(x) The mantissa and exponent for x.
hypot(x, y) Euclidean distance, sqrt(x*x + y*y)
ldexp(x, i) x * (2**i)
log(x) Natural logarithm of x
log10(x) Base 10 logarithm of x
modf(x) Return the fractional and integer parts of x
pow(x, y) x raised to the power of y
sin(x) Sine of x
sinh(x) Hyperbolic sine of x
sqrt(x) Square root of x
tan(x) Tangent of x
tanh(x) Hyperbolic tangent of x


Komplexe Zahlen

In Python sind komplexe Zahlen als Paar von 2 Gleitkommazahlen implementiert.

>>> z=2+4j
>>> abs(z)
4.4721359549995796
>>> v=3-2j
>>> z/v
(-0.15384615384615385+1.2307692307692308j)
>>> z**3
(-88-16j)
>>> z**v
(775.7930102695509+262.01856311381101j)
>>> z.imag
4.0
>>> z.real
2.0

Mathematische Funktionen für komplexe Zahlen sind analog zu Gleitkommazahlen definiert, nur muss das Modul cmath geladen werden ( import cmath )

>>> import cmath
>>> cmath.exp(1+1j)
(1.4686939399158851+2.2873552871788423j)
>>> cmath.exp(math.pi/2 * 1j)
(6.1230317691118863e-17+1j)
>>> ...


Liste der reservierten Wörter in Python

Dürfen nicht als Namen für Variablen, Funktionen, Klassen verwendet werden!

and del for is raise
assert elif from lambda return
break else global not try
class except if or while
continue exec import pass yield
def finally in print


Hinzu kommen noch die Namen gängiger Typen, Klassen und Funktionen wie
bool float sin exp ...
Wird zwar syntaktisch von Python akzeptiert, führt aber leicht ins Chaos ...