Introduction Python Set copy() Method with Examples

Introduction Python Set copy() Method with Examples

The copy() method in Python returns a copy of the Set. We can copy a set to another set using the = operator, however copying a set using = operator means that when we change the new set the copied set will also be changed, if you do not want this behaviour then use the copy() method instead of = operator. We will discuss these with the help of examples.

The copy() method in Python returns a copy of the Set. We can copy a set to another set using the = operator, however copying a set using = operator means that when we change the new set the copied set will also be changed, if you do not want this behaviour then use the copy() method instead of = operator. We will discuss these with the help of examples.

Python Set copy() method

Syntax

new_set = original_set.copy()


Here, the new_set is the set in which you want to copy and original_set is the old set which you want to keep unchanged.

Return Value

The set.copy() method does not return any value, it just copies the original set to a new set.

Example 1

# app.py

# Writing char by char in a set
set1 = {'C', 'o', 'd', 'i', 'n', 'g'}

print("Before copy the set is: ", set1)
# Now we will copy this set to a new set named Set2
# Using = operator
set2 = set1
print("The new set is: ", set2)

# Now we will modify the new set
set2.add('H')
set2.add('i')

# Printing both the set
print("After copying Old set is: ", set1)
print("After copying New set is: ", set2)


Output


Before copy the set is:  {'o', 'd', 'n', 'C', 'g', 'i'}
The new set is:  {'o', 'd', 'n', 'C', 'g', 'i'}
After copying Old set is:  {'o', 'd', 'n', 'C', 'H', 'g', 'i'}
After copying New set is:  {'o', 'd', 'n', 'C', 'H', 'g', 'i'}

So, in this example, we can see that the original set is also modified when we modified the new set.

Now, let’s understand copy() method, where we can keep our original set unchanged.

Example 2


# app.py

# Writing char by char in a set
set1 = {'C', 'o', 'd', 'i', 'n', 'g'}

print("Before copy the set is: ", set1)
# Now we will copy this set to a new set named Set2
# Using copy() method
set2 = set1.copy()
print("The new set is: ", set2)

# Now we will modify the new set
set2.add('H')
set2.add('i')

# Printing both the set
print("After copying Old set is: ", set1)
print("After copying New set is: ", set2)

Output

Before copy the set is:  {'d', 'g', 'n', 'i', 'o', 'C'}
The new set is:  {'i', 'd', 'g', 'o', 'n', 'C'}
After copying Old set is:  {'d', 'g', 'n', 'i', 'o', 'C'}
After copying New set is:  {'i', 'H', 'd', 'g', 'o', 'n', 'C'}


in this example, we can see that though we have made changes to our new set, the old set remains unchanged. This is the advantage of a set copy() method.

Learn More

Thanks for reading !

Guide to Python Programming Language

Guide to Python Programming Language

Guide to Python Programming Language

Description
The course will lead you from beginning level to advance in Python Programming Language. You do not need any prior knowledge on Python or any programming language or even programming to join the course and become an expert on the topic.

The course is begin continuously developing by adding lectures regularly.

Please see the Promo and free sample video to get to know more.

Hope you will enjoy it.

Basic knowledge
An Enthusiast Mind
A Computer
Basic Knowledge To Use Computer
Internet Connection
What will you learn
Will Be Expert On Python Programming Language
Build Application On Python Programming Language

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Python Programming Tutorials For Beginners

Description
Hello and welcome to brand new series of wiredwiki. In this series i will teach you guys all you need to know about python. This series is designed for beginners but that doesn't means that i will not talk about the advanced stuff as well.

As you may all know by now that my approach of teaching is very simple and straightforward.In this series i will be talking about the all the things you need to know to jump start you python programming skills. This series is designed for noobs who are totally new to programming, so if you don't know any thing about

programming than this is the way to go guys Here is the links to all the videos that i will upload in this whole series.

In this video i will talk about all the basic introduction you need to know about python, which python version to choose, how to install python, how to get around with the interface, how to code your first program. Than we will talk about operators, expressions, numbers, strings, boo leans, lists, dictionaries, tuples and than inputs in python. With

Lots of exercises and more fun stuff, let's get started.

Download free Exercise files.

Dropbox: https://bit.ly/2AW7FYF

Who is the target audience?

First time Python programmers
Students and Teachers
IT pros who want to learn to code
Aspiring data scientists who want to add Python to their tool arsenal
Basic knowledge
Students should be comfortable working in the PC or Mac operating system
What will you learn
know basic programming concept and skill
build 6 text-based application using python
be able to learn other programming languages
be able to build sophisticated system using python in the future

To know more:

Learn Python Programming

Learn Python Programming

Learn Python Programming

Description
Learn Python Programming

Learn Python Programming and increase your python programming skills with Coder Kovid.

Python is the highest growing programming language in this era. You can use Python to do everything like, web development, software development, cognitive development, machine learning, artificial intelligence, etc. You should learn python programming and increase your skills of programming.

In this course of learn python programming you don't need any prior programming knowledge. Every beginner can start with.

Basic knowledge
No prior knowledge needed to learn this course
What will you learn
Write Basic Syntax of Python Programming
Create Basic Real World Application
Program in a fluent manner
Get Familiar in Programming Environment