0% found this document useful (0 votes)
9 views8 pages

4TH Lecture

Uploaded by

iswella121
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)
9 views8 pages

4TH Lecture

Uploaded by

iswella121
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/ 8

Dictionary in Python

Dictionaries are used to store data values in key:value pairs WE CANNOT WRITE THE DOUBLES KEYS

e
IN THE ONE DICTIONARY

g
They are unordered, mutable(changeable) & don’t allow duplicate keys

olle
IN THE LIST AND THE TUPLE THER IS ALSO INDEX IS PRESENT

C
BUT IN THE DICTIONARY THERE IS NO INDEX THEREFORE DICTIONARY
IS UNORDERED

a
WE CAN ALSO ASSIGN THE ADDITIONAL VALUE
TO THE VALUE

n
“key” : value

p
KEY IS A IMMUTABLE ELEMENT

A
WHILE DICTIONARY(VALUES) ARE CHANGEABLE
1) KEY IS IMMUTABLE THEREFORE WE ALSO USED
THE TUPLE BECAUSE TUPLE IS ALSO IMMUTABLE
2) WE ALSO USED THE INTEGERS, BOOLEAN AND FLOATING VALUES
AS A KEY

AS DICTIONARY IS MUTABLE THEN WE CAN ADD A NEW PAIR IN MY DICTIONARY


AND WE CAN CHANGE MY ELEMENTS OF THE ONLY VALUE OF KEY NOT KEY

dict[”name”], dict[”cgpa”], dict[”marks”] WE CAN PRINT THE ONE BY ONE INDEX BY THIS METHOD

dict[”key”] = “value” #to assign or add new


NULL DICTIONARY MEANS { }
Dictionary in Python
Nested Dictionaries

lege
Col
pna
A
student[”score”][”math”] TO GET THE INDEX IN THE NESTED DICTIONARY (DICTIONARY IN THE DICTIONARY)
WE USED THE DOUBLE SQUARE BRACKETS[]
Dictionary Methods
myDict.keys( ) #returns all keys

lege
ol
myDict.values( ) #returns all values

na C
p
myDict.items( ) #returns all (key, val) pairs as tuples

A
myDict.get( “key““ ) #returns the key according to value
GIVE THE VALUE OF THE SPECIFIC KEY IF WE WRITE THE WRONG KEY NAME
THEN ITS GIVE NONE

myDict.update( newDict ) #inserts the specified items to the dictionary


ITS UPDATE THE OLD DICTIONARY (myDic) WITH THE NEW ADDING DICTIONARY
SETS ARE MUTABLE BUT SETS ELEMENTS ARE THE IMMUTABLE

Set in Python SIMILARLY SET HAVE NO INDEX AS THE DICTIONARY

SET'S ELEMENTS ARE IMMUTABLE THEREFORE WE USED


Set is the collection of the unordered items. 1) STRINGS

e
2) INTEGERS
3) FLOATING VALUE

g
Each element in the set must be unique & immutable.
4) TUPLE

e
5) BOOLEAN VALUE

l
WE CANNOT USED THE LISTS AND DICTIONARY IN THE SETS

l
BECAUSE THESE ARE THE MUTABLR VALUES

a Co
NO DOUBLE VALUES ARE WRITE IN THE SET

nums = { 1, 2, 3, 4 }

n
BECAUSE SET WANTS THE UNIQU VALUES

set2 = { 1, 2, 2, 2 }

Ap
#repeated elements stored only once, so it resolved to {1, 2}

null_set = set( ) #empty set syntax


Set Methods
set.add( el ) #adds an element

lege
ol
set.remove( el ) #removes the elem an

na C
p
set.clear( ) #empties the set

A
set.pop( ) #removes a random value
Set Methods
set.union( set2 ) #combines both set values & returns new

lege
set.intersection( set2 ) #combines common values & returns new

Col
pna
A
Let‘s Practice
Store following word meanings in a python dictionary :

ge
table : “a piece of furniture”, “list of facts & figures”
cat : “a small animal”

olle
na C
Ap
You are given a list of subjects for students. Assume one classroom is required for 1
subject. How many classrooms are needed by all students.

”python”, “java”, “C++”, “python”, “javascript”,


“java”, “python”, “java”, “C++”, “C”
Let‘s Practice
WAP to enter marks of 3 subjects from the user and store them in a dictionary. Start with
an empty dictionary & add one by one. Use subject name as key & marks as value.

lege
Col
pna
A
Figure out a way to store 9 & 9.0 as separate values in the set.
(You can take help of built-in data types)

You might also like