{"id":2936,"date":"2018-03-10T12:32:38","date_gmt":"2018-03-10T12:32:38","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2936"},"modified":"2018-03-13T06:59:22","modified_gmt":"2018-03-13T06:59:22","slug":"python-2-7-fundamentals-collections-sets","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2018\/03\/10\/python-2-7-fundamentals-collections-sets\/","title":{"rendered":"Python 2.7 &#8211; Fundamentals &#8211; Collections &#8211; Sets"},"content":{"rendered":"<p>A set is an unordered collection of mixed data types. No duplicates can be there in a set. Sets are declared by enclosing the items in curly brackets with each item separated by a comma.<\/p>\n<h3><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-3046\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/set1.png\" alt=\"\" width=\"607\" height=\"100\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/set1.png 607w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/set1-300x49.png 300w\" sizes=\"auto, (max-width: 607px) 100vw, 607px\" \/><\/h3>\n<h3><\/h3>\n<h3><\/h3>\n<h3><\/h3>\n<h3>Operators<\/h3>\n<p>The following operators work on sets<\/p>\n<ul>\n<li>in &#8211; membership<\/li>\n<li>len &#8211; length<\/li>\n<li>| &#8211; or<\/li>\n<li>&amp; &#8211; and<\/li>\n<li>\u00a0&#8211; &#8211; minus<\/li>\n<li>&lt;= &#8211; equivalence<\/li>\n<\/ul>\n<p>They are examined in detail below:<\/p>\n<p><strong>in &#8211; membership<\/strong><\/p>\n<p>Checks if an item exists in the set<\/p>\n<p><strong>len &#8211; length of set<\/strong><\/p>\n<p>Returns the length of a set. If set has no items then the length is zero<\/p>\n<p><strong>\u00a0| &#8211; logical or set1 | set2<\/strong><\/p>\n<p>Combines items of set1 and set2 to create a new set with all the elements of both the sets. Duplicates are ignored<\/p>\n<p><strong>&amp; &#8211; logical and set1 &amp; set2<\/strong><\/p>\n<p>Combines items of set1 and set2 to create a new set with only the common elements in both the sets<\/p>\n<p><strong>\u00a0&#8211; minus \u00a0set1 &#8211; set2<\/strong><\/p>\n<p>Returns all the items in set1 which are not in set2<\/p>\n<p><strong>&lt;= equivalence set1 &lt;= set2<\/strong><\/p>\n<p>Checks whether all items in set1 are in set2<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-3047\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/set2.png\" alt=\"\" width=\"597\" height=\"215\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/set2.png 597w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/set2-300x108.png 300w\" sizes=\"auto, (max-width: 597px) 100vw, 597px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3>Methods<\/h3>\n<p>Sets also support methods which are the same as their mathematical counterparts, which are shown below:<\/p>\n<ul>\n<li>union<\/li>\n<li>intersection<\/li>\n<li>difference<\/li>\n<li>issubset<\/li>\n<li>add<\/li>\n<li>remove<\/li>\n<li>pop<\/li>\n<li>clear<\/li>\n<\/ul>\n<p><strong>union &#8211; set1.union(set2)<\/strong><\/p>\n<p>Returns elements from both set1 and set2. Duplicates are ignored<\/p>\n<p><strong>intersection &#8211; set1.intersection(set1)<\/strong><\/p>\n<p>Returns only common elements from both set1 and set2.<\/p>\n<p><strong>difference &#8211; set1.difference(set2)<\/strong><\/p>\n<p>Returns elements in set1 which are not in set2.<\/p>\n<p><strong>issubset &#8211; set1.issubset(set2)<\/strong><\/p>\n<p>Returns True if all elements of set1 are in set2, else False<\/p>\n<p><strong>add &#8211; set1.add(item)<\/strong><\/p>\n<p>Adds an item to a set. If an item already exists it is not added<\/p>\n<p><strong>remove &#8211; set1.remove(item)<\/strong><\/p>\n<p>Removes an item from the set. If item does not exist then an error is thrown<\/p>\n<p><strong>pop &#8211; set1.pop()<\/strong><\/p>\n<p>Removes an arbitrary item from the set. Since a set is not ordered, there is no guarantee on which item will be removed<\/p>\n<p><strong>clear &#8211; set1.clear()<\/strong><\/p>\n<p>Removes all the elements from a set<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-3050\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/set3.png\" alt=\"\" width=\"609\" height=\"353\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/set3.png 609w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2018\/03\/set3-300x174.png 300w\" sizes=\"auto, (max-width: 609px) 100vw, 609px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>The last collection we will look at are<a href=\"https:\/\/truelogic.org\/wordpress\/2018\/03\/13\/python-2-7-fundamentals-collections-dictionaries\/\" target=\"_blank\" rel=\"noopener\"> dictionaries<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>A set is an unordered collection of mixed data types. No duplicates can be there in a set. Sets are declared by enclosing the items <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2018\/03\/10\/python-2-7-fundamentals-collections-sets\/\" title=\"Python 2.7 &#8211; Fundamentals &#8211; Collections &#8211; Sets\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2107,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[297],"tags":[],"class_list":["post-2936","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2936","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2936"}],"version-history":[{"count":8,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2936\/revisions"}],"predecessor-version":[{"id":3064,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2936\/revisions\/3064"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2107"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2936"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2936"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2936"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}