<gh_stars>0
# -*- coding: utf-8 -*-
"""Untitled7.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/15uXixGnB4G8oLYaN3NmODpqQoRL5maK3
"""

Creating a dictionary

1.	# empty dictionary
2.	my_dict = {}  
3.	# dictionary with integer keys
4.	my_dict = {1: 'apple', 2: 'ball'}  
5.	# dictionary with mixed keys
6.	my_dict = {'name': 'divanshi', 1: [2, 4, 3]}  
7.	# using dict()
8.	my_dict = dict({1:'apple', 2:'ball'})  
9.	# from sequence having each item as a pair 
10.	my_dict = dict([(1,'apple'), (2,'ball')])

# Accessing elements in a dictionary 

# 1.	dict={'name':'Girish','age':23}
# 2.	 print("name is ",dict[name])
# 3.	 print("name is ",dict['name'])
# 4.	name is  Girish
# 5.	 print("age is  ",dict['age'])
# 6.	age is   23
# 7.	print("marks are ",dict["marks"])
# 8.	Traceback (most recent call last):
# 9.	  File "<pyshell#12>", line 1, in <module>
# 10.	    print("marks are ",dict["marks"])
# 11.	KeyError: 'marks'

# Updating dictionary

# 1.	dict={'name':'Girish','age':23}
# 2.	dict['age']=24
# 3.	dict['marks']=90
# 4.	dict
# 5.	{'name': 'Girish', 'age': 24, 'marks': 90}

# Deleting dictionary elements

# 1.	squares = {1:1, 2:4, 3:9, 4:16, 5:25}  # create a dictionary
# 2.	squares.pop(4)  # remove a particular item 16
# 3.	16
# 4.	squares.popitem()  # remove an arbitrary item (1, 1)
# 5.	(5, 25)
# 6.	del squares[2]  # delete a particular item
# 7.	squares
# 8.	{1: 1, 3: 9}
# 9.	squares.clear()  # remove all items
# 10.	squares
# 11.	{}
# 12.	del squares  # delete the dictionary itself
# 13.	 squares
# 14.	Traceback (most recent call last):
# 15.	  File "<pyshell#28>", line 1, in <module>
# 16.	    squares
# 17.	NameError: name 'squares' is not defined

# Properties of dictionary keys

# a.	dict={["name"]:"Girish","age":27}
# b.	Traceback (most recent call last):
# c.	  File "<pyshell#6>", line 1, in <module>
# d.	dict={["name"]:"Girish","age":27}
# e.	TypeError: unhashable type: 'list'

# Dictionary len() method

# 1.	dict={"name":"Girish","age":27}
# 2.	print("length is ",len(dict))
# 3.	length is  2

# Dictionary str() method

# 1.	dict={"name":"Girish","age":34}
# 2.	 str(dict)
# 3.	"{'name': 'Girish', 'age': 34}"

# Dictionary type() method

# d={'name':'Girish','age':34}
# 2.	type(d)
# 3.	<class 'dict'>

# Dictionary clear() method

# 1.	d={'name':'Girish','age':34}
# 2.	d.clear()
# 3.	 d
# 4.	{}

# Dictionary copy() method 

# 1.	x>>> d={'name':'Girish','age':34}
# 2.	 d2=d.copy()
# 3.	print(d2)
# 4.	{'name': 'Girish', 'age': 34}

# Dictionary fromkeys() method 



# 1.	>>> d={'name':'Girish','age':34}
# 2.	>>>dict=dict.fromkeys(d)
# 3.	>>>dict
# 4.	{'name': None, 'age': None}
# 5.	>>> l=['name','marks','age']
# 6.	>>>dict=dict.fromkeys(l)
# 7.	>>>dict
# 8.	{'name': None, 'marks': None, 'age': None}
# 9.	>>>dict=dict.fromkeys(l,10)
# 10.	>>>dict
# 11.	{'name': 10, 'marks': 10, 'age': 10}

# Dictionary get( ) Method

# 1.	>>> d={'name':'Girish','age':34,'marks':90}
# 2.	>>> print(d.get('age'))
# 3.	34
# 4.	>>> print(d.get('sex','male'))
# 5.	male

# Dictionary items() method


# 1.	>>> d={'name':'Girish','age':34,'marks':90}
# 2.	>>> print(d.items())

# Dictionary keys() method

# 1.	>>> d={'name':'Girish','age':34,'marks':90}
# 2.	>>>d.keys()
# 3.	dict_keys(['name', 'age', 'marks'])

# Dictionary setdefault() method

# 1.	>>> d={'name':'girish','age':34}
# 2.	>>> print(d.setdefault('age',None))
# 3.	34
# 4.	>>> print(d.setdefault('sex',None))
# 5.	None
# 6.	>>> d
# 7.	{'name': 'girish', 'age': 34, 'sex': None}

# Dictionary update() method 

# 1.	>>> d={'name':'girish','age':34}
# 2.	>>> d2={'marks':90}
# 3.	>>>d.update(d2)
# 4.	>>> d
# 5.	{'name': 'girish', 'age': 34, 'marks': 90}

# Dictionary values() method 

# 1.	>>> d={'name': 'girish', 'age': 34, 'marks': 90}
# 2.	>>> print ("Values : ",  list(d.values()))
# 3.	Values :  ['girish', 34, 90]

# Programs
# 1. Program to add a key-value pair to the dictionary. 
# 1.	d={'fruits':'apple','color':'pink'}
# 2.	k=input("enter a key to be added: ")
# 3.	v=input("enter its value: ")
# 4.	d.update({k:v})
# 5.	print("updated dictionary is :")
# 6.	print(d)

# Program to concatenate two dictionaries into one.
# 1.	d={'fruits':'apple','color':'pink'}
# 2.	d2={'name':'girish','age':34}
# 3.	d.update(d2)
# 4.	print("concatenated dictionary is :")
# 5.	print(d)

# # Program to find the sum of all values of a dictionary.
# 1.	d={'a':10,'b':20,'c':30,'d':40}
# 2.	print("the sum of values in the dictionary is ")
# 3.	print(sum(d.values()))

# Program which takes a string as a parameter and generates a frequency of characters contained in it.
# 1.	a=input("enter a string: ")
# 2.	d=dict()
# 3.	for i in a:
# 4.	    if(i not in d):
# 5.	        d[i]=1
# 6.	    else:
# 7.	        d[i]+=1
# 8.	print("frequency of each character is ")
# 9.	print(d)

# Program to count the frequency of words in a dictionary.
# 1.	string=input("enter a string: ")
# 2.	l=list()
# 3.	d=dict()
# 4.	l=string.split()
# 5.	for i in l:
# 6.	    if(i not in d):
# 7.	        d[i]=1
# 8.	    else:
# 9.	        d[i]+=1
# 10.	print("the frequency of words is ",d)

# Program to get a minimum and maximum value in a dictionary. 
# 1.	d={'a':23,'b':46,'c':78,'d':12}
# 2.	max=max(d)
# 3.	min=min(d)
# 4.	print("maximum value is ",max)
# 5.	print("minimum value is ",min)

# Program to find the number of positive and negative numbers from a list of numbers. 
# 1.	l1=[23,-33,90,-3,-12,-90,37,6,-7]
# 2.	d=dict()
# 3.	d['pos']=0
# 4.	d['neg']=0
# 5.	for i in l1:
# 6.	    if(i>0):
# 7.	        d['pos']+=1
# 8.	    else:
# 9.	        d['neg']+=1
# 10.	print(d)

# Program to convert octal to binary using a dictionary.
# 1.	num=input("Enter a number")
# 2.	table={'0':'000','1':'001','2':'010','3':'011','4':'100','5':'101','6':'110','7':'111'}
# 3.	b="ob"
# 4.	for i in num:
# 5.	       b+=table[i]
# 6.	print("number in binary is ",b)

# Program to replace dictionary values with their sum.
# 1.	d={'a':10,'b':20,'c':30,'d':40}
# 2.	result=1
# 3.	for key in d:
# 4.	    result=result*d[key]
# 5.	print("the result is ")
# 6.	print(result)
# -*- coding: utf-8 -*-
"""Untitled7.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/15uXixGnB4G8oLYaN3NmODpqQoRL5maK3
"""

Creating a dictionary

1.	# empty dictionary
2.	my_dict = {}  
3.	# dictionary with integer keys
4.	my_dict = {1: 'apple', 2: 'ball'}  
5.	# dictionary with mixed keys
6.	my_dict = {'name': 'divanshi', 1: [2, 4, 3]}  
7.	# using dict()
8.	my_dict = dict({1:'apple', 2:'ball'})  
9.	# from sequence having each item as a pair 
10.	my_dict = dict([(1,'apple'), (2,'ball')])

# Accessing elements in a dictionary 

# 1.	dict={'name':'Girish','age':23}
# 2.	 print("name is ",dict[name])
# 3.	 print("name is ",dict['name'])
# 4.	name is  Girish
# 5.	 print("age is  ",dict['age'])
# 6.	age is   23
# 7.	print("marks are ",dict["marks"])
# 8.	Traceback (most recent call last):
# 9.	  File "<pyshell#12>", line 1, in <module>
# 10.	    print("marks are ",dict["marks"])
# 11.	KeyError: 'marks'

# Updating dictionary

# 1.	dict={'name':'Girish','age':23}
# 2.	dict['age']=24
# 3.	dict['marks']=90
# 4.	dict
# 5.	{'name': 'Girish', 'age': 24, 'marks': 90}

# Deleting dictionary elements

# 1.	squares = {1:1, 2:4, 3:9, 4:16, 5:25}  # create a dictionary
# 2.	squares.pop(4)  # remove a particular item 16
# 3.	16
# 4.	squares.popitem()  # remove an arbitrary item (1, 1)
# 5.	(5, 25)
# 6.	del squares[2]  # delete a particular item
# 7.	squares
# 8.	{1: 1, 3: 9}
# 9.	squares.clear()  # remove all items
# 10.	squares
# 11.	{}
# 12.	del squares  # delete the dictionary itself
# 13.	 squares
# 14.	Traceback (most recent call last):
# 15.	  File "<pyshell#28>", line 1, in <module>
# 16.	    squares
# 17.	NameError: name 'squares' is not defined

# Properties of dictionary keys

# a.	dict={["name"]:"Girish","age":27}
# b.	Traceback (most recent call last):
# c.	  File "<pyshell#6>", line 1, in <module>
# d.	dict={["name"]:"Girish","age":27}
# e.	TypeError: unhashable type: 'list'

# Dictionary len() method

# 1.	dict={"name":"Girish","age":27}
# 2.	print("length is ",len(dict))
# 3.	length is  2

# Dictionary str() method

# 1.	dict={"name":"Girish","age":34}
# 2.	 str(dict)
# 3.	"{'name': 'Girish', 'age': 34}"

# Dictionary type() method

# d={'name':'Girish','age':34}
# 2.	type(d)
# 3.	<class 'dict'>

# Dictionary clear() method

# 1.	d={'name':'Girish','age':34}
# 2.	d.clear()
# 3.	 d
# 4.	{}

# Dictionary copy() method 

# 1.	x>>> d={'name':'Girish','age':34}
# 2.	 d2=d.copy()
# 3.	print(d2)
# 4.	{'name': 'Girish', 'age': 34}

# Dictionary fromkeys() method 



# 1.	>>> d={'name':'Girish','age':34}
# 2.	>>>dict=dict.fromkeys(d)
# 3.	>>>dict
# 4.	{'name': None, 'age': None}
# 5.	>>> l=['name','marks','age']
# 6.	>>>dict=dict.fromkeys(l)
# 7.	>>>dict
# 8.	{'name': None, 'marks': None, 'age': None}
# 9.	>>>dict=dict.fromkeys(l,10)
# 10.	>>>dict
# 11.	{'name': 10, 'marks': 10, 'age': 10}

# Dictionary get( ) Method

# 1.	>>> d={'name':'Girish','age':34,'marks':90}
# 2.	>>> print(d.get('age'))
# 3.	34
# 4.	>>> print(d.get('sex','male'))
# 5.	male

# Dictionary items() method


# 1.	>>> d={'name':'Girish','age':34,'marks':90}
# 2.	>>> print(d.items())

# Dictionary keys() method

# 1.	>>> d={'name':'Girish','age':34,'marks':90}
# 2.	>>>d.keys()
# 3.	dict_keys(['name', 'age', 'marks'])

# Dictionary setdefault() method

# 1.	>>> d={'name':'girish','age':34}
# 2.	>>> print(d.setdefault('age',None))
# 3.	34
# 4.	>>> print(d.setdefault('sex',None))
# 5.	None
# 6.	>>> d
# 7.	{'name': 'girish', 'age': 34, 'sex': None}

# Dictionary update() method 

# 1.	>>> d={'name':'girish','age':34}
# 2.	>>> d2={'marks':90}
# 3.	>>>d.update(d2)
# 4.	>>> d
# 5.	{'name': 'girish', 'age': 34, 'marks': 90}

# Dictionary values() method 

# 1.	>>> d={'name': 'girish', 'age': 34, 'marks': 90}
# 2.	>>> print ("Values : ",  list(d.values()))
# 3.	Values :  ['girish', 34, 90]

# Programs
# 1. Program to add a key-value pair to the dictionary. 
# 1.	d={'fruits':'apple','color':'pink'}
# 2.	k=input("enter a key to be added: ")
# 3.	v=input("enter its value: ")
# 4.	d.update({k:v})
# 5.	print("updated dictionary is :")
# 6.	print(d)

# Program to concatenate two dictionaries into one.
# 1.	d={'fruits':'apple','color':'pink'}
# 2.	d2={'name':'girish','age':34}
# 3.	d.update(d2)
# 4.	print("concatenated dictionary is :")
# 5.	print(d)

# # Program to find the sum of all values of a dictionary.
# 1.	d={'a':10,'b':20,'c':30,'d':40}
# 2.	print("the sum of values in the dictionary is ")
# 3.	print(sum(d.values()))

# Program which takes a string as a parameter and generates a frequency of characters contained in it.
# 1.	a=input("enter a string: ")
# 2.	d=dict()
# 3.	for i in a:
# 4.	    if(i not in d):
# 5.	        d[i]=1
# 6.	    else:
# 7.	        d[i]+=1
# 8.	print("frequency of each character is ")
# 9.	print(d)

# Program to count the frequency of words in a dictionary.
# 1.	string=input("enter a string: ")
# 2.	l=list()
# 3.	d=dict()
# 4.	l=string.split()
# 5.	for i in l:
# 6.	    if(i not in d):
# 7.	        d[i]=1
# 8.	    else:
# 9.	        d[i]+=1
# 10.	print("the frequency of words is ",d)

# Program to get a minimum and maximum value in a dictionary. 
# 1.	d={'a':23,'b':46,'c':78,'d':12}
# 2.	max=max(d)
# 3.	min=min(d)
# 4.	print("maximum value is ",max)
# 5.	print("minimum value is ",min)

# Program to find the number of positive and negative numbers from a list of numbers. 
# 1.	l1=[23,-33,90,-3,-12,-90,37,6,-7]
# 2.	d=dict()
# 3.	d['pos']=0
# 4.	d['neg']=0
# 5.	for i in l1:
# 6.	    if(i>0):
# 7.	        d['pos']+=1
# 8.	    else:
# 9.	        d['neg']+=1
# 10.	print(d)

# Program to convert octal to binary using a dictionary.
# 1.	num=input("Enter a number")
# 2.	table={'0':'000','1':'001','2':'010','3':'011','4':'100','5':'101','6':'110','7':'111'}
# 3.	b="ob"
# 4.	for i in num:
# 5.	       b+=table[i]
# 6.	print("number in binary is ",b)

# Program to replace dictionary values with their sum.
# 1.	d={'a':10,'b':20,'c':30,'d':40}
# 2.	result=1
# 3.	for key in d:
# 4.	    result=result*d[key]
# 5.	print("the result is ")
# 6.	print(result)
# -*- coding: utf-8 -*- Untitled7.ipynb  Automatically generated by Colaboratory.  Original file is located at     https://colab.research.google.com/drive/15uXixGnB4G8oLYaN3NmODpqQoRL5maK3 # empty dictionary # dictionary with integer keys # dictionary with mixed keys # using dict() # from sequence having each item as a pair # Accessing elements in a dictionary # 1.	dict={'name':'Girish','age':23} # 2.	 print("name is ",dict[name]) # 3.	 print("name is ",dict['name']) # 4.	name is  Girish # 5.	 print("age is  ",dict['age']) # 6.	age is   23 # 7.	print("marks are ",dict["marks"]) # 8.	Traceback (most recent call last): # 9.	  File "<pyshell#12>", line 1, in <module> # 10.	    print("marks are ",dict["marks"]) # 11.	KeyError: 'marks' # Updating dictionary # 1.	dict={'name':'Girish','age':23} # 2.	dict['age']=24 # 3.	dict['marks']=90 # 4.	dict # 5.	{'name': 'Girish', 'age': 24, 'marks': 90} # Deleting dictionary elements # 1.	squares = {1:1, 2:4, 3:9, 4:16, 5:25}  # create a dictionary # 2.	squares.pop(4)  # remove a particular item 16 # 3.	16 # 4.	squares.popitem()  # remove an arbitrary item (1, 1) # 5.	(5, 25) # 6.	del squares[2]  # delete a particular item # 7.	squares # 8.	{1: 1, 3: 9} # 9.	squares.clear()  # remove all items # 10.	squares # 11.	{} # 12.	del squares  # delete the dictionary itself # 13.	 squares # 14.	Traceback (most recent call last): # 15.	  File "<pyshell#28>", line 1, in <module> # 16.	    squares # 17.	NameError: name 'squares' is not defined # Properties of dictionary keys # a.	dict={["name"]:"Girish","age":27} # b.	Traceback (most recent call last): # c.	  File "<pyshell#6>", line 1, in <module> # d.	dict={["name"]:"Girish","age":27} # e.	TypeError: unhashable type: 'list' # Dictionary len() method # 1.	dict={"name":"Girish","age":27} # 2.	print("length is ",len(dict)) # 3.	length is  2 # Dictionary str() method # 1.	dict={"name":"Girish","age":34} # 2.	 str(dict) # 3.	"{'name': 'Girish', 'age': 34}" # Dictionary type() method # d={'name':'Girish','age':34} # 2.	type(d) # 3.	<class 'dict'> # Dictionary clear() method # 1.	d={'name':'Girish','age':34} # 2.	d.clear() # 3.	 d # 4.	{} # Dictionary copy() method # 1.	x>>> d={'name':'Girish','age':34} # 2.	 d2=d.copy() # 3.	print(d2) # 4.	{'name': 'Girish', 'age': 34} # Dictionary fromkeys() method # 1.	>>> d={'name':'Girish','age':34} # 2.	>>>dict=dict.fromkeys(d) # 3.	>>>dict # 4.	{'name': None, 'age': None} # 5.	>>> l=['name','marks','age'] # 6.	>>>dict=dict.fromkeys(l) # 7.	>>>dict # 8.	{'name': None, 'marks': None, 'age': None} # 9.	>>>dict=dict.fromkeys(l,10) # 10.	>>>dict # 11.	{'name': 10, 'marks': 10, 'age': 10} # Dictionary get( ) Method # 1.	>>> d={'name':'Girish','age':34,'marks':90} # 2.	>>> print(d.get('age')) # 3.	34 # 4.	>>> print(d.get('sex','male')) # 5.	male # Dictionary items() method # 1.	>>> d={'name':'Girish','age':34,'marks':90} # 2.	>>> print(d.items()) # Dictionary keys() method # 1.	>>> d={'name':'Girish','age':34,'marks':90} # 2.	>>>d.keys() # 3.	dict_keys(['name', 'age', 'marks']) # Dictionary setdefault() method # 1.	>>> d={'name':'girish','age':34} # 2.	>>> print(d.setdefault('age',None)) # 3.	34 # 4.	>>> print(d.setdefault('sex',None)) # 5.	None # 6.	>>> d # 7.	{'name': 'girish', 'age': 34, 'sex': None} # Dictionary update() method # 1.	>>> d={'name':'girish','age':34} # 2.	>>> d2={'marks':90} # 3.	>>>d.update(d2) # 4.	>>> d # 5.	{'name': 'girish', 'age': 34, 'marks': 90} # Dictionary values() method # 1.	>>> d={'name': 'girish', 'age': 34, 'marks': 90} # 2.	>>> print ("Values : ",  list(d.values())) # 3.	Values :  ['girish', 34, 90] # Programs # 1. Program to add a key-value pair to the dictionary. # 1.	d={'fruits':'apple','color':'pink'} # 2.	k=input("enter a key to be added: ") # 3.	v=input("enter its value: ") # 4.	d.update({k:v}) # 5.	print("updated dictionary is :") # 6.	print(d) # Program to concatenate two dictionaries into one. # 1.	d={'fruits':'apple','color':'pink'} # 2.	d2={'name':'girish','age':34} # 3.	d.update(d2) # 4.	print("concatenated dictionary is :") # 5.	print(d) # # Program to find the sum of all values of a dictionary. # 1.	d={'a':10,'b':20,'c':30,'d':40} # 2.	print("the sum of values in the dictionary is ") # 3.	print(sum(d.values())) # Program which takes a string as a parameter and generates a frequency of characters contained in it. # 1.	a=input("enter a string: ") # 2.	d=dict() # 3.	for i in a: # 4.	    if(i not in d): # 5.	        d[i]=1 # 6.	    else: # 7.	        d[i]+=1 # 8.	print("frequency of each character is ") # 9.	print(d) # Program to count the frequency of words in a dictionary. # 1.	string=input("enter a string: ") # 2.	l=list() # 3.	d=dict() # 4.	l=string.split() # 5.	for i in l: # 6.	    if(i not in d): # 7.	        d[i]=1 # 8.	    else: # 9.	        d[i]+=1 # 10.	print("the frequency of words is ",d) # Program to get a minimum and maximum value in a dictionary. # 1.	d={'a':23,'b':46,'c':78,'d':12} # 2.	max=max(d) # 3.	min=min(d) # 4.	print("maximum value is ",max) # 5.	print("minimum value is ",min) # Program to find the number of positive and negative numbers from a list of numbers. # 1.	l1=[23,-33,90,-3,-12,-90,37,6,-7] # 2.	d=dict() # 3.	d['pos']=0 # 4.	d['neg']=0 # 5.	for i in l1: # 6.	    if(i>0): # 7.	        d['pos']+=1 # 8.	    else: # 9.	        d['neg']+=1 # 10.	print(d) # Program to convert octal to binary using a dictionary. # 1.	num=input("Enter a number") # 2.	table={'0':'000','1':'001','2':'010','3':'011','4':'100','5':'101','6':'110','7':'111'} # 3.	b="ob" # 4.	for i in num: # 5.	       b+=table[i] # 6.	print("number in binary is ",b) # Program to replace dictionary values with their sum. # 1.	d={'a':10,'b':20,'c':30,'d':40} # 2.	result=1 # 3.	for key in d: # 4.	    result=result*d[key] # 5.	print("the result is ") # 6.	print(result)