{"id":8332,"date":"2024-03-20T06:27:43","date_gmt":"2024-03-20T06:27:43","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=8332"},"modified":"2024-03-20T06:27:43","modified_gmt":"2024-03-20T06:27:43","slug":"python-sets","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/python-sets\/","title":{"rendered":"Python &#8211; Sets"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#python-sets\">Python &#8211; Sets<\/a><\/li><li ><a href=\"#what-is-set-in-python\">What is Set in Python?<\/a><\/li><li ><a href=\"#what-are-the-examples-of-python-sets\">What are the examples of Python sets?<\/a><\/li><li ><a href=\"#example-2-to-illustrate-that-the-duplicates-are-not-allowed-in-python-sets\">Example 2 &#8211; to illustrate that the duplicates are not allowed in Python sets<\/a><\/li><li ><a href=\"#example-3-to-determine-the-length-of-python-sets\">Example 3-  to determine the length of Python sets<\/a><\/li><li ><a href=\"#example-4-to-create-the-python-set-with-items-of-different-data-types\">Example 4- to create the Python set with items of different data types<\/a><\/li><li ><a href=\"#what-is-the-set-constructor\">What is the Set() Constructor?<\/a><\/li><li ><a href=\"#what-is-the-example-for-a-set-constructor\">What is the example for a set constructor?<\/a><\/li><li ><a href=\"#conclusion\">Conclusion<\/a><\/li><li ><a href=\"#python-sets-fa-qs\">Python &#8211; Sets &#8211; FAQs<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-sets\">Python &#8211; Sets<\/h2>\n\n\n\n<p>The set is one of the<a href=\"https:\/\/www.skillvertex.com\/blog\/python-built-in-functions\/\" data-type=\"post\" data-id=\"7464\"> built-in data <\/a>types in Python. Read this article to learn more about the <strong>Python Sets<\/strong>.<\/p>\n\n\n\n<p>However, in mathematics, a set is the collection of data types including a <a href=\"https:\/\/www.skillvertex.com\/blog\/python-list-comprehension\/\" data-type=\"post\" data-id=\"8090\">list <\/a>or tuple. A set object consists of a collection of one or more immutable objects that will be enclosed by the curly brackets{}.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-set-in-python\">What is Set in Python?<\/h2>\n\n\n\n<p>The<strong> Python set <\/strong>will allow you to store multiple items in a single <a href=\"https:\/\/www.skillvertex.com\/blog\/initialization-of-static-variables-in-c\/\" data-type=\"post\" data-id=\"3034\">variable<\/a>. Thus, the set is among the 4 built-in data types in <a href=\"https:\/\/www.skillvertex.com\/blog\/python-tuple-methods\/\" data-type=\"post\" data-id=\"8306\">Python <\/a>that will store the collection of data.<\/p>\n\n\n\n<p>Furthermore, the Python set will indicate an unordered, unchangeable, and unindexed collection. It won&#8217;t allow any duplicate values. In simple terms, unordered will refer to the items with no defined order in the Python sets. So, it will show in a different order each time you need to use them and won&#8217;t be referred by the <a href=\"https:\/\/www.skillvertex.com\/blog\/which-of-the-following-is-valid-sql-for-an-index\/\" data-type=\"post\" data-id=\"1479\">index <\/a>or <a href=\"https:\/\/www.skillvertex.com\/blog\/python-keyword-arguments\/\" data-type=\"post\" data-id=\"7283\">key<\/a>. The set items are unchangeable.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-are-the-examples-of-python-sets\">What are the examples of Python sets?<\/h2>\n\n\n\n<p>Let us look into examples of <a href=\"https:\/\/www.skillvertex.com\/blog\/python-loop-tuples\/\" data-type=\"post\" data-id=\"8198\">Python <\/a>sets provided below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Creating two sets\nset1 = {1, 2, 3, 4, 5}\nset2 = {4, 5, 6, 7, 8}\n\n# Output: Elements of set1\nprint(\"Set 1:\", set1)\n\n# Output: Elements of set2\nprint(\"Set 2:\", set2)\n\n# Adding an element to set1\nset1.add(6)\n# Output: Updated set1\nprint(\"After adding 6 to set 1:\", set1)\n\n# Removing an element from set2\nset2.remove(8)\n# Output: Updated set2\nprint(\"After removing 8 from set 2:\", set2)\n\n# Union of set1 and set2\nunion_set = set1.union(set2)\n# Output: Union of set1 and set2\nprint(\"Union of set1 and set2:\", union_set)\n\n# Intersection of set1 and set2\nintersection_set = set1.intersection(set2)\n# Output: Intersection of set1 and set2\nprint(\"Intersection of set1 and set2:\", intersection_set)\n\n# Difference between set1 and set2\ndifference_set = set1.difference(set2)\n# Output: Difference between set1 and set2\nprint(\"Difference of set1 and set2:\", difference_set)\n\n# Checking if set1 is a subset of set2\nis_subset = set1.issubset(set2)\n# Output: Whether set1 is a subset of set2\nprint(\"Is set1 a subset of set2:\", is_subset)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Set 1: {1, 2, 3, 4, 5}\nSet 2: {4, 5, 6, 7, 8}\nAfter adding 6 to set 1: {1, 2, 3, 4, 5, 6}\nAfter removing 8 from set 2: {4, 5, 6, 7}\nUnion of set1 and set2: {1, 2, 3, 4, 5, 6, 7}\nIntersection of set1 and set2: {4, 5, 6}\nDifference of set1 and set2: {1, 2, 3}\nIs set1 a subset of set2: False\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-2-to-illustrate-that-the-duplicates-are-not-allowed-in-python-sets\">Example 2 &#8211; to illustrate that the duplicates are not allowed in Python sets<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># Creating a set with duplicate elements\nmy_set = {1, 2, 3, 3, 4, 4, 5}\n\n# Output: The set with duplicates\nprint(\"Set with duplicates:\", my_set)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Set with duplicates: {1, 2, 3, 4, 5}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-3-to-determine-the-length-of-python-sets\">Example 3-  to determine the length of Python sets<\/h2>\n\n\n\n<p>Check out the example below to determine the length of Python sets<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Creating a set\nmy_set = {1, 2, 3, 4, 5}\n\n# Getting the length of the set\nset_length = len(my_set)\n\n# Output: Length of the set\nprint(\"Length of the set:\", set_length)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Length of the set: 5\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-4-to-create-the-python-set-with-items-of-different-data-types\">Example 4- to create the Python set with items of different data types<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># Creating a set with items of different data types\nmixed_set = {1, 2.5, 'apple', (3, 4)}\n\n# Output: The set with different data types\nprint(\"Set with different data types:\", mixed_set)\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Set with different data types: {1, 2.5, 'apple', (3, 4)}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-set-constructor\">What is the Set() Constructor?<\/h2>\n\n\n\n<p>The Set()  constructor will help you to create the Python sets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"what-is-the-example-for-a-set-constructor\">What is the example for a set constructor?<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code># Creating a set using the set constructor\nmy_set = set(&#91;1, 2, 3, 4, 5])\n\n# Output: The created set\nprint(\"Created set using set constructor:\", my_set)\n\n<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Created set using set constructor: {1, 2, 3, 4, 5}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"conclusion\">Conclusion<\/h2>\n\n\n\n<p>In Python, sets are a powerful data structure that stores unique elements. They&#8217;re like lists, but they don&#8217;t allow duplicates. Sets are defined using curly braces {}, and you can create a set using either curly braces directly or the set() constructor. <\/p>\n\n\n\n<p>Hence, a set will allow you to perform various operations with sets like adding elements, removing elements, finding intersections, unions, and differences, and checking for subsets.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"python-sets-fa-qs\">Python &#8211; Sets &#8211; FAQs<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1710914545970\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1.What is set and subset in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. A subset indicates the collection of elements that will belong to another set. In contrast, the set is the collection of data types.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1710914553461\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2.Are Python sets unique?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Yes, set elements are unique<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1710914560883\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3.Is set a class in Python?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. Yes, in Python, a set is a built-in data type and is an unordered collection of unique elements. It is implemented as a class in Python.<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Python &#8211; Sets The set is one of the built-in data types in Python. Read this article to learn more about the Python Sets. However, in mathematics, a set is the collection of data types including a list or tuple. A set object consists of a collection of one or more immutable objects that will &#8230; <a title=\"Python &#8211; Sets\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/python-sets\/\" aria-label=\"More on Python &#8211; Sets\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":8335,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[864],"tags":[990,932,916],"class_list":["post-8332","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorial","tag-python-sets","tag-python-functions","tag-python-programming","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=8332"}],"version-history":[{"count":6,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8332\/revisions"}],"predecessor-version":[{"id":8339,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/8332\/revisions\/8339"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/8335"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=8332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=8332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=8332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}