0% found this document useful (0 votes)
3 views41 pages

Chapter Python Dictionary

Chapter Python Dictionary

Uploaded by

suni.tokas
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)
3 views41 pages

Chapter Python Dictionary

Chapter Python Dictionary

Uploaded by

suni.tokas
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/ 41

LEARNING OUTCOMES

LEARNING OUTCOMES
After studying this lesson, students
will be able to:

 Understand the need of dictionaries;


Solve problems by using dictionaries;
Get clear idea about dictionaries functions;
and
 Understand the difference between list and
dictionary.
WHAT IS DICTIONARY?
DICTIONARIES

A dictionary is an un­ordered collection of key­


value pairs.

A dictionary has a length, specifically the


number of key-­value pairs.

A dictionary provides fast look up by key.

The keys must be immutable object types.


WHAT IS DICTIONARY?

• It is just like English Dictionary where meaning is


associated with a word (Key).
• Dictionaries are containers that associate keys to
values.
• With list, tuples and strings we need to know the
index of individual element to access but with
Dictionaries user need to search value based on
key.
DICTIONARIES

Curly braces mark the beginning and end of


the dictionary

Each entry (key:value) consists of a pair


separated by a colon. The key and corresponding
value is given by writing colon(:) between them.

The key:value pairs are separated by comma.


Internally dictionaries are indexed (i.e.) arranged
on the basis of keys.
CREATING AND
ACCESSING LISTS
>>>Month={“January":31,"February":28,"M
arch":31,"April":30,"May
":31,"June":30,"July":31,"August":31,"Sept
ember":30,"October":31," November":30,
"December":31}

>>>teacher={'Bimlendu':'9470010804','Prak
ash':'8604774180’}
DICTIONARIES
Dictionaries are also called Associative Arrays
or mappings or hashes.

Keys of a dictionaries must be of immutable


type such as Python string, Number, a tuple
(containing only immutable entry) but list which
is mutable can not be used as keys of a
dictionary.

>>>dict2={[2,3]:”abc”} #TypeError:
Un-sharable Type ‘list’
STATE DIAGRAM

>>> A={1:"one",2:"two",3:"three"}

1 one
A= 2 two
3 three

KEYS VALUES
CREATING A DICTIONARY – dict()
CREATING DICTIONARAY – dict()

The function dict ( ) is used to create a


new dictionary with no items. This function is
called built-in function. We can also create
dictionary using {}.
CREATING DICTIONARAY – Example
CREATING AND TRAVERSING DICTIONARAY
CREATING AND TRAVERSING DICTIONARAY

OUT PUT
CREATING AND TRAVERSING DICTIONARAY

OUT PUT
DICTIONARAY – BUILT IN METHODS
DICTIONARAY – BUILT IN METHODS

Dictionary Method Meaning


dict.clear() Removes all the elements of the
dictionary
dict.copy() Returns (shallow)copy of
dictionary.
dict.get(key, for key key, returns value or
default=None) default if key not in dictionary
(note that default's default is
None)

dict.items() returns a list of dict's (key, value)


tuple pairs
DICTIONARAY – BUILT IN METHODS

Dictionary Method Meaning


dict.keys() returns list of dictionary dict's
keys
dict.setdefault key, similar to get(), but will set
default=None dict[key]=default if key is not
already in dict
dict.update(dict2) adds dictionary dict2's key-values
pairs to dict
dict.values() returns list of dictionary dict's
values
DICTIONARAY – BUILT IN METHODS

Dictionary Method Meaning


dict.pop() returns list of dictionary dict's
keys
dict.popitem() similar to get(), but will set
dict[key]=default if key is not
already in dict
dict.clear() METHOD

clear method

OUTPUT is in next slide!


dict.clear() METHOD - OUTPUT

Birthday dictionary cleared


dict.copy() METHOD

copy method

OUTPUT is in next slide!


dict.copy() METHOD - OUTPUT

copy method creates b2 dictionary


dict.get() METHOD

get method

OUTPUT is in next slide!


dict.get() METHOD - OUTPUT

Creating a b2 dictionary using get method


dict.items() METHOD

items method

OUTPUT is in next slide!


dict.items() METHOD - OUTPUT

items method returns dictionary content


dict.keys() METHOD

keys method

OUTPUT is in next slide!


dict.keys() METHOD - OUTPUT

keys method returns dictionary keys


dict.update() METHOD

update method
dict.values() METHOD

values method returns dictionary values


dict.pop() METHOD

pop method removes specified key


values from the dictionary
dict.popitem() METHOD

popitem method removes values/items


from the dictionary
CLASS WORK/HOME WORK
CLASS WORK/HOME WORK
1. Write a Python script to sort (ascending and
descending) a dictionary by value.
2. Write a Python script to add a key to a dictionary.

Sample Dictionary : {0: 10, 1: 20}


Expected Result : {0: 10, 1: 20, 2: 30}
3. Write a Python script to concatenate following
dictionaries to create a new one.
Sample Dictionary :
dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
Expected Result : {1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6:
60}
CLASS WORK/HOME WORK

4. Write a Python script to check if a given key


already exists in a dictionary.

5. Write a Python program to iterate over


dictionaries using for loops.

6. Write a Python script to generate and print a


dictionary that contains a number (between 1 and
n) in the form (x, x*x).
Sample Dictionary ( n = 5) :
Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
CLASS WORK/HOME WORK

7. Write a Python script to print a dictionary where


the keys are numbers between 1 and 15 (both
included) and the values are square of keys.

Sample Dictionary
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81,
10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}

8. Write a Python script to merge two Python


dictionaries.
CLASS WORK/HOME WORK

9. Write a Python program to iterate over


dictionaries using for loops.

10. Write a Python program to sum all the items in


a dictionary.
Class Test
Class Test

Time: 40 Min Max Marks: 20

1. What is dictionary? give example 02


2. Write a python script to traverse a
dictionary 05
3. Explain 5 list built in methods of
dictionary 10
4. What is key value pair? Explain 05
THANK YOU

You might also like