💻 Примеры из повторения

⚡ Мини-пример

students = [("Алексей", 80, 100), ("Екатерина", 40, 100)]
for name, points, max_points in students:
    percent = points / max_points * 100
    print(name, percent)

Кортеж и одноэлементный кортеж

# examples/tuple_basics.py
my_tuple = (10, 20, 30)
single = (5,)
print(len(my_tuple))
print(type(single))

Распаковка с остатком

# examples/star_unpacking.py
numbers = [1, 2, 3, 4, 5]
first, *other, last = numbers
print(first)
print(last)
print(other)

enumerate() для редактирования

# examples/enumerate_edit.py
numbers = [10, 20, 30, 40]
for index, value in enumerate(numbers):
    numbers[index] = value * 10
print(numbers)

Соседние элементы

# examples/neighbours.py
numbers = [10, 20, 15, 30, 25, 35]
for index, value in enumerate(numbers[:-1]):
    if value > numbers[index + 1]:
        print(f"{value} больше, чем {numbers[index + 1]}")

Отчёт по студентам

# examples/student_report.py
students = [
    ("Алексей", 80, 100),
    ("Мария", 50, 100),
    ("Дмитрий", 70, 100),
    ("Екатерина", 40, 100),
]

print(f"{'Имя':<15}{'Процент':>10}{'Статус':>12}")
print("-" * 37)
for name, points, max_points in students:
    percent = points / max_points * 100
    status = "Незачёт" if percent < 50 else "Зачёт"
    print(f"{name:<15}{percent:>10.2f}{status:>12}")

Анализ продаж

# examples/sales_analysis.py
sales = [("Bread", 20), ("Milk", 15), ("Eggs", 30), ("Butter", 10), ("Cheese", 25)]
total = 0
top_product = sales[0]

for product, amount in sales:
    total += amount
    if amount > top_product[1]:
        top_product = (product, amount)

print("Общее количество проданных товаров:", total)
print(f"Самый популярный товар: {top_product[0]} ({top_product[1]} шт.)")