names_and_ages = {
'Ivan': 24,
'Elen': 21,
'Oleg': 18
}
names_and_ages = dict([
('Ivan', 24),
('Elen', 21),
('Oleg', 18)
])
names_and_ages = dict(
Ivan=24,
Elen=21,
Oleg=18
)
>>> names_and_ages = dict(Ivan=24, Elen=21, Oleg=18)
>>> names_and_ages
{'Ivan': 24, 'Elen': 21, 'Oleg': 18}
>>> names_and_ages['Elen']
21
>>> names_and_ages['Oleg']
18
>>> names_and_ages['Svetlana']
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
names_and_ages['Svetlana']
KeyError: 'Svetlana'
>>> names_and_ages['Svetlana'] = 19
>>> names_and_ages
{'Ivan': 24, 'Elen': 21, 'Oleg': 18, 'Svetlana': 19}
>>> names_and_ages['Oleg'] = 30
>>> names_and_ages
{'Ivan': 24, 'Elen': 21, 'Oleg': 30, 'Svetlana': 19}
del names_and_ages['Oleg']
>>> names_and_ages
{'Ivan': 24, 'Elen': 21, 'Svetlana': 19}
>>> langs = {'Питон': '3.8', 'Сишарп': '10'}
>>> langs
{'Питон': '3.8', 'Сишарп': '10'}
>>> langs.clear()
>>> langs
{}
>>> d = {'Пайтон': 3.8, 'Сишарп': 10, 'С++': 11}
>>> print(d.get('Пайтон'))
3.8
>>> print(d.get('Java'))
None
>>> d = {'Пайтон': 3.8, 'Сишарп': 10, 'С++': 11}
>>> print(d.get('Java', -1))
-1
>>> d = {'Питон': 10, 'Паскаль': 20, 'Бейсик': 30}
>>> d
{'Питон': 10, 'Паскаль': 20, 'Бейсик': 30}
>>> list(d.items())
[('Питон', 10), ('Паскаль', 20), ('Бейсик', 30)]
>>> list(d.items())[1][0]
'Паскаль'
>>> list(d.items())[1][1]
20
>>> d = {'Питон': 10, 'Паскаль': 20, 'Бейсик': 30}
>>> d
{'Питон': 10, 'Паскаль': 20, 'Бейсик': 30}
>>> list(d.keys())
['Питон', 'Паскаль', 'Бейсик']
>>> d = {'Питон': 10, 'Паскаль': 20, 'Бейсик': 30}
>>> d
{'Питон': 10, 'Паскаль': 20, 'Бейсик': 30}
>>> list(d.values())
[10, 20, 30]
>>> d = {'Питон': 10, 'Паскаль': 20, 'Бейсик': 30}
>>> d
{'Питон': 10, 'Паскаль': 20, 'Бейсик': 30}
>>> d.pop('Питон')
10
>>> d
{'Паскаль': 20, 'Бейсик': 30}
>>> d.pop('Котлин')
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
d.pop('Котлин')
KeyError: 'Котлин'
>>> d = {'Питон': 10, 'Паскаль': 20, 'Бейсик': 30}
>>> d
{'Питон': 10, 'Паскаль': 20, 'Бейсик': 30}
>>> d.popitem()
('Бейсик': 30)
>>> d
{'Питон': 10, 'Паскаль': 20}
>>> de = {}
>>> de.popitem()
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
de.popitem()
KeyError: 'popitem(): dictionary is empty'
>>> dict1 = {'a': 1, 'b': 2, 'c': 3}
>>> dict2 = {'b': 20, 'd': 40}
>>> dict1.update(dict2)
>>> dict1
{'a': 1, 'b': 20, 'c': 3, 'd': 40}