72 lines
2.1 KiB
Python
72 lines
2.1 KiB
Python
destinations = {
|
|
"Pyongyang": 5,
|
|
"Havana": 2,
|
|
"Caracas": 7
|
|
}
|
|
|
|
destinations["Moscow"] = 0
|
|
destinations["Ho-Chi-Minh Town"] = 0
|
|
|
|
def add_destination(destinations: dict, town: str, number: int):
|
|
if town not in destinations:
|
|
destinations[town] = 0
|
|
|
|
def add_reservation(destinations: dict, town: str, number: int):
|
|
if town in destinations:
|
|
destinations[town] += number
|
|
else:
|
|
destinations[town] = number
|
|
|
|
def remove_reservation(destinations: dict, town: str):
|
|
if town in destinations:
|
|
new_destinations = dict(destinations)
|
|
del new_destinations[town]
|
|
return new_destinations
|
|
|
|
def show_destinations_available(destinations: dict):
|
|
for town in destinations:
|
|
if destinations[town] != 0:
|
|
print(town + " : " + str(destinations[town]))
|
|
|
|
def show_destinations_by_reservations(destinations: dict):
|
|
print(sorted(destinations)) # ["Pyongyang", "Havana", "Caracas"]
|
|
print(sorted(destinations.keys())) # ["Pyongyang", "Havana", "Caracas"]
|
|
print(sorted(destinations.values)) # [5, 2, 7]
|
|
print(sorted(destinations.items)) # [("Pyongyang", 5), ("Havana", 2), ("Caracas", 7)]
|
|
|
|
def return_second_tuple_element(tuple):
|
|
return tuple[1]
|
|
|
|
print(sorted(destinations.items(), key= return_second_tuple_element)) # [('Moscow', 0), ('Ho-Chi-Minh Town', 0), ('Havana', 2), ('Pyongyang', 5), ('Caracas', 7)]
|
|
print(sorted(destinations.items(), key= lambda tuple : tuple[1])) # [('Moscow', 0), ('Ho-Chi-Minh Town', 0), ('Havana', 2), ('Pyongyang', 5), ('Caracas', 7)]
|
|
|
|
#show_destinations_available(destinations)
|
|
|
|
# Données déstructurées
|
|
|
|
dc_infos = ("Dale", "Cooper", "32")
|
|
dc_prenom = dc_infos[0]
|
|
dc_nom = dc_infos[1]
|
|
dc_age = dc_infos[2]
|
|
|
|
dc_prenom, dc_nom, dc_age = dc_infos
|
|
|
|
def generator():
|
|
for i in range(10):
|
|
yield i
|
|
|
|
def square_generator(n):
|
|
for i in range(n):
|
|
yield i ** 2
|
|
|
|
print(generator())
|
|
squares = square_generator(10)
|
|
|
|
print(next(squares))
|
|
print(next(squares))
|
|
print(next(squares))
|
|
print(next(squares))
|
|
print(next(squares))
|
|
print(next(squares))
|
|
print(next(squares))
|
|
print(next(squares)) |