{"id":1128,"date":"2024-02-24T10:49:05","date_gmt":"2024-02-24T10:49:05","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=1128"},"modified":"2024-02-24T10:49:06","modified_gmt":"2024-02-24T10:49:06","slug":"python-sets","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/02\/24\/python-sets\/","title":{"rendered":"Python\u00a0Sets"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>myset = {\"apple\",&nbsp;\"banana\",&nbsp;\"cherry\"}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Set<\/h2>\n\n\n\n<p>Sets are used to store multiple items in a single variable.<\/p>\n\n\n\n<p>Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are\u00a0List,\u00a0Tuple, and\u00a0Dictionary, all with different qualities and usage.<\/p>\n\n\n\n<p>A set is a collection which is&nbsp;<em>unordered<\/em>,&nbsp;<em>unchangeable*<\/em>, and&nbsp;<em>unindexed<\/em>.<\/p>\n\n\n\n<p><strong>* Note:<\/strong>&nbsp;Set&nbsp;<em>items<\/em>&nbsp;are unchangeable, but you can remove items and add new items.<\/p>\n\n\n\n<p>Sets are written with curly brackets.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ExampleGet your own Python Server<\/h3>\n\n\n\n<p>Create a Set:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thisset = {\"apple\",\u00a0\"banana\",\u00a0\"cherry\"}<br>print(thisset)<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;Sets are unordered, so you cannot be sure in which order the items will appear.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Set Items<\/h2>\n\n\n\n<p>Set items are unordered, unchangeable, and do not allow duplicate values.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Unordered<\/h2>\n\n\n\n<p>Unordered means that the items in a set do not have a defined order.<\/p>\n\n\n\n<p>Set items can appear in a different order every time you use them, and cannot be referred to by index or key.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Unchangeable<\/h2>\n\n\n\n<p>Set items are unchangeable, meaning that we cannot change the items after the set has been created.<\/p>\n\n\n\n<p>Once a set is created, you cannot change its items, but you can remove items and add new items.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Duplicates Not Allowed<\/h2>\n\n\n\n<p>Sets cannot have two items with the same value.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Duplicate values will be ignored:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thisset = {\"apple\",\u00a0\"banana\",\u00a0\"cherry\",\u00a0\"apple\"}<br><br>print(thisset)<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;The values&nbsp;<code>True<\/code>&nbsp;and&nbsp;<code>1<\/code>&nbsp;are considered the same value in sets, and are treated as duplicates:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p><code>True<\/code>&nbsp;and&nbsp;<code>1<\/code>&nbsp;is considered the same value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thisset = {\"apple\",\u00a0\"banana\",\u00a0\"cherry\",\u00a0True,\u00a01,\u00a02}<br><br>print(thisset)<\/code><\/pre>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;The values&nbsp;<code>False<\/code>&nbsp;and&nbsp;<code>0<\/code>&nbsp;are considered the same value in sets, and are treated as duplicates:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p><code>False<\/code>&nbsp;and&nbsp;<code>0<\/code>&nbsp;is considered the same value:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thisset = {\"apple\",\u00a0\"banana\",\u00a0\"cherry\",\u00a0False,\u00a0True,\u00a00}<br><br>print(thisset)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Get the Length of a Set<\/h2>\n\n\n\n<p>To determine how many items a set has, use the&nbsp;<code>len()<\/code>&nbsp;function.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Get the number of items in a set:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thisset = {\"apple\",\u00a0\"banana\",\u00a0\"cherry\"}<br><br>print(len(thisset))<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Set Items &#8211; Data Types<\/h2>\n\n\n\n<p>Set items can be of any data type:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>String, int and boolean data types:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set1 = {\"apple\",\u00a0\"banana\",\u00a0\"cherry\"}<br>set2 = {1,\u00a05,\u00a07,\u00a09,\u00a03}<br>set3 = {True,\u00a0False,\u00a0False}<\/code><\/pre>\n\n\n\n<p>A set can contain different data types:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>A set with strings, integers and boolean values:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>set1 = {\"abc\",\u00a034,\u00a0True,\u00a040,\u00a0\"male\"}\n\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">type()<\/h2>\n\n\n\n<p>From Python&#8217;s perspective, sets are defined as objects with the data type &#8216;set&#8217;:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;class 'set'&gt;<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>What is the data type of a set?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>myset = {\"apple\",\u00a0\"banana\",\u00a0\"cherry\"}<br>print(type(myset))<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">The set() Constructor<\/h2>\n\n\n\n<p>It is also possible to use the&nbsp;set()&nbsp;constructor to make a set.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<p>Using the set() constructor to make a set:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>thisset = set((\"apple\",\u00a0\"banana\",\u00a0\"cherry\"))\u00a0# note the double round-brackets<br>print(thisset)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Set Sets are used to store multiple items in a single variable. Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are\u00a0List,\u00a0Tuple, and\u00a0Dictionary, all with different qualities and usage. A set is a collection which is&nbsp;unordered,&nbsp;unchangeable*, and&nbsp;unindexed. * Note:&nbsp;Set&nbsp;items&nbsp;are unchangeable, but you can remove items [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[107],"tags":[],"class_list":["post-1128","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/1128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=1128"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/1128\/revisions"}],"predecessor-version":[{"id":1129,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/1128\/revisions\/1129"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=1128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=1128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=1128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}