# Operatoren

# Voorrang

In volgorde van voorrang tijdens uitvoeren (meest naar minst).

Voorrang Operatoren Betekenis
1 … ** … machtsverheffing (Eng. exponentiation)
2 ~… bitsgewijze NOT (Eng. bitwise NOT)
3 +…
-…
positief (Eng. positive)
negatief (Eng. negative)
4 … * …
… @ …
… / …
… // …
… % …
vermenigvuldiging (Eng. multiplication)
matrixvermenigvuldiging (Eng. matrix multiplication)
deling (Eng. division)
geheeltallige deling (Eng. floor division, integer division)
modulo (Eng. remainder)
5 … + …
… - …
optelling (Eng. addition)
aftrekking (Eng. subtraction)
6 … << …
… >> …
bitverschuiving naar links (Eng. bit shift left)
bitverschuiving naar rechts (Eng. bit shift right)
7 … & … bitsgewijze AND (Eng. bitwise AND)
8 … ^ … bitsgewijze XOR (Eng. bitwise XOR)
9 … | … bitsgewijze OR (Eng. bitwise OR)
10 … in …
… not in …
… is …
… is not …
… == …
… != …
… < …
… <= …
… >= …
… > …
lidmaatschap (Eng. membership)
invers lidmaatschap (Eng. inverse membership)
identiteit (Eng. identity)
inverse identiteit (Eng. inverse indentity)
gelijkheid (Eng. equality)
inverse gelijkheid (Eng. inverse equality)
kleiner dan (Eng. less than)
kleiner dan of gelijk aan (Eng. less than or equal to)
groter dan of gelijk aan (Eng. greater than or equal to)
groter dan (Eng. greater than)
11 not … Booleaanse NOT (Eng. boolean NOT)
12 … and … Booleaanse XOR (Eng. boolean AND)
13 … or … Booleaanse OR (Eng. boolean OR)

# Rekenkundig

# Machtsverheffing






 
 

# ./python/syntax/operators/arithmetic__1.py
x = 2
y = -2
z = 3

print(z ** x)
print(z ** y)
1
2
3
4
5
6
7
$ python3 ./python/syntax/operators/arithmetic__1.py
9
0.1111111111111111
$ _

# Vermenigvuldiging, deling, geheeltallige deling, modulo (rest)






 
 

# ./python/syntax/operators/arithmetic__2.py
x = 2
y = -2
z = 3

print(x * z, x / z, x // z, x % z)
print(y * z, y / z, y // z, y % z)
1
2
3
4
5
6
7
$ python3 ./python/syntax/operators/arithmetic__2.py
6 0.6666666666666666 0 2
-6 -0.6666666666666666 -1 1
$ _

# Matrixvermenigvuldiging











 





# ./python/syntax/operators/arithmetic__3.py
from numpy import matrix

A = matrix([[1, 2],
            [3, 4]])
B = matrix([[6, 7],
            [8, 9]])

print(A)
print(B)
print(A @ B)
print(A[0, 0]*B[0, 0] + A[0, 1]*B[1, 0],
      A[0, 0]*B[0, 1] + A[0, 1]*B[1, 1],
      A[1, 0]*B[0, 0] + A[1, 1]*B[1, 0],
      A[1, 0]*B[0, 1] + A[1, 1]*B[1, 1])
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ python3 ./python/syntax/operators/arithmetic__3.py
[[1 2]
[3 4]]
[[6 7]
[8 9]]
[[22 25]
[50 57]]
22 25 50 57
$ _

# Optelling, aftrekking






 
 

# ./python/syntax/operators/arithmetic__4.py
x = 2
y = -2
z = 3

print(x + z, x - z)
print(y + z, y - z)
1
2
3
4
5
6
7
$ python3 ./python/syntax/operators/arithmetic__4.py
5 -1
1 -5
$ _

# Positief, negatief






 
 
 

# ./python/syntax/operators/arithmetic__5.py
x = 2
y = -2
z = 3

print(+x, -x)
print(+y, -y)
print(-(-z))
1
2
3
4
5
6
7
8
$ python3 ./python/syntax/operators/arithmetic__5.py
2 -2
-2 2
3
$ _

# Concatenering






 

# ./python/syntax/operators/concatenation.py
a = 'lorem'
b = ' '
c = 'ipsum'

print(a + b + c)
1
2
3
4
5
6
$ python3 ./python/syntax/operators/concatenation.py
lorem ipsum
$ _

# Vergelijking






 
 

# ./python/syntax/operators/comparison__1.py
x = 1
y = 1
z = True

print(x is y, x == y)
print(y is z, y == z)
1
2
3
4
5
6
7
$ python3 ./python/syntax/operators/comparison__1.py
True True
False True
$ _





 
 

# ./python/syntax/operators/comparison__2.py
x = 0
y = 0
z = False

print(x is not y, x != y)
print(y is not z, y != z)
1
2
3
4
5
6
7
$ python3 ./python/syntax/operators/comparison__2.py
False False
True False
$ _

# Logisch







 
 

# ./python/syntax/operators/logic__1.py
a = 'A'
z = 'z'
x = 1
y = 9.99

print(a < z, a > z)
print(x < y, x > y)
1
2
3
4
5
6
7
8
$ python3 ./python/syntax/operators/logic__1.py
True False
True False
$ _

# Toewijzing

# Assignment


 


# ./python/syntax/operators/assignment.py
v = 'I’m a variable.'
print(v)
1
2
3
$ python3 ./python/syntax/operators/assignment.py
I’m a variable.
$ _

# Reassignment



 
 


# ./python/operators/assignments.py
i = 0
i = 1
i += 1
print(i)
1
2
3
4
5
$ python3 ./python/syntax/operators/reassignment.py
2
$ _

# Conditionele expressie

Opmerking

De conditionele expressie wordt ook wel de ternaire operator genoemd.

De expressie … if … else … is snelschrift voor een if-statement.








 

# ./python/syntaxis/operators/conditional_expression.py
CONDITION = (
    False,
    True,
    )
is_true = CONDITION[0]

print('This is True.' if is_true else 'This is False.')
1
2
3
4
5
6
7
8
$ python3 
This is False.
$ _

Het bovenstaande script kan ook zoals hieronder geschreven worden.








 
 
 
 

# ./python/syntaxis/conditionals/if_else.py
CONDITION = (
    False,
    True,
    )
is_true = CONDITION[0]

if is_true:
    print('This is True.')
else:
    print('This is False.')
1
2
3
4
5
6
7
8
9
10
11
$ python3 
This is False.
$ _

# Bronnen

© 2024 Arteveldehogeschool Laatst bijgewerkt: 14/2/2020, 11:07:52