0% found this document useful (0 votes)
43 views26 pages

School of Computing and Information Technology: Course Delivery

The document provides an overview of dictionaries in Python. It defines dictionaries as unordered sets of key-value pairs and demonstrates how to create, access, modify, copy and perform other operations on dictionaries. It also discusses the None value in Python which represents a null value.

Uploaded by

yogi yogendra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
43 views26 pages

School of Computing and Information Technology: Course Delivery

The document provides an overview of dictionaries in Python. It defines dictionaries as unordered sets of key-value pairs and demonstrates how to create, access, modify, copy and perform other operations on dictionaries. It also discusses the None value in Python which represents a null value.

Uploaded by

yogi yogendra
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 26

School of Computing and Information Technology

Course Delivery

B. Tech – VII Semester (BTCS15F7530)


Programming with Python
By

Prof. Supreeth S
Assistant Professor
supreeth.s@reva.edu.in
Unit-1
2
DICTIONARIES

• A dictionary is an unordered set of key-value pairs.


When you add a key to a dictionary, you must also
add a value for that key.

• In Python dictionaries are written with curly


brackets, and they have keys and values.
Creating a Dictionary
dict = {
  "apple": "green",
  "banana": "yellow",
  "cherry": "red"
}

>>> 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

>>> 1000 in SUFFIXES


True

>>> 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

>>> print(sorted(squares)) # Output: [1, 3, 5, 7, 9]

>>> squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}


>>> for i in squares:
print(squares[i])

Output: 1 9 81 25 49
Removing Items – pop()

>>> x={'brand':"Ford", 'model':"Mustang", 'year':1964}


>>> x
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
>>> x.pop()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: pop expected at least 1 arguments, got 0

>>> x.pop('model')
'Mustang'
>>> x
{'brand': 'Ford', 'year': 1964}
Removing Items – popitem()

>>> x={'brand':"Ford", 'model':"Mustang", 'year':1964}


>>> x.popitem()
('year', 1964)
>>> x
{'brand': 'Ford', 'model': 'Mustang'}

>>> 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

>>> x={'brand':"Ford", 'model':"Mustang", 'year':1964}


>>> del x['model']
>>> x
{'brand': 'Ford', 'year': 1964}

>>> del x

>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined
Removing Items – clear()

>>> x={'brand':"Ford", 'model':"Mustang", 'year':1964}


>>> x.clear()
>>> x
{}
Copy a Dictionary
>>> x={'brand':"Ford", 'model':"Mustang", 'year':1964}
>>> y=x
>>> y Its not copying, its just referencing,
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
>>> y.popitem()
('year', 1964)
>>> y
{'brand': 'Ford', 'model': 'Mustang'}
>>> x
{'brand': 'Ford', 'model': 'Mustang'}
>>> x.popitem()
('model', 'Mustang')
>>> x
{'brand': 'Ford'}
>>> y
{'brand': 'Ford'}
>>> x={'brand':"Ford", 'model':"Mustang", 'year':1964}
>>> y=x.copy()
>>> x.popitem()
('year', 1964)
>>> x
{'brand': 'Ford', 'model': 'Mustang'}
>>> y
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
>>> x={'brand':"Ford", 'model':"Mustang", 'year':1964}
>>> z=dict(x)
>>> z
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
>>> x.popitem()
('year', 1964)
>>> x
{'brand': 'Ford', 'model': 'Mustang'}
>>> z
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964}
SUFFIXES = {1000: ['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
1024: ['KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']}

>>> len(SUFFIXES)
2 Mixed-Value Dictionaries

>>> 1000 in SUFFIXES


True

>>> 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

print(sorted(squares)) # Output: [1, 3, 5, 7, 9]


Other methods – keys(), values()
car = { car = {
"brand": "Ford", "brand": "Ford",
"model": "Mustang", "model": "Mustang",
"year": 1964 "year": 1964
} }

x = car.keys() x = car.values()

print(x) print(x)

dict_keys(['brand', 'model', 'year'])

dict_values(['Ford', 'Mustang', 1964])


Other methods – items()
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}

x = car.items()

print(x)
Output:
dict_items([('brand', 'Ford'), ('model', 'Mustang'), ('year', 1964)])
• Do you just need an ordered sequence of items?

• Do you just need to know whether or not you've


already got a particular value, but without ordering?

• you don't need to store duplicates?

• Do you need to associate values with keys, so you can


look them up efficiently (by key) later on?
Dictionary Methods
Method Description
clear() Removes all the elements from the dictionary
copy() Returns a copy of the dictionary
get() Returns the value of the specified key
items() Returns a list containing the a tuple for each key value pair
keys() Returns a list containing the dictionary's keys
pop() Removes the element with the specified key
popitem() Removes the last inserted key-value pair
update() Updates the dictionary with the specified key-value pairs
values() Returns a list of all the values in the dictionary
None
None is a special constant in Python.
A null value.
None is not the same as False.
None is not 0.
None is not an empty string.
It has its own datatype (NoneType).
You can assign None to any variable, but you can not create
other NoneType objects.

All variables whose value is None are equal to each other

Comparing None to anything other than None will always return False.
>>> type(None)
<class 'NoneType'>

>>> None == False


False

>>> None == 0
False

>>> None == ''


False
>>> None == None
True

>>> x = None
>>> x == None
True

>>> y = None
>>> x == y
True
Thank You

You might also like