Код:
students = {
"Petrov": 16,
"Sidorov": 17,
"Ivanov": 18,
"Melnikov": 19,
"Vasiliev": 20,
"Kuznetsov": 16,
"Popov": 17,
"Golovin": 18,
"Zubkov": 19,
"Bogdanov": 20
}
# Increases the age of all students by 1
for key in students:
students[key] += 1
# If the age is more than 17, reduce it by 2
if students[key] > 17:
students[key] -= 2
# Delete an element from the dictionary if the student is 18 years old
for key in list(students.keys()):
if students[key] == 18:
del students[key]
# Display the dictionary
print(students)
# Display the values of dictionary elements for which the age value is 16
result = {key: value for key, value in students.items() if value == 16}
print(result)
Вывод:
{'Petrov': 16, 'Sidorov': 16, 'Melnikov': 17, 'Vasiliev': 18, 'Kuznetsov': 16, 'Popov': 16, 'Zubkov': 17, 'Bogdanov': 18}
{'Petrov': 16, 'Kuznetsov': 16, 'Popov': 16}
Copyright © 2024 SCHOLAR.TIPS - All rights reserved.
Answers & Comments
Код:
students = {
"Petrov": 16,
"Sidorov": 17,
"Ivanov": 18,
"Melnikov": 19,
"Vasiliev": 20,
"Kuznetsov": 16,
"Popov": 17,
"Golovin": 18,
"Zubkov": 19,
"Bogdanov": 20
}
# Increases the age of all students by 1
for key in students:
students[key] += 1
# If the age is more than 17, reduce it by 2
for key in students:
if students[key] > 17:
students[key] -= 2
# Delete an element from the dictionary if the student is 18 years old
for key in list(students.keys()):
if students[key] == 18:
del students[key]
# Display the dictionary
print(students)
# Display the values of dictionary elements for which the age value is 16
result = {key: value for key, value in students.items() if value == 16}
print(result)
Вывод:
{'Petrov': 16, 'Sidorov': 16, 'Melnikov': 17, 'Vasiliev': 18, 'Kuznetsov': 16, 'Popov': 16, 'Zubkov': 17, 'Bogdanov': 18}
{'Petrov': 16, 'Kuznetsov': 16, 'Popov': 16}