Datentypen beschreiben was für Data der Rechner benutzt. Zum Beispiel, integers (1,2,3) sind kein floating point numbers (3.1415..).
Rechners benutzen immer das Binary System (0,1), trotzdem müssen Software Entwickler diesen meistens nicht benutzen. Sie können einfach integers oder andern Datentypen benutzen.
In Python ist explizite Definition von Datentypen nicht verpflichtet.
Practice Python with interactive exercises
Datentypen
Datentypen der Python unterstützt sind:
| Datentypen |
Beschreibung |
Beispiel |
Note |
| int |
Integers. |
0,1,2,3.. |
Mit diesem Programm bekommst du das maximal Zahl:
import sys
print(sys.maxint)
|
| float |
Floating point numbers. |
6.7395, 1.6840 |
Für das Minimum und Maximum Zahl:
import sys
print(sys.float_info)
|
| long |
Long integers. |
* |
- |
| complex |
Complex numbers. |
2r, 3i |
Komplexen Zahlen erweitern den Zahlenbereich der reellen Zahlen mit einer imaginären Zahl. |
| boolean |
Boolean numbers. |
True, False |
Subtype von integers. |
Floating point numbers
Die floating point numbers sind manchmal nicht korrekt wegen Abrundens Fehler. Sei Vorsicht als du die floating point numbers benutzt.
Beispiel von floating point Fehler:
x = 1
y = 0.00000000000000007
print(x + y)
# output 1
Der Arianne 5 Rakete war abgestürzt wegen floating point Fehler. Der Rakete war in 1996 gemacht und hat 7 Billionen gekostet.
Beispiel
Explizite Definition von Datentypen ist nicht verpflichtet, wie das Beispiel hierunter zeigt.
#!/usr/bin/python
x = 3 # integer (variablen x)
f = 3.1415926 # floating (variablen f)
name = "Python" # string
big = 358315791 # long
z = complex(2,3) # (2+3i) Komplexe Zahl.
print(x)
print(f)
print(name)
print(big)
print(z)
Resultat:
3
3.1415926
Python
358315791
(2+3j)
Operatoren
Du kannst arithmetische Operatoren benutzen wie Addieren (+), Subtraktion (-), Teilung (-) und Multiplikation (*).
| Operation |
Result |
| x + y |
Summe von x und y. |
| x * y |
Multiplikation von x und y. |
| x - y |
Unterschied zwischen x und y. |
| x / y |
Teilung von x durch y. |
| x % y |
Rest x/y |
| x ** y |
x hoch y |
| abs(x) |
absolute wert von x |
| sqrt(x) |
Quadratwurzel von x |
In die Code hierunter machen wir zwei variablen (x und y). Exekutieren die Expression (x+y) und in speichern das Resultat in die variablen sum.
#!/usr/bin/env python
x = 3
y = 8
sum = x + y
print(sum)
Mathematische Funktionen
Python unterstützt verschiedene mathematische Funktionen.
| Function |
Returns |
Example |
| abs(x) |
Returns the absolute value of x. |
x = -35
x = abs(x)
print(x)
|
| cmp(x,y) |
Returns -1 if x < y
Returns 0 if x equals to y
Returns 1 if x > y. |
x = 6
y = 4
print( cmp(x,y) )
|
| exp(x) |
Returns the exponential of x |
import math
x = 6
print( math.exp(x) )
|
| log(x) |
The natural logarithm of x |
import math
x = 6
print( math.log(x) )
|
| log10(x) |
The base-10 logarithm of x |
import math
x = 6
print( math.log10(x) )
|
| pow(x,y) |
The result of x**y |
import math
x = 6
print( math.pow(x,2) )
|
| sqrt(x) |
The square root of x |
import math
x = 6
print( math.sqrt(x) )
|
Manchmal ist konvertieren zwischen Datentypen Essential.
| Function |
Purpose |
| int(x) |
Convert x to integer |
| long(x) |
Convert x to long |
| float(x) |
Convert x to float |
Keyboard Input
Wenn du variablen an der Endbenutzer fragen möchtest, kannst du der (Python 3) input() oder der raw_input() Funktion benutzen.
Python 3 version:
x = int(input("Enter x:"))
y = int(input("Enter y:"))
sum = x + y
print(sum)
Python 2 version:
x = int(raw_input("Enter x:"))
y = int(raw_input("Enter y:"))
sum = x + y
print(sum)