Python-Operatoren – Eine schnelle Referenz

Python-Operatoren ermöglichen uns, gängige Verarbeitungen an Variablen durchzuführen. Wir werden uns verschiedene Arten von Operatoren mit Beispielen ansehen und auch die Operatorpräzedenz betrachten. Sie sind die speziellen Symbole, die die Werte eines oder mehrerer Operanden manipulieren können.

Liste der Python-Operatoren

Python-Operatoren können in mehrere Kategorien eingeteilt werden.

  • Zuweisungsoperatoren
  • Arithmetische Operatoren
  • Logische Operatoren
  • Vergleichsoperatoren
  • Bitweise Operatoren

Python-Zuweisungsoperatoren

Zuweisungsoperatoren umfassen den grundlegenden Zuweisungsoperator Gleichheitszeichen (=).

Aber um den Code zu vereinfachen und Redundanz zu reduzieren, enthält Python auch arithmetische Zuweisungsoperatoren.

Dazu gehört der += Operator in Python für die Additionszuweisung, //= Operator für die Bodenteilungszuweisung und andere.

Hier ist eine Liste aller arithmetischen Zuweisungsoperatoren in Python.

Operator Description
+= a+=b is equivalent to a=a+b
*= a*=b is equivalent to a=a*b
/= a/=b is equivalent to a=a/b
%= a%=b is equivalent to a=a%b
**= a**=b is equivalent to a=a**b (exponent operator)
//= a//=b is equivalent to a=a//b (floor division)

Verwendung von Zuweisungsoperatoren


# Nehmen Sie zwei Variablen, weisen Sie Werte mit Zuweisungsoperatoren zu
a=3
b=4

print("a: "+str(a))
print("b: "+str(b))

# Es entspricht a=a+b
a+=b

print("a: "+str(a))
print("b: "+str(b))

# Es entspricht a=a*b
a*=b
print("a: "+str(a))
print("b: "+str(b))

# Es entspricht a=a/b
a/=b
print("a: "+str(a))
print("b: "+str(b))

# Es entspricht a=a%b
a%=b
print("a: "+str(a))
print("b: "+str(b))

# Es entspricht a=a**b (Exponentenoperator)
a**=b
print("a: "+str(a))
print("b: "+str(b))

# Es entspricht a=a//b (Ganzzahldivision)
a//=b
print("a: "+str(a))
print("b: "+str(b))

Python-Arithmetikoperatoren

Operator Description Example
+ used to add two numbers sum = a + b
used for subtraction difference = a – b
* used to multiply two numbers. If a string and int is multiplied then the string is repeated the int times. mul = a*b>>> “Hi”*5
‘HiHiHiHiHi’
/ used to divide two numbers div = b/a
% modulus operator, returns the remainder of division mod = a%b
** exponent operator

# Erstellen Sie zwei Variablen
a=100
b=200

# Additions (+) Operator
print(a+b) 

# Subtraktions (-) Operator
print(a-b) 

# Multiplikations (*) Operator
print(a*b)

# Divisions (/) Operator
print(b/a)

# Modulus (%) Operator
print(a%b) # prints the remainder of a/b

# Exponent (**) Operator
print(a**b) #prints a^b

Python-Vergleichsoperatoren

Operator Description Example
== returns True if two operands are equal, otherwise False. flag = a == b
!= returns True if two operands are not equal, otherwise False. flag = a != b
> returns True if left operand is greater than the right operand, otherwise False. flag = a > b
< returns True if left operand is smaller than the right operand, otherwise False. flag = a < b
>= returns True if left operand is greater than or equal to the right operand, otherwise False. flag = a > b
<= returns True if left operand is smaller than or equal to the right operand, otherwise False. flag = a < b

# create two variables
a=100
b=200

# (==) operator, checks if two operands are equal or not
print(a==b)

# (!=) operator, checks if two operands are not equal
print(a!=b)

# (>) operator, checks left operand is greater than right operand or not
print(a>b)

# (<) operator, checks left operand is less than right operand or not
print(a<b)
#(>=) operator, checks left operand is greater than or equal to right operand or not
print(a>=b)

# (<=) operator, checks left operand is less than or equal to right operand or not
print(a<=b)

# Erstelle zwei Variablen

Operator Description Example
& Binary AND Operator x = 10 & 7 = 2
Binary OR Operator
^ Binary XOR Operator x = 10 ^ 7 = 13
~ Binary ONEs Compliment Operator x = ~10 = -11
<< Binary Left Shift operator x = 10<<1 = 20
>> Binary Right Shift Operator x = 10>>1 = 5

#create two variables
a=10 # binary 1010
b=7  # binary 0111

# Binary AND (&) operator, done binary AND operation
print(a&b)

# Binary OR (|) operator, done binary OR operation
print(a|b)

# Binary XOR (^) operator, done binary XOR operation
print(a^b)

# Binary ONEs Compliment (~) operator, done binary One's Compliment operation
print(~a)

# Binary Left Shift (<<) operator, done binary Left Shift operation
print(a<<1) 
# Binary Right Shift (>>) operator, done binary Right Shift operation
print(a>>1)

# (==) Operator, überprüft ob zwei Operanden gleich oder ungleich sind

Operator Description Example
and Logical AND Operator flag = exp1 and exp2
or Logical OR Operator flag = exp1 or exp2
not Logical NOT Operator flag = not(True) = False

# (!=) Operator, überprüft ob zwei Operanden ungleich sind
a=int(input())

# (>) Operator, überprüft ob der linke Operand größer als der rechte Operand ist

if a%4==0 and a%3==0:
    print("divided by both 4 and 3")

# (<) Operator, überprüft ob der linke Operand kleiner als der rechte Operand ist oder nicht
if a%4==0 or a%3==0:
    print("either divided by 4 or 3")

# (<=) Operator, überprüft ob der linke Operand kleiner oder gleich dem rechten Operanden ist
if not(a%4==0 or a%3==0):
    print("neither divided by 4 nor 3")

Python Bitweise Operatoren

# Erstelle zwei Variablen


>>> 2+3*4

# Binärer UND (&) Operator, durchgeführte binäre UND-Operation

# Binärer ODER (|) Operator, durchgeführte binäre ODER-Operation

  1. # Binärer XOR (^) Operator, durchgeführte binäre XOR-Operation
  2. # Binärer Einzelkomplement-Operator (~), durchgeführte binäre Einzelkomplement-Operation
  3. # Binärer Linksverschiebeoperator (<<) Operator, fertig binär Links Shift Operation
  4. Python Logische Operatoren
  5. #Nehme Benutzereingabe als Ganzzahl
  6. # Logische UND-Operation
  7. # Logische ODER-Operation
  8. # Logische NOT-Operation
  9. Python Operator Priorität
  10. Die Priorität dieser Operatoren gibt das Prioritätsniveau der Operatoren an. Dies wird wichtig, wenn ein Ausdruck mehrere Operatoren enthält. Betrachten Sie beispielsweise den folgenden Ausdruck:

Source:
https://www.digitalocean.com/community/tutorials/python-operators