Operators#
Arithmetic operators#
Unary operators
Operator |
Name |
Example |
---|---|---|
|
Addition |
|
|
Subtraction |
|
|
Multiplication |
|
|
Division |
|
|
Modulus |
|
|
Exponentiation |
|
|
Floor division |
|
Comparison operators#
Operator |
Name |
Example |
---|---|---|
|
Equal |
|
|
Not equal |
|
|
Greater than |
|
|
Less than |
|
|
Greater than or equal to |
|
|
Less than or equal to |
|
Logical operators#
Operator |
Description |
Example |
---|---|---|
|
Returns True if both statements are true |
|
|
Returns True if one of the statements is true |
|
|
Reverse the result, returns False if the result is true |
|
Identity operators#
Operator |
Description |
Example |
---|---|---|
|
Returns True if both variables are the same object |
|
|
Returns True if both variables are not the same object |
|
Membership operators#
Operator |
Description |
Example |
---|---|---|
|
Returns True if a sequence with the specified value is present in the object |
|
|
Returns True if a sequence with the specified value is not present in the object |
|
Bitwise operators#
A Bitwise operator operates on a bit string, a bit array or binary numeral and are basic actions that can directly be done by the processor itself. The processor uses the basic [boolean operators](#84-boolean-operators-basics) on bit level and can shift the bit string.
Operator |
Name |
Description |
---|---|---|
|
AND |
Sets each bit to 1 if both bits are 1 |
|
OR |
Sets each bit to 1 if one of two bits is 1 |
|
XOR |
Sets each bit to 1 if only one of two bits is 1 |
|
NOT |
Inverts all the bits |
|
Zero fill left shift |
Shift left by pushing zeros in from the right and let the leftmost bits fall off |
|
Signed right shift |
Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
The first example is applying AND on two signed integers. The processor converts the integers to bit strings and then applies the AND-table to every position between the two string. For 3 & 4 the result is 0 as there are no overlapping bits between the two integers. For 3 & 7 the result is 3 as the bits for number 3 are also in number 7 and will result matching number 3.
1#!/usr/bin/env python3
2
3def main():
4 # 3 = 0000 0011
5 # 4 = 0000 0100 AND
6 # 0 = 0000 0000
7 print(3 & 4)
8 # 3 = 0000 0011
9 # 7 = 0000 0111 AND
10 # 3 = 0000 0011
11 print(3 & 7)
12
13
14if __name__ == "__main__":
15 main()
The next example is applying OR on two signed integers. The processor converts the integers to bit strings and then applies the OR-table to every position between the two string. For 3 | 4
the result is 7
as all bits that are 1
will end up in the result. For 3 | 7
the result is also 7
as the bits for number 3 are also in number 7 and will result matching number 7.
1#!/usr/bin/env python3
2
3def main():
4 # 3 = 0000 0011
5 # 4 = 0000 0100 OR
6 # 7 = 0000 0111
7 print(3 | 4)
8 # 3 = 0000 0011
9 # 7 = 0000 0111 OR
10 # 7 = 0000 0111
11 print(3 | 7)
12
13
14if __name__ == "__main__":
15 main()
The next example is applying XOR on two signed integers. The processor converts the integers to bit strings and then applies the XOR-table to every position between the two string. For 3 | 4
the result is 7
as all bits that are 1
will end up in the result. For 3 | 7
the result is also 7
as the bits for number 3 are also in number 7 and will result matching number 7.
1#!/usr/bin/env python3
2
3def main():
4 # 3 = 0000 0011
5 # 4 = 0000 0100 XOR
6 # 7 = 0000 0111
7 print(3 ^ 4)
8 # 3 = 0000 0011
9 # 7 = 0000 0111 XOR
10 # 4 = 0000 0100
11 print(3 ^ 7)
12
13
14if __name__ == "__main__":
15 main()
1#!/usr/bin/env python3
2
3def main():
4 # 3 = 0000 0011 NOT
5 # -4 = 1111 1100
6 print(~3)
7 # 127 = 0111 1111 NOT
8 # -128 = 1000 0000
9 print(~127)
10
11
12if __name__ == "__main__":
13 main()
1#!/usr/bin/env python3
2
3def main():
4 # 3 = 0000 0011 SHIFT RIGHT 1
5 # 1 = 0000 0001
6 print(3 >> 1)
7 # Shift left
8 # 3 = 0000 0011 SHIFT LEFT 1
9 # 6 = 0000 0110
10 print(3 << 1)
11
12
13if __name__ == "__main__":
14 main()
Assignment operators#
Operator |
Description |
Example |
Long form |
---|---|---|---|
|
Assignment |
|
|
|
Arithmetic Addition |
|
|
|
Arithmetic Subtraction |
|
|
|
Arithmetic Multiplication |
|
|
|
Arithmetic Division |
|
|
|
Arithmetic Modulus |
|
|
|
Arithmetic Exponentiation |
|
|
|
Arithmetic Floor division |
|
|
|
Bitwise AND |
|
|
|
Bitwise OR |
|
|
|
Bitwise XOR |
|
|
|
Bitwise Shift Left |
|
|
|
Bitwise Shift Right |
|
|
Operator Precedence#
Highest precedence at top, lowest at bottom.
Operators in the same box evaluate left to right.
Operator |
Description |
---|---|
|
Parentheses (grouping) |
|
Function call |
|
Slicing |
|
Subscription |
|
Attribute reference |
|
Exponentiation |
|
Bitwise not |
|
Positive, negative |
|
Multiplication, division, remainder |
|
Addition, subtraction |
|
Bitwise shifts |
|
Bitwise AND |
|
Bitwise XOR |
|
Bitwise OR |
|
Comparisons, membership, identity |
|
Boolean NOT |
|
Boolean AND |
|
Boolean OR |
|
Lambda expression |