Python Operators
- Previous Page Python Boolean
- Next Page Python Lists
Python Operators
Operatoren worden gebruikt om acties uit te voeren op variabelen en waarden.
Python verdeelt operatoren in de volgende groepen:
- Wiskundige operatoren
- Toewijzingsoperatoren
- Vergelijkingsoperatoren
- Logische operatoren
- Identiteit operatoren
- Lid operatoren
- Bit operatoren
Python wiskundige operatoren
Wiskundige operatoren worden samen met getallen gebruikt om gebruikelijke wiskundige berekeningen uit te voeren:
Operator | Naam | Example | Try It |
---|---|---|---|
+ | Toevoegen | x + y | Try It |
- | Verlagen | x - y | Try It |
* | Vermenigvuldigen | x * y | Try It |
/ | Delen | x / y | Try It |
% | Modulo | x % y | Try It |
** | Macht | x ** y | Try It |
// | Bodemdeling (hele deling) | x // y | Try It |
Python toewijzingsoperatoren
Toewijzingsoperatoren worden gebruikt om variabelen toe te wijzen:
Operator | Example | Gelijk | Try It |
---|---|---|---|
= | x = 5 | x = 5 | Try It |
+= | x += 3 | x = x + 3 | Try It |
-= | x -= 3 | x = x - 3 | Try It |
*= | x *= 3 | x = x * 3 | Try It |
/= | x /= 3 | x = x / 3 | Try It |
%= | x %= 3 | x = x % 3 | Try It |
//= | x //= 3 | x = x // 3 | Try It |
**= | x **= 3 | x = x ** 3 | Try It |
&= | x &= 3 | x = x & 3 | Try It |
|= | x |= 3 | x = x | 3 | Try It |
^= | x ^= 3 | x = x ^ 3 | Try It |
>>= | x >>= 3 | x = x >> 3 | Try It |
<<= | x <<= 3 | x = x << 3 | Try It |
Python vergelijkingsoperatoren
Vergelijkingsoperatoren worden gebruikt om twee waarden te vergelijken:
Operator | Naam | Example | Try It |
---|---|---|---|
== | Gelijk aan | x == y | Try It |
!= | Niet gelijk aan | x != y | Try It |
> | Groter | x > y | Try It |
< | Kleiner | x < y | Try It |
>= | Groter of gelijk aan | x >= y | Try It |
<= | Kleiner of gelijk aan | x <= y | Try It |
Python logische operatoren
Logische operatoren worden gebruikt om conditie-expressies te combineren:
Operator | Description | Example | Try It |
---|---|---|---|
en | Als beide zinnen waar zijn, wordt True geretourneerd. | x > 3 en x < 10 | Try It |
of | Als een van de zinnen waar is, wordt True geretourneerd. | x > 3 of x < 4 | Try It |
not | Het omgekeerde resultaat, als het resultaat true is, dan wordt False geretourneerd | not(x > 3 and x < 10) | Try It |
Python Identity Operators
Identity operators are used to compare objects, not to compare whether they are equal, but if they are actually the same object, they have the same memory location:
Operator | Description | Example | Try It |
---|---|---|---|
is | Returns true if the two variables are the same object. | x is y | Try It |
is not | Returns true if the two variables are not the same object. | x is not y | Try It |
Python Membership Operators
Membership operators are used to test whether a sequence appears in the object:
Operator | Description | Example | Try It |
---|---|---|---|
in | Returns True if the specified value exists in the sequence of the object. | x in y | Try It |
not in | Returns True if the specified value does not exist in the sequence of the object. | x not in y | Try It |
Python Bitwise Operators
Bitwise operators are used to compare (binary) numbers:
Operator | Description | Example |
---|---|---|
& | AND | Set each bit to 1 if both bits are 1. |
| | OR | Set each bit to 1 if one of the two bits is 1. |
^ | XOR | Set each bit to 1 if only one of the two bits is 1. |
~ | NOT | Reverse all bits. |
<< | Zero fill left shift | Move to the left by pushing zero to the right, pushing off the leftmost position. |
>> | Signed right shift | Move to the right by pushing the copy of the leftmost position to the right, pushing off the rightmost position. |
- Previous Page Python Boolean
- Next Page Python Lists