사전은 순서가 지정되지 않고 변경 가능하며 인덱싱되는 모음입니다. Python에서 사전은 중괄호로 작성되며 키와 값이 있습니다. copy() 메서드는 사전의 얕은 복사본을 반환합니다.
예
#creating a dictionary original = {1:'vishesh', 2:'python'} # copying using copy() function new = original.copy() # removing all elements from the list Only new list becomes empty as #copy() does shallow copy. new.clear() print('new: ', new) print('original: ', original) # between = and copy() original = {1:'Vishesh', 2:'python'} # copying using copy() function new = original.copy() # removing all elements from new list # and printing both new.clear() print('new: ', new) print('original: ', original) original = {1:'one', 2:'two'} # copying using = new = original # removing all elements from new list # and printing both new.clear() print('new: ', new) print('original: ', original)
출력
('new: ', {}) ('original: ', {1: 'vishesh', 2: 'python'}) ('new: ', {}) ('original: ', {1: 'Vishesh', 2: 'python'}) ('new: ', {}) ('original: ', {})