In deze tutorial leer je alles over de verschillende soorten operators in Python, hun syntaxis en hoe je ze kunt gebruiken met voorbeelden.
Video: operators in Python
Wat zijn operators in python?
Operators zijn speciale symbolen in Python die rekenkundige of logische berekeningen uitvoeren. De waarde waarop de operator werkt, wordt de operand genoemd.
Bijvoorbeeld:
>>> 2+3 5
Hier +
is de operator die optellen uitvoert. 2
en 3
zijn de operanden en 5
is de output van de operatie.
Rekenkundige operatoren
Rekenkundige operatoren worden gebruikt om wiskundige bewerkingen uit te voeren zoals optellen, aftrekken, vermenigvuldigen, enz.
Operator | Betekenis | Voorbeeld |
---|---|---|
+ | Voeg twee operanden of unaire plus toe | x + y + 2 |
- | Trek de rechter operand af van de linker of unaire min | x - y- 2 |
* | Vermenigvuldig twee operanden | x * y |
/ | Verdeel de linker operand door de rechter (resulteert altijd in float) | x / y |
% | Modulus - rest van de verdeling van de linker operand door de rechter | x% y (rest van x / y) |
// | Verdeling van de verdieping - verdeling die resulteert in een geheel getal dat naar links wordt aangepast op de getallenlijn | x // y |
** | Exponent - linker operand verheven tot de macht van rechts | x ** y (x tot de macht y) |
Voorbeeld 1: rekenkundige operatoren in Python
x = 15 y = 4 # Output: x + y = 19 print('x + y =',x+y) # Output: x - y = 11 print('x - y =',x-y) # Output: x * y = 60 print('x * y =',x*y) # Output: x / y = 3.75 print('x / y =',x/y) # Output: x // y = 3 print('x // y =',x//y) # Output: x ** y = 50625 print('x ** y =',x**y)
Uitvoer
x + y = 19 x - y = 11 x * y = 60 x / y = 3,75 x // y = 3 x ** y = 50625
Vergelijkingsoperatoren
Vergelijkingsoperatoren worden gebruikt om waarden te vergelijken. Het keert terug True
of False
volgens de conditie.
Operator | Betekenis | Voorbeeld |
---|---|---|
> | Groter dan - Waar als de linker operand groter is dan de rechter | x> y |
< | Minder dan - Waar als de linker operand kleiner is dan de rechter | x <y |
== | Gelijk aan - Waar als beide operanden gelijk zijn | x == y |
! = | Niet gelijk aan - Waar als operanden niet gelijk zijn | x! = y |
> = | Groter dan of gelijk aan - Waar als de linker operand groter is dan of gelijk is aan rechts | x> = y |
<= | Kleiner dan of gelijk aan - Waar als de linker operand kleiner is dan of gelijk is aan de rechterkant | x <= y |
Voorbeeld 2: vergelijkingsoperatoren in Python
x = 10 y = 12 # Output: x> y is False print('x> y is',x>y) # Output: x < y is True print('x < y is',x= y is False print('x>= y is',x>=y) # Output: x <= y is True print('x <= y is',x<=y)
Uitvoer
x> y is False x = y is False x <= y is True
Logische operators
Logische operatoren zijn de and
, or
, not
operators.
Operator | Betekenis | Voorbeeld |
---|---|---|
en | Waar als beide operanden waar zijn | x en y |
of | Waar als een van de operanden waar is | x of y |
niet | Waar als operand onwaar is (vormt een aanvulling op de operand) | niet x |
Voorbeeld 3: Logische operatoren in Python
x = True y = False print('x and y is',x and y) print('x or y is',x or y) print('not x is',not x)
Uitvoer
x en y is False x of y is True, niet x is False
Hier is de waarheidstabel voor deze operators.
Bitwise-operators
Bitwise-operators werken op operanden alsof het reeksen van binaire cijfers zijn. Ze werken beetje bij beetje, vandaar de naam.
2 is bijvoorbeeld 10
in binair en 7 is 111
.
In de onderstaande tabel: zij x = 10 ( 0000 1010
in binair) en y = 4 ( 0000 0100
in binair)
Operator | Betekenis | Voorbeeld |
---|---|---|
& | Bitwise EN | x & y = 0 ( 0000 0000 ) |
| | Bitsgewijs OF | x | y = 14 ( 0000 1110 ) |
~ | Bitsgewijs NIET | ~ x = -11 ( 1111 0101 ) |
^ | Bitsgewijze XOR | x y = 14 ( 0000 1110 ) |
>> | Bitsgewijs naar rechts verschuiven | x >> 2 = 2 ( 0000 0010 ) |
<< | Bitsgewijs naar links verschuiven | x << 2 = 40 (0010 1000 ) |
Assignment operators
Assignment operators are used in Python to assign values to variables.
a = 5
is a simple assignment operator that assigns the value 5 on the right to the variable a on the left.
There are various compound operators in Python like a += 5
that adds to the variable and later assigns the same. It is equivalent to a = a + 5
.
Operator | Example | Equivalent to |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 5 | x = x + 5 |
-= | x -= 5 | x = x - 5 |
*= | x *= 5 | x = x * 5 |
/= | x /= 5 | x = x / 5 |
%= | x %= 5 | x = x % 5 |
//= | x //= 5 | x = x // 5 |
**= | x **= 5 | x = x ** 5 |
&= | x &= 5 | x = x & 5 |
|= | x |= 5 | x = x | 5 |
^= | x ^= 5 | x = x 5 |
>>= | x>>= 5 | x = x>> 5 |
<<= | x <<= 5 | x = x << 5 |
Special operators
Python language offers some special types of operators like the identity operator or the membership operator. They are described below with examples.
Identity operators
is
and is not
are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical.
Operator | Meaning | Example |
---|---|---|
is | True if the operands are identical (refer to the same object) | x is True |
is not | True if the operands are not identical (do not refer to the same object) | x is not True |
Example 4: Identity operators in Python
x1 = 5 y1 = 5 x2 = 'Hello' y2 = 'Hello' x3 = (1,2,3) y3 = (1,2,3) # Output: False print(x1 is not y1) # Output: True print(x2 is y2) # Output: False print(x3 is y3)
Output
False True False
Here, we see that x1 and y1 are integers of the same values, so they are equal as well as identical. Same is the case with x2 and y2 (strings).
But x3 and y3 are lists. They are equal but not identical. It is because the interpreter locates them separately in memory although they are equal.
Membership operators
in
en not in
zijn de lidmaatschapsoperatoren in Python. Ze worden gebruikt om te testen of een waarde of variabele wordt gevonden in een reeks (string, lijst, tuple, set en woordenboek).
In een woordenboek kunnen we alleen testen op de aanwezigheid van de sleutel, niet op de waarde.
Operator | Betekenis | Voorbeeld |
---|---|---|
in | Waar als waarde / variabele wordt gevonden in de reeks | 5 in x |
niet in | Waar als waarde / variabele niet in de reeks wordt gevonden | 5 niet in x |
Voorbeeld # 5: lidmaatschapsoperatoren in Python
x = 'Hello world' y = (1:'a',2:'b') # Output: True print('H' in x) # Output: True print('hello' not in x) # Output: True print(1 in y) # Output: False print('a' in y)
Uitvoer
Juist Juist Juist Fout
Hier 'H'
staat in x maar 'hello'
is niet aanwezig in x (onthoud dat Python hoofdlettergevoelig is). Evenzo 1
is de sleutel en 'a'
is de waarde in woordenboek y. Keert dus 'a' in y
terug False
.