0% found this document useful (0 votes)
24 views2 pages

Yetfjdt

The document discusses different collection data types in Python including lists, tuples, dictionaries and sets. It provides examples of creating, accessing and modifying dictionary data type including adding, updating and removing items from a dictionary. Methods like keys(), values(), items() are demonstrated along with nested dictionaries.

Uploaded by

hetvaghasiya1234
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)
24 views2 pages

Yetfjdt

The document discusses different collection data types in Python including lists, tuples, dictionaries and sets. It provides examples of creating, accessing and modifying dictionary data type including adding, updating and removing items from a dictionary. Methods like keys(), values(), items() are demonstrated along with nested dictionaries.

Uploaded by

hetvaghasiya1234
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/ 2

Dictionaries for x in dict:

print("List is a collection which is ordered and changeable. print(dict[x])


Allows duplicate members.") #Make a copy of a dictionary with the copy() method
print() mydict = dict.copy()
print("Tuple is a collection which is ordered and unchangeable. print(mydict)
Allows duplicate members.") #Make a copy of a dictionary with the dict() function
print() thisdict = dict.copy()
print("Dictionary is a collection which is unordered and print(thisdict)
changeable. No duplicate members.") myClass = {
print() "s1" : {
print("Set is a collection which is unordered and unindexed. No "name" : "Abc",
duplicate members.") "age" : 20
print() },
dict = {'Name': 'Abc', 'Age': 15, 'Class':'11th'} "s2" : {
print(dict) #print a dictionary "name" : "Pqr",
print ("dict['Name']: ", dict['Name']) #Dictionary items are "age" : 18
presented in key:value pairs, and can be referred to by using },
the key name. "s3" : {
print ("dict['Age']: ", dict['Age']) "name" : "Lmn",
dict['Age'] = 16; #update existing entry "age" : 20
dict['School'] = "J. B. & KARP School"; #Add new entry }
print(dict) }
#del dict['Name']; #del keyword removes the item with the print(myClass)
specified key name
#print ("dict['Name']: ", dict['Name']) #1. Write a program to create a dictionary containing
print(dict)
#dict.clear(); #clear() method empties the dictionary names of winner as keys and number of competition as
print ("dict['Age']: ", dict['Age']) values.
#del dict ; #del keyword can also delete the dictionary
completely n = int(input("How many students:"))
print(dict) winlist = {}
dict = {'Name': 'Abc', 'Age': 15, 'Class':'11th', 'Name':'Mno'}
#Duplicate values will overwrite existing values for i in range(n):
print(dict) k = input("Name of student:")
print(len(dict)) #Print the number of items in the dictionary
print(type(dict)) #Print the data type of a dictionary v = int(input("Number of competitions won:"))
x = dict["Name"] #Access the items of a dictionary by referring winlist[k] = v
to its key name, inside square brackets
print(x) print(winlist)
#get() method that will give you the same result #2. Create a dictionary with the opposite mapping.
x = dict.get("Age")
print(x) n = int(input("How many students:"))
#keys() method will return a list of all the keys in the dictionary winlist = {}
x = dict.keys()
print(x) for i in range(n):
#values() method will return a list of all the values in the k = input("Name of student:")
dictionary
x = dict.values() v = int(input("Number of competitions won:"))
print(x) winlist[k] = v
#items() method will return each item in a dictionary, as tuples
in a list rev_dict = {v : k for k, v in winlist.items()}
x = dict.items() print(winlist)
print(x)
#Determine if a specified key is present in a dictionary use the print(rev_dict)
in keyword #3. Write a program to find the pair of key : value to
if "Age" in dict:
print("Yes, 'Age' is one of the keys in the dict dictionary") ask value.
#update() method will update dict dictionary n = int(input("How many students:"))
dict.update({"Age": 17})
print(dict) st = {}
#pop() method removes the item with the specified key name: for i in range(n):
dict.pop("Age")
print(dict) k = input("Name of student:")
#popitem() method removes the last inserted item v = int(input("Number of competitions won:"))
dict.popitem()
print(dict) st[k] = v
#Print all key names in the dictionary, one by one print(st)
dict = {'Name': 'Abc', 'Age': 15, 'Class':'11th'}
for x in dict: ans = "y"
print(x) while ans == 'y' or ans == 'Y':
#Print all values in the dictionary, one by one
v = int(input("Enter value:"))
for k in st:
if st[k] == v:
print(k,':',st[k])
ans = input("Do you want to check more y/n:")
#4 From the string, to create a list and a Dictionary
t = "malayalam"
myDict = {}
ct = 0
myList = []
for word in t:
if word not in myList:
myList.append(word)
myDict[word] = 0
ct += 1
myDict[word] +=1
print(myDict)
print(myList)
#5. Write a program to input a word. Count the
occurrence of each vowel in the word and store it in a
dictionary as key-value pair. Pass this dictionary to a
function and print the occurrence of each vowel.
def printFrequency(freqDict):
for key in freqDict:
print(key,':',freqDict[key])

w = input('Enter a word:')
characters = list(w.upper())
vCount = {}
for ch in characters:
vList = ['A', 'E', 'I', 'O', 'U']
if ch in vList:
unique_key = ch
if unique_key not in vCount:
freq = characters.count(unique_key)
vCount[unique_key] = freq
printFrequency(vCount)

You might also like