Python Operatoren – Een Snelle Referentie

Python-operatoren stellen ons in staat om gemeenschappelijke bewerkingen op variabelen uit te voeren. We zullen verschillende soorten operators bekijken met voorbeelden en ook operatorprecedentie. Het zijn de speciale symbolen die de waarden van één of meer operanden kunnen manipuleren.

Lijst van Python-operatoren

Python-operatoren kunnen worden ingedeeld in verschillende categorieën.

  • Toewijzingsoperatoren
  • Rekenkundige operatoren
  • Logische operatoren
  • Vergelijkingsoperatoren
  • Bitwise operatoren

Python-toewijzingsoperatoren

Toewijzingsoperatoren omvatten de basis-toewijzingsoperator gelijkteken (=).

Maar om code te vereenvoudigen en redundantie te verminderen, bevat Python ook rekenkundige toewijzingsoperatoren.

Dit omvat de += operator in Python gebruikt voor toevoegingsopdracht, //= operator voor vloerafbeelding, en anderen.

Hier is een lijst van alle rekenkundige toewijzingsoperatoren 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)

Met toewijzingsoperators werken


# neem twee variabelen, wijs waarden toe met toewijzingsoperators
a=3
b=4

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

# het is equivalent aan a=a+b
a+=b

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

# het is equivalent aan a=a*b
a*=b
print("a: "+str(a))
print("b: "+str(b))

# het is equivalent aan a=a/b
a/=b
print("a: "+str(a))
print("b: "+str(b))

# het is equivalent aan a=a%b
a%=b
print("a: "+str(a))
print("b: "+str(b))

# het is equivalent aan a=a**b (exponentiële operator)
a**=b
print("a: "+str(a))
print("b: "+str(b))

# het is equivalent aan a=a//b (gehele deling)
a//=b
print("a: "+str(a))
print("b: "+str(b))

Python Rekenkundige Operatoren

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

#maak twee variabelen
a=100
b=200

# optel (+) operator
print(a+b) 

# aftrekking (-) operator
print(a-b) 

# vermenigvuldiging (*) operator
print(a*b)

# deling (/) operator
print(b/a)

# modulus (%) operator
print(a%b) # prints the remainder of a/b

# exponent (**) operator
print(a**b) #prints a^b

Python Vergelijkingsoperatoren

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)

# creëer twee variabelen

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, controleert of twee operanden gelijk zijn of niet

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, controleert of twee operanden niet gelijk zijn
a=int(input())

# (>) operator, controleert of de linker operand groter is dan de rechter operand of niet

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

# (<) operator, controleert linker operand is minder dan rechter operand of niet
if a%4==0 or a%3==0:
    print("either divided by 4 or 3")

# (<=) operator, controleert of de linker operand kleiner is dan of gelijk aan de rechter operand of niet
if not(a%4==0 or a%3==0):
    print("neither divided by 4 nor 3")

Python Bitwise Operators

#creëer twee variabelen


>>> 2+3*4

# Binaire EN (&) operator, uitgevoerde binaire EN-operatie

# Binaire OF (|) operator, uitgevoerde binaire OF-operatie

  1. # Binaire XOR (^) operator, uitgevoerde binaire XOR-operatie
  2. # Binaire Eén Complement (~) operator, uitgevoerde binaire Eén Complement-operatie
  3. # Binaire Linkse Verschuiving (<<) operator, uitgevoerde binaire Linkse Verschuiving operatie
  4. Python Logische Operatoren
  5. #neem gebruikersinvoer als integer
  6. # logische EN-operatie
  7. # logische OF-operatie
  8. # logische NIET-operatie
  9. Python Operator Voorrang
  10. Voorrang van deze operatoren betekent het prioriteitsniveau van de operatoren. Dit wordt belangrijk wanneer een expressie meerdere operatoren bevat. Overweeg bijvoorbeeld de volgende expressie:

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