5.1. Operators

5.1.1. Comparision

  • == - eq (equals)

  • != - ne (not-equals)

Listing 5.1. Comparing str
'Monty Python' == 'Python'      # False
'Monty Python' != 'Python'      # True

'Python' == 'Python'            # True
'Python' != 'Python'            # False

'python' == 'Python'            # False
'python' != 'Python'            # True
Listing 5.2. Comparing tuple
(1, 2, 3) == (1, 2)             # False
(1, 2, 3) != (1, 2)             # True

(1, 2) == (1, 2)                # True
(1, 2) == (2, 1)                # False
Listing 5.3. Comparing list
[1, 2, 3] == [1, 2]             # False
[1, 2, 3] != [1, 2]             # True

[1, 2] == [1, 2]                # True
[1, 2] == [2, 1]                # False
Listing 5.4. Comparing set
{1, 2, 3} == {1, 2}             # False
{1, 2, 3} != {1, 2}             # True

{1, 2} == {1, 2}                # True
{1, 2} == {2, 1}                # True

5.1.2. Contains

  • in checks whether value is in sequence

  • works with str, list, tuple, set, frozenset, dict

  • Computational complexity for checking if sequence "contains":

    • O(n) - in str

    • O(n) - in list

    • O(n) - in tuple

    • O(1) - in set

    • O(1) - in frozenset

    • O(1) - in dict

  • More information in Contains

Listing 5.5. list contains
data = 'abc'

'x' in data                      # False
'a' in data                      # True

'x' not in data                  # True
'a' not in data                  # False
Listing 5.6. list contains
data = [1, 2, 3]

0 in data                        # False
1 in data                        # True

0 not in data                    # True
1 not in data                    # False
Listing 5.7. tuple contains
data = (1, 2, 3)

0 in data                        # False
1 in data                        # True

0 not in data                    # True
1 not in data                    # False
Listing 5.8. set contains
data = {1, 2, 3}

0 in data                        # False
1 in data                        # True

0 not in data                    # True
1 not in data                    # False
Listing 5.9. frozenset contains
data = frozenset({1, 2, 3})

0 in data                        # False
1 in data                        # True

0 not in data                    # True
1 not in data                    # False
data = {
    'first_name': 'Jan',
    'last_name': 'Twardowski',
}

'first_name' in data            # True
'agency' in data                # False

'first_name' not in data        # False
'agency' not in data            # True

5.1.3. Operator Precedence

Table 5.1. Operator precedence

Operator

Description

lambda

Lambda expression

if, elif, else

Conditional expression

and

Boolean AND

or

Boolean OR

not x

Boolean NOT

in, not in, is, is not,

<, <=, >, >=, !=, ==

Comparisons, including membership tests and identity tests

|

Bitwise OR

^

Bitwise XOR

&

Bitwise AND

<<, >>

Shifts

**

Exponentiation

*, @, /, //, %

Multiplication, matrix multiplication, division, floor division, remainder

+, -

Addition and subtraction

+x, -x, ~x

Positive, negative, bitwise NOT

await

Await expression

x[index], x[index:index],

x(arguments...), x.attribute

Subscription, slicing, call, attribute reference

(expressions...), [expressions...],

{key: value...}, {expressions...}

Binding or tuple display, list display, dictionary display, set display

5.1.4. Assignments

5.1.4.1. Is even number

English
  1. Read a number from user

  2. User will pass only valid int

  3. Number is even, when divided modulo (%) by 2 reminder equal to 0

  4. Print whether number is odd

  5. Do not use if statement

Polish
  1. Wczytaj liczbę od użytkownika

  2. Użytkownika poda tylko poprawne int

  3. Liczba jest parzysta, gdy dzielona modulo (%) przez 2 ma resztę równą 0

  4. Wypisz czy liczba jest nieparzysta

  5. Nie używaj instrukcji if

The whys and wherefores
  • Reading input from user

  • Type casting

  • Print formatting

  • Numerical operators

Hints
  • % has different meaning for int and str