Account
Categories

Python Set copy() Method


Definition:

When you use the copy() method in your program, it duplicates the original set into a new variable.

Now you will clearly understand the method through the flowchart.

s = {10, 20, 30}   (original)

s2 = s.copy()
   │
   ▼
{10, 20, 30}
   │
   └──────────────►  s2 = {10, 20, 30}   (copy)

s = {10, 20, 30}   (original)

Syntax:

new_set = set_name.copy()

Example:

s = {10, 20, 30}
s2 = s.copy()
print(s2)

Output:

Exercise:

Create a set s = {1, 2, 3} and copy it into s2.