⚡ Ответы на закрепление
bool() даёт True для: a, c, e, f, g, j.
- Сравнения: 1 — True, 2 — False, 3 — True, 4 — True.
True and False → False, True or False → True, not True → False.
not x > 3 при x = 5 → False.
not a or b and c при a=False, b=True, c=True → True.
Ответы на задания для закрепления
| Задание | Ответ | Почему |
| bool() | a, c, e, f, g, j | ненулевые числа и непустые строки истинны |
| Сравнения | 1 — b, 2 — a, 3 — b, 4 — b | b в source соответствует True, a — False |
| Логика 1 | 1 — b, 2 — a, 3 — b | and, or, not |
not x > 3 | b: False | x > 3 даёт True, затем not |
a < b and b < 10 | b: False | b < 10 ложно при b = 10 |
1 < x < 5 | a: True | 3 находится между 1 и 5 |
not a or b and c | a: True | not False даёт True |
Практика 1. Число в диапазоне
# 01_range_check.py
first_number = int(input("Введите первое число: "))
second_number = int(input("Введите второе число: "))
print(1 <= first_number <= second_number)
Практика 2. Сравнение чисел
# 02_compare_three_numbers.py
first_number = int(input("Введите первое число: "))
second_number = int(input("Введите второе число: "))
third_number = int(input("Введите третье число: "))
print("Первое больше второго:", first_number > second_number)
print("Первое меньше третьего:", first_number < third_number)
print("Первое равно третьему:", first_number == third_number)
Домашнее 1. Логические операции
# hw_01_boolean_operations.py
value1 = bool(int(input("Enter first value (1=True, 0=False): ")))
value2 = bool(int(input("Enter second value (1=True, 0=False): ")))
print("and:", value1 and value2)
print("or:", value1 or value2)
print("not:", not value1)
print("equal:", value1 == value2)
print("not equal:", value1 != value2)
Домашнее 2. Проверка условий
# hw_02_conditions.py
light_on = bool(int(input("Свет включён? (1=True, 0=False): ")))
window_open = bool(int(input("Окно открыто? (1=True, 0=False): ")))
print("Свет включён?", light_on)
print("Окно открыто?", window_open)
print("Оба условия выполнены?", light_on and window_open)
print("Хотя бы одно условие выполнено?", light_on or window_open)
Домашнее 3. Стоимость мобильного тарифа
# hw_03_mobile_tariff.py
used_mb = int(input("Введите использованные мегабайты: "))
base_price = 30
extra_mb = used_mb - 500
has_overuse = used_mb > 500
total_price = base_price + extra_mb * 0.2 * int(has_overuse)
print("Общая стоимость:", total_price)