{"id":31649,"date":"2024-11-01T09:01:17","date_gmt":"2024-11-01T09:01:17","guid":{"rendered":"http:\/\/atmokpo.com\/w\/?p=31649"},"modified":"2024-11-01T11:48:48","modified_gmt":"2024-11-01T11:48:48","slug":"python-data-types-sets","status":"publish","type":"post","link":"https:\/\/atmokpo.com\/w\/31649\/","title":{"rendered":"Python Data Types &#8211; Sets"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Python Set Data Type<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">In Python, a <strong>Set<\/strong> is an <strong>unordered data type<\/strong> that represents a collection of unique values. Sets are defined using curly braces <code>{}<\/code>, and each element is unique. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_set = {1, 2, 3, 4, 5}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Characteristics of Sets<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. No Duplicates Allowed<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Since sets <strong>do not allow duplicate values<\/strong>, if the same value is added multiple times, only one instance is stored.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_set = {1, 2, 2, 3, 4}\nprint(my_set)  # {1, 2, 3, 4}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. Unordered<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Since sets are an <strong>unordered type<\/strong>, they do not support indexing or slicing. To access elements of a set, you must use a loop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_set = {\"apple\", \"banana\", \"cherry\"}\nfor item in my_set:\n    print(item)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">3. Adding and Removing Elements in a Set<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Sets are <strong>mutable<\/strong>, which means you can add or remove elements. You can use the <code>add()<\/code> method to add elements, and <code>remove()<\/code> or <code>discard()<\/code> methods to remove them.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_set = {1, 2, 3}\nmy_set.add(4)            # {1, 2, 3, 4}\nmy_set.remove(2)         # {1, 3, 4}\nmy_set.discard(5)        # {1, 3, 4} (no error when removing a non-existent element)\nprint(my_set)<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. Set Operations<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Sets support various operations such as union, intersection, and difference. These operations can be performed using the <code>|<\/code>, <code>&amp;<\/code>, and <code>-<\/code> operators or methods.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set1 = {1, 2, 3, 4}\nset2 = {3, 4, 5, 6}\n\n# Union\nunion_set = set1 | set2\nprint(union_set)  # {1, 2, 3, 4, 5, 6}\n\n# Intersection\nintersection_set = set1 &amp; set2\nprint(intersection_set)  # {3, 4}\n\n# Difference\ndifference_set = set1 - set2\nprint(difference_set)  # {1, 2}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. Set Methods<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Sets offer various methods for easy manipulation of elements:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>set.add(x)<\/code>: Adds an element to the set.<\/li>\n\n\n\n<li><code>set.remove(x)<\/code>: Removes a specific element from the set, and raises an error if the element is not present.<\/li>\n\n\n\n<li><code>set.discard(x)<\/code>: Removes a specific element from the set, and does not raise an error if the element is not present.<\/li>\n\n\n\n<li><code>set.union(other_set)<\/code>: Returns the union of two sets.<\/li>\n\n\n\n<li><code>set.intersection(other_set)<\/code>: Returns the intersection of two sets.<\/li>\n\n\n\n<li><code>set.difference(other_set)<\/code>: Returns the difference of two sets.<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>set1 = {1, 2, 3}\nset2 = {3, 4, 5}\n\nset1.add(6)\nprint(set1)  # {1, 2, 3, 6}\n\nset1.discard(2)\nprint(set1)  # {1, 3, 6}\n\nunion = set1.union(set2)\nprint(union)  # {1, 3, 4, 5, 6}\n\nintersection = set1.intersection(set2)\nprint(intersection)  # {3}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">6. Applications of Sets<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Sets are useful in various situations such as removing duplicate values or finding common elements through operations between multiple sets. For example, to remove duplicates from a list, you can convert it to a set.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_list = [1, 2, 2, 3, 4, 4, 5]\nmy_set = set(my_list)\nprint(my_set)  # {1, 2, 3, 4, 5}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Sets represent a collection of unique values and do not allow duplicate values.<\/li>\n\n\n\n<li>Sets are unordered, so they do not support indexing or slicing.<\/li>\n\n\n\n<li>You can manipulate set elements using methods such as <code>add()<\/code>, <code>remove()<\/code>, and <code>discard()<\/code>.<\/li>\n\n\n\n<li>Sets support operations such as union, intersection, and difference, which allow for easy analysis of data relationships.<\/li>\n\n\n\n<li>Sets can be used to remove duplicate values or find common elements, among other tasks.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Sets are one of the most useful data types in Python, particularly suited for handling duplicate data or performing set operations. Utilize the various features of sets to manage data efficiently!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python Set Data Type In Python, a Set is an unordered data type that represents a collection of unique values. Sets are defined using curly braces {}, and each element is unique. For example: Characteristics of Sets 1. No Duplicates Allowed Since sets do not allow duplicate values, if the same value is added multiple &hellip; <a href=\"https:\/\/atmokpo.com\/w\/31649\/\" class=\"more-link\">\ub354 \ubcf4\uae30<span class=\"screen-reader-text\"> &#8220;Python Data Types &#8211; Sets&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[98],"tags":[95],"class_list":["post-31649","post","type-post","status-publish","format-standard","hentry","category--en","tag--en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Data Types - Sets - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/atmokpo.com\/w\/31649\/\" \/>\n<meta property=\"og:locale\" content=\"ko_KR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Data Types - Sets - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"og:description\" content=\"Python Set Data Type In Python, a Set is an unordered data type that represents a collection of unique values. Sets are defined using curly braces {}, and each element is unique. For example: Characteristics of Sets 1. No Duplicates Allowed Since sets do not allow duplicate values, if the same value is added multiple &hellip; \ub354 \ubcf4\uae30 &quot;Python Data Types &#8211; Sets&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/atmokpo.com\/w\/31649\/\" \/>\n<meta property=\"og:site_name\" content=\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\" \/>\n<meta property=\"article:published_time\" content=\"2024-11-01T09:01:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-11-01T11:48:48+00:00\" \/>\n<meta name=\"author\" content=\"root\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:site\" content=\"@bebubo4\" \/>\n<meta name=\"twitter:label1\" content=\"\uae00\uc4f4\uc774\" \/>\n\t<meta name=\"twitter:data1\" content=\"root\" \/>\n\t<meta name=\"twitter:label2\" content=\"\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04\" \/>\n\t<meta name=\"twitter:data2\" content=\"2\ubd84\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/atmokpo.com\/w\/31649\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31649\/\"},\"author\":{\"name\":\"root\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\"},\"headline\":\"Python Data Types &#8211; Sets\",\"datePublished\":\"2024-11-01T09:01:17+00:00\",\"dateModified\":\"2024-11-01T11:48:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31649\/\"},\"wordCount\":351,\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"keywords\":[\"\ud30c\uc774\uc36c\uac15\uc88c\"],\"articleSection\":[\"Python Study\"],\"inLanguage\":\"ko-KR\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/atmokpo.com\/w\/31649\/\",\"url\":\"https:\/\/atmokpo.com\/w\/31649\/\",\"name\":\"Python Data Types - Sets - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"isPartOf\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#website\"},\"datePublished\":\"2024-11-01T09:01:17+00:00\",\"dateModified\":\"2024-11-01T11:48:48+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/atmokpo.com\/w\/31649\/#breadcrumb\"},\"inLanguage\":\"ko-KR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/atmokpo.com\/w\/31649\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/atmokpo.com\/w\/31649\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\ud648\",\"item\":\"https:\/\/atmokpo.com\/w\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Data Types &#8211; Sets\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/atmokpo.com\/w\/#website\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/atmokpo.com\/w\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"ko-KR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/atmokpo.com\/w\/#organization\",\"name\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\",\"url\":\"https:\/\/atmokpo.com\/w\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"contentUrl\":\"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png\",\"width\":400,\"height\":400,\"caption\":\"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8\"},\"image\":{\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/bebubo4\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7\",\"name\":\"root\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"ko-KR\",\"@id\":\"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g\",\"caption\":\"root\"},\"sameAs\":[\"http:\/\/atmokpo.com\/w\"],\"url\":\"https:\/\/atmokpo.com\/w\/author\/root\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Data Types - Sets - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/atmokpo.com\/w\/31649\/","og_locale":"ko_KR","og_type":"article","og_title":"Python Data Types - Sets - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","og_description":"Python Set Data Type In Python, a Set is an unordered data type that represents a collection of unique values. Sets are defined using curly braces {}, and each element is unique. For example: Characteristics of Sets 1. No Duplicates Allowed Since sets do not allow duplicate values, if the same value is added multiple &hellip; \ub354 \ubcf4\uae30 \"Python Data Types &#8211; Sets\"","og_url":"https:\/\/atmokpo.com\/w\/31649\/","og_site_name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","article_published_time":"2024-11-01T09:01:17+00:00","article_modified_time":"2024-11-01T11:48:48+00:00","author":"root","twitter_card":"summary_large_image","twitter_creator":"@bebubo4","twitter_site":"@bebubo4","twitter_misc":{"\uae00\uc4f4\uc774":"root","\uc608\uc0c1 \ub418\ub294 \ud310\ub3c5 \uc2dc\uac04":"2\ubd84"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/atmokpo.com\/w\/31649\/#article","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/31649\/"},"author":{"name":"root","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7"},"headline":"Python Data Types &#8211; Sets","datePublished":"2024-11-01T09:01:17+00:00","dateModified":"2024-11-01T11:48:48+00:00","mainEntityOfPage":{"@id":"https:\/\/atmokpo.com\/w\/31649\/"},"wordCount":351,"publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"keywords":["\ud30c\uc774\uc36c\uac15\uc88c"],"articleSection":["Python Study"],"inLanguage":"ko-KR"},{"@type":"WebPage","@id":"https:\/\/atmokpo.com\/w\/31649\/","url":"https:\/\/atmokpo.com\/w\/31649\/","name":"Python Data Types - Sets - \ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","isPartOf":{"@id":"https:\/\/atmokpo.com\/w\/#website"},"datePublished":"2024-11-01T09:01:17+00:00","dateModified":"2024-11-01T11:48:48+00:00","breadcrumb":{"@id":"https:\/\/atmokpo.com\/w\/31649\/#breadcrumb"},"inLanguage":"ko-KR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/atmokpo.com\/w\/31649\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/atmokpo.com\/w\/31649\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\ud648","item":"https:\/\/atmokpo.com\/w\/en\/"},{"@type":"ListItem","position":2,"name":"Python Data Types &#8211; Sets"}]},{"@type":"WebSite","@id":"https:\/\/atmokpo.com\/w\/#website","url":"https:\/\/atmokpo.com\/w\/","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","description":"","publisher":{"@id":"https:\/\/atmokpo.com\/w\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/atmokpo.com\/w\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"ko-KR"},{"@type":"Organization","@id":"https:\/\/atmokpo.com\/w\/#organization","name":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8","url":"https:\/\/atmokpo.com\/w\/","logo":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/","url":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","contentUrl":"https:\/\/atmokpo.com\/w\/wp-content\/uploads\/2024\/11\/logo.png","width":400,"height":400,"caption":"\ub77c\uc774\ube0c\uc2a4\ub9c8\ud2b8"},"image":{"@id":"https:\/\/atmokpo.com\/w\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/bebubo4"]},{"@type":"Person","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/91b6b3b138fbba0efb4ae64b1abd81d7","name":"root","image":{"@type":"ImageObject","inLanguage":"ko-KR","@id":"https:\/\/atmokpo.com\/w\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/708197b41fc6435a7ce22d951b25d4a47e9e904270cb1f04682d4f025066f80c?s=96&d=mm&r=g","caption":"root"},"sameAs":["http:\/\/atmokpo.com\/w"],"url":"https:\/\/atmokpo.com\/w\/author\/root\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31649","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/comments?post=31649"}],"version-history":[{"count":1,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31649\/revisions"}],"predecessor-version":[{"id":31650,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/posts\/31649\/revisions\/31650"}],"wp:attachment":[{"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/media?parent=31649"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/categories?post=31649"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/atmokpo.com\/w\/wp-json\/wp\/v2\/tags?post=31649"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}