Refer this to understand Looping Concepts in dictionary
Size: 375.17 KB
Language: en
Added: May 27, 2024
Slides: 8 pages
Slide Content
Dictionary By, Prof. S.S.Gawali Computer Engineering, Sanjivani College of Engineering Sanjivani Rural Education Society’s Sanjivani College of Engineering, Kopargaon-423 603 Department of Computer Engineering
What is Dictionary? Dictionary is mapping data type in python. Dictionary is a data structure which store values as a pair of key and value. In dictionary keys are unique and immutable. In dictionary values are not unique but mutable. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 2
Dictionary Looping Dictionary looping means visiting elements (key, value or item) in the dictionary. ‘for’ loop use to loop through the dictionary. There are three different way to loop dictionary: By visiting only keys By visiting only values By visiting Items (Key and Values at atime ) Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 3
Loop to print keys in Dictionary Printing all keys of dictionary stud={'rollno':1, ' name':'Soham ', ' class':'FY '} for key in stud: #for loop to visit keys print(key) #printing keys Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 4 Output: rollno name class
Loop to print keys in Dictionary stud={'rollno':1, ' name':'Soham ', ' class':'FY '} for val in stud.values (): #loop to work with values in dict print( val ) # display value Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 5 Output: 1 Soham FY
Loop to print items (Key, Value) in Dictionary stud={'rollno':1, ' name':'Soham ', ' class':'FY '} for i in stud.items (): # loop to visit items in dict print( i ) # display item as tuple Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 6 Output: (' rollno ', 1) ('name', 'Soham') ('class', 'FY')
Loop to print items (Key, Value) in Dictionary stud={'rollno':1, ' name':'Soham ', ' class':'FY '} for key,value in stud.items (): #loop to consider key and values separatetly print( key,value ) # display key and its value Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 7 Output: rollno 1 name Soham class FY
Thank You…. Department of Computer Engineering, Sanjivani College of Engineering, Kopargaon 8