IKH

In Built functions

dict()

  • To create a dictionary.

Example

Python
d=dict()
print(d, type(d))
d=dict({1:"ajay",2:"amit"})
print(d, type(d))
d=dict([(1,"ajay"),(2,"amit"),(3,"manish")])
print(d, type(d))

Output

PowerShell
{} <class 'dict'>
{1: 'ajay', 2: 'amit'} <class 'dict'>
{1: 'ajay', 2: 'amit', 3: 'manish'} <class 'dict'>

len()

  • Returns the number of items in the dictionary

Example

Python
d=dict()
print(len(d))
d=dict({1:"ajay",2:"amit"})
print(len(d))

Output

PowerShell
0
2

clear()

  • To remove all elements from the dictionary

Example

Python
d=dict({1:"ajay",2:"amit"})
print(d)
d.clear()
print(d)

Output

PowerShell
{1: 'ajay', 2: 'amit'}
{}

get()

  • To get the value associated with the key.
  • d.get(key) ⇒ If the key is available then returns the corresponding value otherwise returns None. It wont raise any error.
  • d.get(key,defaultvalue) ⇒ If the key is available then returns the corresponding value otherwise returns default value.

Example

Python
d={1:"ajay",2:"amit",3:"manish"}
print(d[1])
print(d[4])
print(d.get(1))
print(d.get(4))
print(d.get(1,"Guest"))
print(d.get(4,"Guest"))

Output

PowerShell
ajay
KeyError: 4
ajay
None
ajay
Guest

pop()

  • It removes the entry associated with the specified key and returns the corresponding value.
  • If the specified key is not available then we will get KeyError.

Example

Python
d={1:"ajay",2:"amit",3:"manish"} 
print(d.pop(1)) 
print(d) 
print(d.pop(4))

Output

PowerShell
ajay
{2: 'amit', 3: 'manish'}
KeyError: 4

popitem()

  • It removes an arbitrary item (key-value) from the dictionary and return it.

Example

Python
d={1:"ajay",2:"amit",3:"manish"} 
print(d.pop(1)) 
print(d)
print(d.popitem()) 
print(d)

Output

PowerShell
ajay
{2: 'amit', 3: 'manish'}
(3, 'manish')
{2: 'amit'}
  • If the dictionary is empty then we will get KeyError.

Example

Python
d={}
print(d.popitem())

Output

PowerShell
KeyError: 'popitem(): dictionary is empty'

keys()

  • It returns all keys associated with dictionary.

Example

Python
d={1:"ajay",2:"amit",3:"manish"}
print(d.keys())
for k in d.keys():
  print(k)

Output

PowerShell
dict_keys([1, 2, 3])
1
2
3

values()

  • It returns all values associated with the dictionary.

Example

Python
d={1:"ajay",2:"amit",3:"manish"}
print(d.values())
for v in d.values():
  print(v)

Output

PowerShell
dict_values(['ajay', 'amit', 'manish'])
ajay
amit
manish

items()

  • It returns list of tuples representing key-value pairs.

Example

Python
d={1:"ajay",2:"amit",3:"manish"}
for k,v in d.items():
  print(k,"--",v)

Output

PowerShell
1 -- ajay
2 -- amit
3 -- manish

copy()

  • To create exactly duplicate dictionary (cloned copy).

Example

Python
d={1:"ajay",2:"amit",3:"manish"}
print(d, id(d))
d1=d.copy()
print(d1, id(d1))

Output

PowerShell
{1: 'ajay', 2: 'amit', 3: 'manish'} 139872491861568
{1: 'ajay', 2: 'amit', 3: 'manish'} 139872489489984

setdefault()

  • If the key is already available then this function returns the corresponding value.
  • If the key is not available then the specified key-value will be added as new item to the dictionary.

Example

Python
d={1:"ajay",2:"amit",3:"manish"}
print(d.setdefault(4,"suman"))
print(d)
print(d.setdefault(5,"sachin"))
print(d)

Output

PowerShell
suman
{1: 'ajay', 2: 'amit', 3: 'manish', 4: 'suman'}
sachin
{1: 'ajay', 2: 'amit', 3: 'manish', 4: 'suman', 5: 'sachin'}

update()

  • All items present in the new dictionary will be added to existing dictionary.

Example

Python
d={1:"ajay",2:"amit",3:"manish"}
print(d)
d.update({4:"suman"})
print(d)

Output

PowerShell
{1: 'ajay', 2: 'amit', 3: 'manish'}
{1: 'ajay', 2: 'amit', 3: 'manish', 4: 'suman'}

Question

Write a program to take dictionary from the keyboard and print the sum of values

Python
d=eval(input("Enter dictionary:"))
s=sum(d.values())
print("Sum= ",s)

Output

PowerShell
Enter dictionary:{'A': 10, 'B': 100}
Sum=  110

Question

Write a program to find number of occurrences of each letter present in the given string?

Python
word=input("Enter any word: ")
d={}
for x in word:
  d[x]=d.get(x,0)+1
for k,v in d.items():
  print(k,"occurred ",v," times")

Output

PowerShell
Enter any word: internetknowledgehub
i occurred  1  times
n occurred  3  times
t occurred  2  times
e occurred  4  times
r occurred  1  times
k occurred  1  times
o occurred  1  times
w occurred  1  times
l occurred  1  times
d occurred  1  times
g occurred  1  times
h occurred  1  times
u occurred  1  times
b occurred  1  times

Question

Write a program to find number of occurrences of each vowel present in the given string?

Python
word=input("Enter any word: ")
vowels={'a','e','i','o','u'}
d={}
for x in word:
  if x in vowels:
    d[x]=d.get(x,0)+1
for k,v in sorted(d.items()):
  print(k,"occurred ",v," times")

Output

PowerShell
Enter any word: internetknowledgehub
e occurred  4  times
i occurred  1  times
o occurred  1  times
u occurred  1  times

Question

Write a program to accept student name and marks from the keyboard and creates a dictionary. Also display student marks by taking student name as input?

Python
n=int(input("Enter the number of students: "))
d={}
for i in range(n):
  name=input("Enter Student Name: ")
  marks=input("Enter Student Marks: ")
  d[name]=marks
while True:
  name=input("Enter Student Name to get Marks: ")
  marks=d.get(name,-1)
  if marks== -1:
    print("Student Not Found")
  else:
    print("The Marks of",name,"are",marks)
  option=input("Do you want to find another student marks[Yes|No]")
  if option=="No":
    break
print("Thanks for using our application")

Output

PowerShell
Enter the number of students: 4
Enter Student Name: ajay
Enter Student Marks: 90
Enter Student Name: amit
Enter Student Marks: 95
Enter Student Name: manish
Enter Student Marks: 97
Enter Student Name: seema
Enter Student Marks: 85
Enter Student Name to get Marks: seema
The Marks of seema are 85
Do you want to find another student marks[Yes|No]No
Thanks for using our application

Ungraded Questions

Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!


Name
Email
Phone