Operators

Arithmetic operators

Unary operators

Operator

Name

Example

+

Addition

x + y

-

Subtraction

x - y

*

Multiplication

x * y

/

Division

x / y

%

Modulus

x % y

**

Exponentiation

x ** y

//

Floor division

x // y

Comparison operators

Operator

Name

Example

==

Equal

x == y

!=

Not equal

x != y

>

Greater than

x > y

<

Less than

x < y

>=

Greater than or equal to

x >= y

<=

Less than or equal to

x <= y

Logical operators

Operator

Description

Example

and

Returns True if both statements are true

x < 5 and  x < 10

or

Returns True if one of the statements is true

x < 5 or x < 4

not

Reverse the result, returns False if the result is true

not(x < 5 and x < 10)

Identity operators

Operator

Description

Example

is

Returns True if both variables are the same object

x is y

is not

Returns True if both variables are not the same object

x is not y

Membership operators

Operator

Description

Example

in

Returns True if a sequence with the specified value is present in the object

x in y

not in

Returns True if a sequence with the specified value is not present in the object

x not in y

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.

Example:

 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.

Example:

 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.

Example:

 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()

Example:

 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()

Example:

 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

x = 2

x = 2

+=

Arithmetic Addition

x += 2

x = x + 2

-=

Arithmetic Subtraction

x -= 2

x = x - 2

*=

Arithmetic Multiplication

x *= 2

x = x * 2

/=

Arithmetic Division

x /= 2

x = x / 2

%=

Arithmetic Modulus

x %= 2

x = x % 2

//=

Arithmetic Exponentiation

x //= 2

x = x // 2

**=

Arithmetic Floor division

x **= 2

x = x ** 2

&=

Bitwise AND

x &= 2

x = x & 2

|=

Bitwise OR

x |= 2

x = x | 2

^=

Bitwise XOR

x ^= 2

x = x ^ 2

>>=

Bitwise Shift Left

x >>= 2

x = x >> 2

<<=

Bitwise Shift Right

x <<= 2

x = x << 2

Operator Precedence

  • Highest precedence at top, lowest at bottom.

  • Operators in the same box evaluate left to right.

Operator

Description

()

Parentheses (grouping)

f(args...)

Function call

x[index:index]

Slicing

x[index]

Subscription

x.attribute

Attribute reference

**

Exponentiation

~x

Bitwise not

+x, -x

Positive, negative

*, /, %

Multiplication, division, remainder

+, -

Addition, subtraction

<<, >>

Bitwise shifts

&

Bitwise AND

^

Bitwise XOR

|

Bitwise OR

in, not in, is, is not, <, <=, >, >=, <>, !=, ==

Comparisons, membership, identity

not x

Boolean NOT

and

Boolean AND

or

Boolean OR

lambda

Lambda expression