5.1. Conditional Operators¶

5.1.1. Equals¶

• == - eq (equals)

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

Listing 5.2. Comparing tuple
(1, 2, 3) == (1, 2)             # False
(1, 2) == (1, 2)                # True
(1, 2) == (2, 1)                # False

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

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

(1,2) == [1,2]                  # False
(1,2) == {1,2}                  # False
1,2 == {1,2}                    # (1, False)


5.1.2. Not-Equals¶

• != - ne (not-equals)

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

Listing 5.6. Comparing tuple
(1, 2, 3) != (1, 2)             # True

Listing 5.7. Comparing list
[1, 2, 3] != [1, 2]             # True

Listing 5.8. Comparing set
{1, 2, 3} != {1, 2}             # True


5.1.3. Greater Than¶

• > - gt (greater than)

• Set uses > for set.issuperset(). More information in Sequence Set

'a' > 'b'       # False
'b' > 'a'       # True

'abc' > 'ab'    # True
'abc' > 'abc'   # False
'abc' > 'abcd'  # False

'def' > 'abc'   # True
'abc' > 'xy'    # False
'abc' > 'xyz'   # False

(3, 9) > (3, 8)         # True
(3, 8, 3) > (3, 7, 6)   # True
(3, 8) > (3, 9)         # False

(2, 7) > (3, 6)         # False
(3, 6) > (2, 7)         # True

[3, 9] > [3, 8]         # True
[3, 8, 3] > [3, 7, 6]   # True
[3, 8] > [3, 9]         # False

[2, 7] > [3, 6]         # False
[3, 6] > [2, 7]         # True


5.1.4. Examples¶

import sys

print(sys.version_info)
# sys.version_info(major=3, minor=8, micro=3, releaselevel='final', serial=0)

sys.version_info > (3, 7)       # True
sys.version_info > (3, 8)       # True
sys.version_info > (3, 9)       # False

sys.version_info > (3, 8, 2)    # True
sys.version_info > (3, 8, 3)    # True
sys.version_info > (3, 8, 4)    # False


5.1.5. 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

+, -

+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.6. Assignments¶

5.1.6.1. Conditional Operators Modulo¶

• Assignment name: Conditional Operators Modulo

• Last update: 2020-10-01

• Complexity level: easy

• Lines of code to write: 3 lines

• Estimated time of completion: 3 min

English
1. Read a number from user

2. User will input int and will not try to input invalid data

3. Define result: bool with parity check of input number

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

5. Print result

6. Do not use if statement

Polish
1. Wczytaj liczbę od użytkownika

2. Użytkownika poda int i nie będzie próbował wprowadzać niepoprawnych danych

3. Zdefiniuj result: bool z wynikiem sprawdzania parzystości liczby wprowadzonej

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

5. Wypisz result

6. Nie używaj instrukcji if

The whys and wherefores
• % has different meaning for int and str