Course
Copy Sets
Python Tutorial
This Python tutorial has been written for the beginners to help them understand the basic to advanced concepts of Python Programming Language. After completing this tutorial, you will find yourself at a great level of expertise in Python, from where you can take yourself to the next levels to become a world class Software Engineer.
Copy Sets
The
copy()
method in set class creates a shallow copy of a set object.Syntax
set.copy()
Return Value
The
copy()
method returns a new set which is a shallow copy of existing set.Example
lang1 = {"C", "C++", "Java", "Python"}print ("lang1: ", lang1, "id(lang1): ", id(lang1))lang2 = lang1.copy()print ("lang2: ", lang2, "id(lang2): ", id(lang2))lang1.add("PHP")print ("After updating lang1")print ("lang1: ", lang1, "id(lang1): ", id(lang1))print ("lang2: ", lang2, "id(lang2): ", id(lang2))
Output
lang1: {'Python', 'Java', 'C', 'C++'} id(lang1): 2451578196864lang2: {'Python', 'Java', 'C', 'C++'} id(lang2): 2451578197312After updating lang1lang1: {'Python', 'C', 'C++', 'PHP', 'Java'} id(lang1): 2451578196864lang2: {'Python', 'Java', 'C', 'C++'} id(lang2): 2451578197312
Practice with Online Editor
Note: This Python online Editor is a Python interpreter written in Rust, RustPython may not fully support all Python standard libraries and third-party libraries yet.
Remember to save code(Ctrl
+S
Or
Command
+S
) before run it.