7.1. Conditional Operator

7.1.1. Equals

  • == - eq (equals)

Comparing str:

>>> 'Monty Python' == 'Python'
False
>>> 'Python' == 'Python'
True
>>> 'python' == 'Python'
False

Comparing tuple:

>>> (1, 2, 3) == (1, 2)
False
>>> (1, 2) == (1, 2)
True
>>> (1, 2) == (2, 1)
False

Comparing list:

>>> [1, 2, 3] == [1, 2]
False
>>> [1, 2] == [1, 2]
True
>>> [1, 2] == [2, 1]
False

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)

7.1.2. Not-Equals

  • != - ne (not-equals)

Comparing str:

>>> 'Monty Python' != 'Python'
True
>>> 'Python' != 'Python'
False
>>> 'python' != 'Python'
True

Comparing tuple:

>>> (1, 2, 3) != (1, 2)
True

Comparing list:

>>> [1, 2, 3] != [1, 2]
True

Comparing set:

>>> {1, 2, 3} != {1, 2}
True

7.1.3. Greater Than

  • > - gt (greater than)

  • Set uses > for set.issuperset()

>>> 'a' > 'b'
False
>>> 'b' > 'a'
True
>>> 'abc' > 'ab'
True
>>> 'abc' > 'abc'
False
>>> 'abc' > 'abcd'
False
>>> 'def' > 'abc'
True
>>> 'abc' > 'xy'
False
>>> 'abc' > 'self'
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

7.1.4. Problems

>>> 1, 2 == (1, 2)
(1, False)
>>> 1
1
>>> 2 == (1, 2)
False
>>> 1,     2==(1,2)
(1, False)
>>> (1, 2) == 1, 2
(False, 2)

7.1.5. Examples

>>> import sys
>>>
>>> print(sys.version_info)
sys.version_info(major=3, minor=9, micro=7, releaselevel='final', serial=0)
>>>
>>> sys.version_info > (3, 8)
True
>>> sys.version_info > (3, 9)
True
>>> sys.version_info > (3, 10)
False
>>>
>>> sys.version_info > (3, 9, 0)
True
>>> sys.version_info > (3, 9, 3)
True
>>> sys.version_info >= (3, 9, 4)
True
>>> sys.version_info > (3, 9, 15)
False
>>> '3.8.0' > '3.9.0'
False
>>> '3.9.0' > '3.10.0'
True
>>> '3.09.0' > '3.10.0'
False
>>> myversion = '3.9.0'
>>> required = '3.7.0'
>>>
>>> myversion >= required
True
>>> myversion = '3.9.0'.split('.')
>>> required = '3.7.0'.split('.')
>>>
>>> myversion >= required
True

7.1.6. Operator Precedence

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

7.1.7. Assignments

Code 7.1. Solution
"""
* Assignment: Conditional Operator Modulo
* Required: yes
* Complexity: easy
* Lines of code: 3 lines
* Time: 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. Do not use `if` statement
    6. Run doctests - all must succeed

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. Nie używaj instrukcji `if`
    6. Uruchom doctesty - wszystkie muszą się powieść

Hints:
    * `%` has different meaning for `int` and `str`
    * `%` on `str` is overloaded as a string formatting
    * `%` on `int` is overloaded as a modulo division

Tests:
    >>> import sys; sys.tracebacklimit = 0

    >>> assert result is not Ellipsis, \
    'Assign result to variable: `result`'

    >>> assert type(result) is bool, \
    'Variable `result` has invalid type, should be bool'

    >>> result
    True
"""

from unittest.mock import MagicMock


# Simulate user input (for test automation)
input = MagicMock(side_effect=['4'])

number = input('What is your number?: ')

# bool: Whether input number is even or odd (modulo divide)
result = ...