[go: up one dir, main page]

0% found this document useful (0 votes)
12 views3 pages

Dictionary Program

The document provides a comprehensive overview of various methods and techniques for creating and manipulating dictionaries in Python. It covers topics such as using integer and mixed keys, nested dictionaries, adding and updating elements, accessing values, and deleting data. Additionally, it demonstrates common dictionary methods like copy, clear, get, items, keys, pop, and update.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Dictionary Program

The document provides a comprehensive overview of various methods and techniques for creating and manipulating dictionaries in Python. It covers topics such as using integer and mixed keys, nested dictionaries, adding and updating elements, accessing values, and deleting data. Additionally, it demonstrates common dictionary methods like copy, clear, get, items, keys, pop, and update.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

# Creating a Dictionary

def dict_integer_keys():
# with Integer Keys
dict = {1: 'Python', 2: 'For', 3: 'Learning'}
print("\nDictionary with the use of Integer Keys: ")
print(dict) # to get all dict value
print(dict.values()) # to get only value of dict
print(dict.keys()) # to get only the keys of dict

def dict_mixed_keys():
# with Mixed keys
dict = {'Name': 'Python', 1: [1, 2, 3, 4]}
print("\nDictionary with the use of Mixed Keys: ")
print(dict)
print(dict[1]) # to fetch the value with key

def dict_withdict():
# with dict() method
dic = dict({1: 'Python', 2: 'For', 3: 'Training'})
print("\nDictionary with the use of dict(): ")
print(dic)

def dict_with_pair():
# with each item as a Pair
dic = dict([(1, 'Python'), (2, 'For')])
print("\nDictionary with each item as a pair: ")
print(dic.keys())
print(dic.values())

def dict_nested():
# nested declaration
dic = {1: 'python', 2: 'For',
3: {'A': 'Welcome', 'B': 'To', 'C': 'Python'}}
print(dic)
print(dic[3]) # to fetch the value with key

def dic_empty_add_update():
# Creating an empty Dictionary
Dict = {}
print("Empty Dictionary: ")
print(Dict)

# Adding elements one at a time


Dict[0] = 'Python'
Dict[2] = 'For'
Dict[3] = 1
print("\nDictionary after adding 3 elements: ")
print(Dict)

# Adding set of values to a single Key


Dict['Value_set'] = 2, 3, 4
print("\nDictionary after adding 3 elements: ")
print(Dict)
# Updating existing Key's Value
Dict[2] = 'Welcome'
print("\nUpdated key value: ")
print(Dict)

# Adding Nested Key value to Dictionary


Dict[5] = {'Nested': {'1': 'Life', '2': 'Test'}}
print("\nAdding a Nested Key: ")
print(Dict)

def dict_value_with_key():
# Creating a Dictionary
dict = {1: 'Python', 'name': 'For', 3: 'Testing'}

# accessing a element using key


print("Accessing a element using key:")
print(dict['name'])

# accessing a element using key


print("Accessing a element using key:")
print(dict[1])

def dict_value_with_get():
# Creating a Dictionary
dict = {1: 'Python', 'name': 'For', 3: 'Testing'}

# accessing a element using key


print("Accessing a element using key:")
print(dict.get('name'))

# accessing a element using key


print("Accessing a element using key:")
print(dict.get(1))

def dict_value_with_key_for_nested():
# Creating a Dictionary
dict = {'Dict1': {1: 'Python'},
'Dict2': {'Name': 'For'}}

# Accessing element using key


print(dict['Dict1'])
print(dict['Dict1'][1])
print(dict['Dict2']['Name'])
print(dict['Dict2'].keys())
print(dict['Dict1'].values())

def dict_data_deletion():

# Creating a Dictionary
Dict = {1: 'Python', 'name': 'For', 3: 'Testing'}

print("Dictionary =")
print(Dict)
#Deleting some of the Dictionary data
del(Dict[1])
print("Data after deletion Dictionary=")
print(Dict)

def all_dict_methods():
dict1 = {1: "Python", 2: "Java", 3: "Ruby", 4: "Scala"}

# copy() method
dict2 = dict1.copy()
print(dict2)

# clear() method
dict1.clear()
print(dict1)

# get() method
print(dict2.get(1))

# items() method
print(dict2.items())

# keys() method
print(dict2.keys())

# pop() method
dict2.pop(4)
print(dict2)

# popitem() method
dict2.popitem()
print(dict2)

# update() method
dict2.update({3: "Scala"})
print(dict2)

# values() method
print(dict2.values())

dict_integer_keys()
dict_mixed_keys()
dict_withdict()
dict_with_pair()
dict_nested()
dic_empty_add_update()
dict_value_with_key()
dict_value_with_get()
dict_value_with_key_for_nested()
dict_data_deletion()
all_dict_methods()

You might also like