Python Operationen

Arithmetische Operationen und Zuweisungen

JAVA/C++ Python Zweck
\bgroup\color{dblue}$ -x$\egroup \bgroup\color{dblue}$ -x$\egroup Vorzeichen
\bgroup\color{dblue}$ x+y$\egroup \bgroup\color{dblue}$ x+y$\egroup Addition
\bgroup\color{dblue}$ x-y$\egroup \bgroup\color{dblue}$ x-y$\egroup Subtraktion
\bgroup\color{dblue}$ x*y$\egroup \bgroup\color{dblue}$ x*y$\egroup Multiplikation
\bgroup\color{dblue}$ x/y$\egroup \bgroup\color{dblue}$ x/y$\egroup Division
\bgroup\color{dblue}$ x\%y$\egroup \bgroup\color{dblue}$ x\%y$\egroup Modulo (Rest nach Div.)
\bgroup\color{dblue}$ pow(x,y) $\egroup \bgroup\color{dblue}$ x**y$\egroup Potenzieren
\bgroup\color{dblue}$ x= y $\egroup \bgroup\color{dblue}$ x= y $\egroup Zuweisung
\bgroup\color{dblue}$ x\, += \, y\, $\egroup \bgroup\color{dblue}$ x\, += \, y\, $\egroup Zuweisung mit Änderung ( \bgroup\color{dblue}$x = x + y$\egroup )
\bgroup\color{dblue}$(-=, *=, /=, \%=)$\egroup \bgroup\color{dblue}$(-=, *=, /=, \%=, **= )$\egroup

Kein x++, ++x, x–, –x in Python !



Logische Operationen und Vergleiche:

JAVA/C++ Python Zweck
false / (oder \bgroup\color{dblue}$0$\egroup) False falsch
true / (oder \bgroup\color{dblue}$\neq 0$\egroup) True wahr
!x not x Negation
\bgroup\color{dblue}$x \&\& y$\egroup x and y AND Verknüpfung
\bgroup\color{dblue}$x \vert\vert y$\egroup x or y OR Verknüpfung
\bgroup\color{dblue}$x < y$\egroup \bgroup\color{dblue}$x < y$\egroup kleiner als
\bgroup\color{dblue}$x <= y$\egroup \bgroup\color{dblue}$x <= y$\egroup kleiner als oder gleich
\bgroup\color{dblue}$x > y $\egroup \bgroup\color{dblue}$x > y $\egroup größer als
\bgroup\color{dblue}$x >= y$\egroup \bgroup\color{dblue}$x >= y$\egroup größer als oder gleich
\bgroup\color{dblue}$x == y$\egroup \bgroup\color{dblue}$x == y$\egroup gleich
\bgroup\color{dblue}$x != y$\egroup \bgroup\color{dblue}$x != y$\egroup ungleich

Vorrangregeln für Operatoren: f = a*x**2 + b*x + c funktioniert wie erwartet nach Mathematik (Punkt vor Strich ...). Weiteres im Prinzip eindeutig festgelegt, dennoch besser mit Klammern unmissverständlich klarmachen: x = y > z ? (x=y) > z oder x = (y>z)


Bit Operationen:

Python, JAVA, C++ Zweck
\bgroup\color{dblue}$\tilde{} \; x$\egroup Complement
\bgroup\color{dblue}$x \& y$\egroup bitwise AND
\bgroup\color{dblue}$x \vert y$\egroup bitwise OR
\bgroup\color{dblue}$x \; \hat{} \; y$\egroup bitwise XOR
\bgroup\color{dblue}$x << y$\egroup left shift
\bgroup\color{dblue}$x >> y$\egroup right shift mit sign


print(2 | 1)  # = 3
print(2 & 1)  # = 0
print(2 & 3)  # = 2
print(2 <<1)  # = 4


Mehr Beispiele später in den Übungen