5.1. Conditional Operators¶
5.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)
5.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
5.1.3. Greater Than¶
>
-gt
(greater than)Set uses
>
forset.issuperset()
>>> '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=9, micro=1, 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, 1)
True
>>> sys.version_info > (3, 9, 2)
False
5.1.5. Operator Precedence¶
Operator |
Description |
---|---|
|
Lambda expression |
|
Conditional expression |
|
Boolean AND |
|
Boolean OR |
|
Boolean 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 |
|
Positive, negative, bitwise NOT |
|
Await expression |
|
Subscription, slicing, call, attribute reference |
|
Binding or tuple display, list display, dictionary display, set display |
5.1.6. Assignments¶
"""
* Assignment: Conditional Operators Modulo
* 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. 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`
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:
TODO: Input Stub
>>> type(result)
<class 'bool'>
>>> result in (True, False)
True
"""
# Given
number = input('What is your number?: ')