0% found this document useful (0 votes)
11 views

Dictionaries 1 2

Uploaded by

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

Dictionaries 1 2

Uploaded by

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

Dictionaries Need for Dictionaries, Basics

of Dictionaries, creating a Dictionary,


Adding and Replacing Values, retrieving
values, Formatting Dictionaries, Deleting
Items, Comparing Two Dictionaries, The
Methods of Dictionary Class, Traversing
Dictionaries, Nested Dictionaries, Simple
Programs in Dictionaries, and Polynomials
as 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?
• Values in a dictionary can be of any data
type and can be duplicated, whereas keys
can’t be repeated and must be immutable.
Creating a Dictionary
• In 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.
We can create dictionaries by using a built
in function called dict().
Creating dictionary using dict()
function
Accessing the dictionary values
• To access data keys of the dictionary can
be used to obtain the values

Case sensitive
The dictionary is a mutable data type, and utilising
the right keys allows you to change its values.
Dict[key] = value and the value can both be
modified.
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
Replacing values to a dictionary

Adding values to a dictionary


Adding and replacing values to a
dictionary
Replacing value of dictionary
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 Elements using pop() Method

The pop() method is one of the ways to get


rid of elements from a 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
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.
Formatting dictionaries
• The % operator is used to substitute values
from a dictionary , into a string by using key.
PROPERTIES OF DICTIONARIES
• In the dictionary, we cannot store multiple values for the same
keys. If we pass more than one value for a single key, then the
value which is last assigned is considered as the value of the key.
The key cannot belong to any mutable object in Python. Numbers, strings, or tuples can
be used as the key, however mutable objects like lists cannot be used as the key in a
dictionary.
DICTIONARY METHODS
COPY()

CLEAR()
Items()

keys()

values()
get()

pop()

popitem()
fromkeys()

update()
setdefault()
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