# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## QUICK REVISION NOTES 5

# ### Dictionary and its method

# +
# Dictionary is a combination of key-value pairs seperated by semi-colan ':' and kept inside '{}'
# Dictionary is a mutable object but it doent allow mutable object as key
# Key cant be duplicated but values can be duplicated.
# Indexing and slicing is not possible.
# To access a vale we have to use its key.
# -

dct = {'std': {'name':'abc', 'num':40}, 'std2':{'num':50}}
print(dct,'\nType of dct is',type(dct), '\nID of dct is', id(dct))

# +
# To access a value in dictionary
print(dct['std'])
print(dct['std']['name'])
print()

# To change the value in dictionary
dct['std']['num'] = 10
print(dct)

# +
# To view all the methods in dictionary

print(dir(dict))

# The identifiers starting and ending with underscore (_) are language defined identifiers
# Other than that everything are the methods of dictionary

# +
# get and setdefault

# get() - It returns the value of the particular key
#       - If the key is not available it returns None which can be edited by the user.
print('Get method in dictionary')
print(dct.get('std'))  # Returns the value of 'std'
print(dct.get('std3')) # Returns None since the key is not present
print(dct.get('hello', 'key-value pair not available'))  # Returns user defined output if the key not available
print(dct) 

# setdefult() - It returns the value of the particular key.
#             - If the key is not available it returns None and adds None as value to mentioned key
#             - This can also be used to add key-value pair to dictionary
print('\nSetdefault method in dictionary')
sdct = {1:20, 'true': 56, 'class':10}
print(sdct.setdefault('age'))  # Key 'age is not available so it returns None and saves it as value'
print(sdct.setdefault('name','krishna')) # Key 'name' is not available so it returns the assigned value & saves it
print(sdct)

#  update() - Its is used to update the dictionary with new key value pair or assign new value to available key
print('\nDefault method in dictionary')
udct = {10:20,30:10,50:89,45:82}
udct.update({10:'suresh'}) # return type is None
udct.update({'name':'krishna',101:202,50:{5:5,60:6}}) # return type is None
print(udct)

# +
# Removing a pair from dictionary

# pop() - It is used to remove a particular element from the dictionary
print('Pop method in dictionary')
pdct = {10:40,50:5,6:60,8:80}
print(pdct.pop(10)) # removes the pair and returns the removed value
# print(pdct.pop(100))  # returns key error if the key is not present in the dictionary
print(pdct.pop(100, 'Key not present')) # To avoid KeyError custom message can be used
print(pdct) # returns the dictionary after removing the mentioned pair

# popitem() - This removes a last pair from the dictionary.
#           - Returns key error if the dictionary is empty.
print('\nPopitem method in dictionary')
print(pdct.popitem()) # removes the last pair and returns it
print(pdct) # returns the dictionary after removing last pair

# +
#items(), keys(), values() - These creates a sequence of required elements from the dictionary

sdct = {10:1, 20:2, 30:3, 40:4, 50:5}
print(sdct.keys())
print(sdct.values())
print(sdct.items())

# This can be looped through dictionary
print('\nPrinting keys')
for i in sdct.keys():
    print(i, end=' ')
    
print('\n\nPrinting values')
for i in sdct.values():
    print(i, end=' ')
    
print('\n\nPrinting items')
for i in sdct.items():
    print(i, end=' ')
# -

# fromkeys() - This method is used to create default values for all keys in dictionary.
fkdct={}
fkdct = fkdct.fromkeys([1,2,3,4,5,6,7],'suresh') # Assign it to a variable will return the expected output
print(fkdct)

# +
# copy() - This method creates a shallow copy of the dictionary

spdct = {1:400, 2:800, 3:1200, 4:2000}
cpdct = spdct.copy()
print('Before making changes to copy')
print(spdct,cpdct,sep='\n')
print('\nAfter making changes to copy')
cpdct.update({4:1600})
print(spdct,cpdct,sep='\n')

# To create a deepcopy:
from copy import deepcopy
dpdct = deepcopy(spdct)

StarcoderdataJupyterScriptsDedupFiltered