study-python-fastapi/Hometask4.py
2024-12-01 10:54:32 +00:00

50 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Списки
# 1. Робота із списками
numbers = [1, 2, 3, 4, 5]
numbers.append(10)
numbers.append(20)
numbers.remove(10)
print(numbers) # Output: [1, 2, 3, 4, 5, 20]
# 2. Знаходження суми
numbers = [1, 2, 3, 4, 5]
sum_of_numbers = sum(numbers)
print(sum_of_numbers) # Output: 15
# 3. Подвійні значення
numbers = [1, 2, 3, 4, 5]
doubled_numbers = [x * 2 for x in numbers]
print(doubled_numbers) # Output: [2, 4, 6, 8, 10]# Кортежі
# 1. Робота із кортежами
fruits = ("яблуко", "банан", "апельсин")
for fruit in fruits:
print(fruit)
# 2. Об'єднання кортежів
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
combined_tuple = tuple1 + tuple2
print(combined_tuple) # Output: (1, 2, 3, 4, 5, 6)# Словники
# 1. Робота із словниками
athlete = {
"ім'я": "Ліонель Мессі",
"вік": 34,
"спорт": "футбол",
"команда": "Інтер Маямі",
}
for key, value in athlete.items():
print(f"{key}: {value}")
# 2. Оновлення словника
books = {"1984": 1949, "To Kill a Mockingbird": 1960}
books["The Great Gatsby"] = 1925
print(books)
# 3. Пошук значення
countries = {"Україна": "Київ", "Франція": "Париж", "Німеччина": "Берлін"}
country = "Україна" # Змінити для перевірки різних випадків
capital = countries.get(country, "Країна не знайдена")
print(capital) # Output: "Київ"