School of Computing and Information Technology: Course Delivery
School of Computing and Information Technology: Course Delivery
Course Delivery
Prof. Supreeth S
Assistant Professor
supreeth.s@reva.edu.in
Unit-1
2
DICTIONARIES
>>> dict["apple"]
‘green‘
>>> dict["RED"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'RED'
>>> a_dict = {'server': 'db.diveintopython3.org',
'database': 'mysql'}
>>> a_dict
{'server': 'db.diveintopython3.org', 'database':
'mysql'}
>>> a_dict['server']
'db.diveintopython3.org'
>>> a_dict['database']
'mysql'
>>> a_dict['database'] = 'blog‘
>>> a_dict
{'server': 'db.diveintopython3.org', 'database': 'blog'}
>>> a_dict = {'server': 'db.diveintopython3.org', 'database': 'mysql‘,
‘admin’:’admin’, ‘age’:25}
>>> a_dict
{'server': 'db.diveintopython3.org',’age’:25,’admin’:’admin’, 'database': 'mysql'}
>>> a_dict['server']
'db.diveintopython3.org‘
>>> a_dict['database']
'mysql‘
>>> a_dict['db.diveintopython3.org']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'db.diveintopython3.org'
Modifying a Dictionary
>>>a_dict={'server':'db.diveintopython3.org','database':'blog'
}
>>> a_dict
{'server': 'db.diveintopython3.org', 'database': 'blog'}
>>> a_dict['user']='dora'
>>> a_dict
{'user': 'dora', 'server': 'db.diveintopython3.org', 'database':
'blog'}
>>> a_dict['user']='mark'
>>> a_dict
{'user': 'mark', 'server': 'db.diveintopython3.org', 'database':
'blog'}
Mixed Value Dictionaries
SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']}
>>> len(SUFFIXES)
2
>>> SUFFIXES[1000]
['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
>>> SUFFIXES[1024]
['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
>>> SUFFIXES[1000][3]
'TB'
>>> squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
>>> print(len(squares)) # Output: 5
Output: 1 9 81 25 49
Removing Items – pop()
>>> x.pop('model')
'Mustang'
>>> x
{'brand': 'Ford', 'year': 1964}
Removing Items – popitem()
>>> x.popitem()
('model', 'Mustang')
>>> x.popitem()
('brand', 'Ford')
>>> x.popitem()
Traceback (most recent call last):File "<stdin>", line 1, in <module>
KeyError: 'popitem(): dictionary is empty'
Removing Items – del
>>> del x
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
Removing Items – clear()
>>> len(SUFFIXES)
2 Mixed-Value Dictionaries
>>> SUFFIXES[1000]
['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
>>> SUFFIXES[1024]
['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
>>> SUFFIXES[1000][3]
'TB'
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
print(len(squares)) # Output: 5
x = car.keys() x = car.values()
print(x) print(x)
x = car.items()
print(x)
Output:
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])
• Do you just need an ordered sequence of items?
Comparing None to anything other than None will always return False.
>>> type(None)
<class 'NoneType'>
>>> None == 0
False
>>> x = None
>>> x == None
True
>>> y = None
>>> x == y
True
Thank You