{"id":23874,"date":"2021-10-05T20:29:14","date_gmt":"2021-10-05T14:59:14","guid":{"rendered":"https:\/\/python-programs.com\/?p=23874"},"modified":"2021-11-22T18:33:25","modified_gmt":"2021-11-22T13:03:25","slug":"python-program-for-set-copy-method","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-for-set-copy-method\/","title":{"rendered":"Python Program for Set copy() Method"},"content":{"rendered":"
Python set:<\/strong><\/p>\n Python set is a list of items that are not ordered. \u2013 Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.<\/p>\n The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n set copy() Method:<\/strong><\/p>\n The Python set copy method is used to copy the entire set into a new set.<\/p>\n Syntax:<\/strong><\/p>\n Parameters:<\/strong> The copy() method for sets does not accept any parameters.<\/p>\n Return value<\/b>: The function returns a shallow copy of the original set as its return value.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Python set: Python set is a list of items that are not ordered. \u2013 Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created. The elements of the set, unlike other Python sets, …<\/p>\nGiven set = {'hello', 'this', 'is', 'btechgeeks'}<\/pre>\n
The given set is :\r\n{'this', 'hello', 'btechgeeks', 'is'}\r\nThe New set after copying all the values from the given set is:\r\n{'this', 'hello', 'btechgeeks', 'is'}<\/pre>\n
Here all the values of the given set are copied into the new set using the copy() function.<\/pre>\n
Given set = {'good', 'morning', 'btechgeeks'}<\/pre>\n
The given set is :\r\n{'btechgeeks', 'morning', 'good'}\r\nThe New set after copying all the values from the given set is:\r\n{'btechgeeks', 'morning', 'good'}<\/pre>\n
Here all the values of the given set are copied into the new set using the copy() function.<\/pre>\n
Program for Set copy() Method in Python<\/h2>\n
\n
set.copy()<\/pre>\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the set as static input and initialize it with some random values.\r\n# Store it in a variable.\r\ngven_set = {'hello', 'this', 'is', 'btechgeeks'}\r\n# Print the above-given set.\r\nprint(\"The given set is :\")\r\nprint(gven_set)\r\n# Apply copy() function to the given set to copy all its elements into the new set.\r\n# Store it in another variable.\r\ngvnset_copy = gven_set.copy()\r\n# Print the above new set after copying all the values from the given set\r\nprint(\"The New set after copying all the values from the given set is:\")\r\nprint(gvnset_copy)\r\n<\/pre>\n
The given set is :\r\n{'this', 'hello', 'btechgeeks', 'is'}\r\nThe New set after copying all the values from the given set is:\r\n{'this', 'hello', 'btechgeeks', 'is'}<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the set as user input using the set(), input(), and split() functions.\r\n# Store it in a variable.\r\ngven_set = set(input(\"Enter some random values separated by spaces = \").split())\r\n# Print the above-given set.\r\nprint(\"The given set is :\")\r\nprint(gven_set)\r\n# Apply copy() function to the given set to copy all its elements into the new set.\r\n# Store it in another variable.\r\ngvnset_copy = gven_set.copy()\r\n# Print the above new set after copying all the values from the given set\r\nprint(\"The New set after copying all the values from the given set is:\")\r\nprint(gvnset_copy)\r\n<\/pre>\n
Enter some random values separated by spaces = good morning btechgeeks\r\nThe given set is :\r\n{'morning', 'btechgeeks', 'good'}\r\nThe New set after copying all the values from the given set is:\r\n{'morning', 'btechgeeks', 'good'}<\/pre>\n","protected":false},"excerpt":{"rendered":"