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

Dictionaries

Uploaded by

Abhiram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views26 pages

Dictionaries

Uploaded by

Abhiram
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Dictionaries

What is a Dictionary?
• Dictionary in Python is an unordered collection of data
values, used to store data values like a map, which unlike
other Data Types that hold only single value as an element.
• Dictionary holds a pair of values, one being the Key and the
other corresponding pair element being its key-value.
What is a Dictionary?
• Dictionary in Python is an unordered collection of
data values, used to store data values like a map,
which unlike other Data Types that hold only
single value as an element.
• A dictionary is a collection which is unordered,
changeable and indexed
• Values in a dictionary can be of any data type and
can be duplicated, whereas keys can’t be
repeated and must be immutable.
• Key value is provided in the dictionary to make it
more optimized.
Creating a Dictionary

• n Python, a Dictionary can be created by


placing sequence of elements within curly
“{ }” braces, separated by ‘comma’.
• Dictionary keys are case sensitive, same name
but different cases of Key will be treated
distinctly.
Creating empty dictionary
Creating dictionary using dict() function
• We can create dictionaries by using a builtin
function called dict().
Adding and replacing values to a dictionary

• To add new items in to a dictionary , we can


use the subscript [] operator.
• Syntax: Dictioanry_name [key] = value
Adding and replacing values to a dictionary
Replacing value of dictionary
Formatting dictionaries

• The % operator is used to substitute values from a


dictionary , in to a string by using key.
Deleting items from dictionary
• We can delete any entry from dictionary.
• The “del” operator is used to remove key and
its associated value.
• If key is found in the dictionary then it is
removed otherwise python raises an error.
• Syntax: del dictioanry_name[key]
Deleting items from dictionary
Comparing two dictionaries
• The “==” operator is used to test if two
dictionaries contains the same items.
• It returns true two dictionaries contains same
items.
• Similarly the “!=” operator is used to test if
two dictionaries doesn’t contain same items.
• It returns true two dictionaries contains
different items.
Comparing two dictionaries
The methods of dictionary class
• keys() – return a sequence of keys from dictionary
The methods of dictionary class
• values() – return a sequence of vales from a
dictionary
The methods of dictionary class
• items() – Return a sequence of tuples
The methods of dictionary class
• get(key) – return the value for key.
The methods of dictionary class
• clear() – Delete all entries of a dictionary
Traversing dictionaries
• The for loop is used to traverse all keys and
values of a dictionary.
• The variable of the for loop is bound to each
key in an unspecified order.
• It means it retrieve the keys and values in any
order.
Traversing dictionaries using for loop
Nested dictionaries
• A dictionary with in a dictionary is called nested dictionary.
Traversing Nested dictionaries
Polynomials as dictionaries
• Dictionaries can change their content so they are
mutable in nature.
• The keys in a dictionary is not restricted to be strings.
• Any immutable object can be used as key.
• A common type of key used in dictionaries is integers.
• We can use dictionaries while calculating polynomial
equation.
• A dictionary is used to map a power to a coefficient.
let the equation be -2 + y^4 + 3y^6
The dictionary for the above equation is
{0:-2, 4:1, 6:3}
Write a python program to calculate the polynomial
equation -2 + y^4 + 3y^6

You might also like