Datentypen und Variablen
Python hosting: Host, run, and code Python in the cloud!
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.
Python Programming Bootcamp: Go from zero to hero
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: {% codeblock lang:python line_number:false %} import sys print(sys.maxint) {% endcodeblock %} |
| float | Floating point numbers. | 6.7395, 1.6840 | Für das Minimum und Maximum Zahl: {% codeblock lang:python line_number:false %} import sys print(sys.float_info) {% endcodeblock %} |
| 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:
|
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.
|
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.
|
Mathematische Funktionen
Python unterstützt verschiedene mathematische Funktionen.
| Function | Returns | Example |
|---|---|---|
| abs(x) | Returns the absolute value of x. | {% codeblock lang:python line_number:false %} x = -35 x = abs(x) print(x) {% endcodeblock %} |
| cmp(x,y) |
Returns -1 if x < y Returns 0 if x equals to y Returns 1 if x > y. |
{% codeblock lang:python line_number:false %} x = 6 y = 4 print( cmp(x,y) ) {% endcodeblock %} |
| exp(x) | Returns the exponential of x | {% codeblock lang:python line_number:false %} import math x = 6 print( math.exp(x) ) {% endcodeblock %} |
| log(x) | The natural logarithm of x | {% codeblock lang:python line_number:false %} import math x = 6 print( math.log(x) ) {% endcodeblock %} |
| log10(x) | The base-10 logarithm of x | {% codeblock lang:python line_number:false %} import math x = 6 print( math.log10(x) ) {% endcodeblock %} |
| pow(x,y) | The result of x**y | {% codeblock lang:python line_number:false %} import math x = 6 print( math.pow(x,2) ) {% endcodeblock %} |
| sqrt(x) | The square root of x | {% codeblock lang:python line_number:false %} import math x = 6 print( math.sqrt(x) ) {% endcodeblock %} |
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:
|
Python 2 version:
|
Leave a Reply: