# 6.13. Boolean Operator¶

## 6.13.1. Equals¶

• == - eq (equals)

Comparing str:

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


Comparing tuple:

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


Comparing list:

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


Comparing set:

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


Comparing types and values:

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


## 6.13.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


## 6.13.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


## 6.13.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)


## 6.13.5. Examples¶

>>> import sys
>>>
>>>
>>> print(sys.version_info)
sys.version_info(major=3, minor=10, micro=4, releaselevel='final', serial=0)
>>>
>>>
>>> sys.version_info >= (3, 9)
True
>>>
>>> sys.version_info >= (3, 10)
True
>>>
>>> sys.version_info >= (3, 11)
False
>>>
>>> sys.version_info >= (3, 12)
False
>>>
>>> sys.version_info >= (3, 9, 0)
True
>>>
>>> sys.version_info >= (3, 10, 0)
True
>>>
>>> sys.version_info >= (3, 11, 0)
False
>>>
>>> sys.version_info >= (3, 12, 0)
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.8.0'
>>>
>>> myversion >= required
True

>>> myversion = '3.10.0'
>>> required = '3.8.0'
>>>
>>> myversion >= required
False

>>> myversion = '3.9.0'.split('.')
>>> required = '3.8.0'.split('.')
>>>
>>> myversion >= required
True


## 6.13.6. Operator Precedence¶

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

## 6.13.7. To If or not to If¶

>>> number = 10
>>>
>>> if number % 2 == 0:
...     is_even = True
... else:
...     is_even = False
>>>
>>> print(is_even)
True

>>> number = 10
>>> is_even = True if number % 2 == 0 else False
>>>
>>> print(is_even)
True

>>> number = 10
>>> is_even = number % 2 == 0
>>>
>>> print(is_even)
True

>>> number = 10
>>> is_even = (number % 2 == 0)
>>>
>>> print(is_even)
True


## 6.13.8. Assignments¶

"""
* 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:
* int()
* %
* ==
* % 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 your 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?: ')

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