

{"id":5771,"date":"2018-01-09T10:04:46","date_gmt":"2018-01-09T04:34:46","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=5771"},"modified":"2026-04-21T14:40:29","modified_gmt":"2026-04-21T09:10:29","slug":"python-set-and-booleans-with-examples","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/","title":{"rendered":"Python Set and Booleans with Syntax and Examples"},"content":{"rendered":"<p>So far, we have learned about various data types in Python. We dealt with strings, numbers, lists, tuples, and dictionaries.<\/p>\n<p>We\u2019ve also learned that we don\u2019t need to declare the type of data while defining it. Today, we will talk about Python set examples and Python Booleans.<\/p>\n<p>After that, we will move on to Python functions in further lessons.<\/p>\n<h3>What are Sets in Python?<\/h3>\n<p>First, we focus on Python sets. A set in Python holds a sequence of values. It is sequenced but does not support indexing.<\/p>\n<p>We will understand that as we get deeper into the article with the Python set Examples.<\/p>\n<h4>1. Creating a Python Set<\/h4>\n<p>To declare a set, you need to type a sequence of items separated by commas, inside curly braces. After that, assign it to a Python variable.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a={1,3,2}<\/pre>\n<p>As you can see, we wrote it in the order 1, 3, 2. In point b, we will access this set and see what we get back.<\/p>\n<p>A set may contain values of different types.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; c={1,2.0,'three'}<\/pre>\n<p><strong>a. Duplicate Elements<\/strong><\/p>\n<p>A set also cannot contain duplicate elements. Let\u2019s try adding duplicate elements to another set, and then access it in point b.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; b={3,2,1,2}<\/pre>\n<p><strong>b. Mutability<\/strong><\/p>\n<p>A set is mutable, but may not contain mutable items like a list, set, or even a dictionary.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; d={[1,2,3],4}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#9&gt;&#8221;, line 1, in &lt;module&gt;d={[1,2,3],4}<\/p>\n<p>TypeError: unhashable type: &#8216;list&#8217;<\/p>\n<\/div>\n<p>As we discussed, there is no such thing as a nested Python set.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; d={{1,3,2},4}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#10&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>d={{1,3,2},4}<\/p>\n<p>TypeError: unhashable type: &#8216;set&#8217;<\/p>\n<p><strong>c. The Python set() function<\/strong><\/p>\n<\/div>\n<p>You can also create a set with the set() function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; d=set()\r\n&gt;&gt;&gt; type(d)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class \u2018set\u2019&gt;<\/div>\n<p>This creates an empty set object. Remember that if you declare an empty set as the following code, it is an empty dictionary, not an empty set. We confirm this using the type() function.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; d={}\r\n&gt;&gt;&gt; type(d)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class &#8216;dict&#8217;&gt;<\/div>\n<p>The set() function may also take one argument, however. It should be an iterable, like a list.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; d=set([1,3,2])<\/pre>\n<h4>2. Accessing a Set in Python<\/h4>\n<p>Since sets in Python do not support indexing, it is only possible to access the entire set at once. Let\u2019s try accessing the sets from point a.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; a<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 3}<\/div>\n<p>Did you see how it reordered the elements into an ascending order? Now let\u2019s try accessing the set c.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; c<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2.0, &#8216;three&#8217;}<\/div>\n<p>Finally, let\u2019s access set b.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; b<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 3}<\/div>\n<p>As you can see, we had two 2s when we declared the set, but now we have only one, and it automatically reordered the set.<\/p>\n<p>Also, since sets do not support indexing, they cannot be sliced. Let\u2019s try slicing one.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; b[1:]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#26&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>b[1:]<\/p>\n<p>TypeError: &#8216;set&#8217; object is not subscriptable<\/p>\n<\/div>\n<p>As you can see in the error, a set object is not subscriptable.<\/p>\n<h4>3. Deleting a Set in Python<\/h4>\n<p>Again, because a set isn\u2019t indexed, you can\u2019t delete an element using its index. So for this, you must use one of the following methods.<\/p>\n<p>A method must be called on a set, and it may alter the set. For the following examples, let\u2019s take a set called numbers.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; numbers={3,2,1,4,6,5}\r\n&gt;&gt;&gt; numbers<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 3, 4, 5, 6}<\/div>\n<p><strong>a. discard() method in Python<\/strong><\/p>\n<p>This method takes the item to delete as an argument.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; numbers.discard(3)\r\n&gt;&gt;&gt; numbers<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 4, 5, 6}<\/div>\n<p>As you can see in the resulting set, item 3 has been removed.<\/p>\n<p><strong>b. remove() method in Python<\/strong><\/p>\n<p>Like the discard() method, remove() deletes an item from the set.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; numbers.remove(5)\r\n&gt;&gt;&gt; numbers<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 4, 6}<\/div>\n<p><strong>discard() vs remove():<\/strong><\/p>\n<p>These two methods may appear the same to you, but there\u2019s actually a difference.<\/p>\n<p>If you try deleting an item that doesn\u2019t exist in the set, discard() ignores it, but remove() raises a KeyError.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; numbers.discard(7)\r\n&gt;&gt;&gt; numbers<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 4, 6}<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; numbers.remove(7)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#37&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>numbers.remove(7)<\/p>\n<p>KeyError: 7<\/p>\n<\/div>\n<p><strong>c. pop() method in Python set<\/strong><\/p>\n<p>Like in a dictionary, you can call the pop() method on a set. However, here, it does not take an argument.<\/p>\n<p>Because a set doesn\u2019t support indexing, there is absolutely no way to pass an index to the pop method. Hence, it pops out an arbitrary item.<\/p>\n<p>Furthermore, it prints out the item that was popped.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; numbers.pop()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<p>Let\u2019s try popping anot\/her element.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; numbers.pop()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<p>Let\u2019s try it on another set as well.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; {2,1,3}.pop()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<p><strong>d. clear() method in Python set<\/strong><\/p>\n<p>Like the pop method(), the clear() method for a dictionary can be applied to a Python set as well. It empties the set in Python.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; numbers.clear()\r\n&gt;&gt;&gt; numbers<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">set()<\/div>\n<p>As you can see, it denoted an empty set as set(), not as {}.<\/p>\n<h4>4. Updating a Set in Python<\/h4>\n<p>As we discussed, a Python set is mutable. But as we have seen earlier, we can\u2019t use indices to reassign it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; numbers={3,1,2,4,6,5}\r\n&gt;&gt;&gt; numbers[3]<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#56&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>numbers[3]<\/p>\n<p>TypeError: &#8216;set&#8217; object does not support indexing<\/p>\n<\/div>\n<p>So, we use two methods for this purpose- add() and update(). We have seen the update() method on tuples, lists, and strings.<\/p>\n<p><strong>a. add() method in Python set<\/strong><\/p>\n<p>It takes as an argument the item to be added to the set.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; numbers.add(3.5)\r\n&gt;&gt;&gt; numbers<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 3, 4, 5, 6, 3.5}<\/div>\n<p>If you add an existing item in the set, the set remains unaffected.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; numbers.add(4)\r\n&gt;&gt;&gt; numbers<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 3, 4, 5, 6, 3.5}<\/div>\n<p><strong>b. update() method in Python set<\/strong><\/p>\n<p>This method can add multiple items to the set at once, which it takes as arguments.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; numbers.update([7,8],{1,2,9})\r\n&gt;&gt;&gt; numbers<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 3, 4, 5, 6, 3.5, 7, 8, 9}<\/div>\n<p>As is visible, we could provide a list and a set as arguments to this. This is because this is different than creating a set.<\/p>\n<h4>5. Python Functions on Sets<\/h4>\n<p>A function is something that you can apply to a Python set, and it performs operations on it and returns a value. Let\u2019s talk about some of the functions that a set supports.<\/p>\n<p>We\u2019ll take a new set for exemplary purposes.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; days={'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'}<\/pre>\n<p><strong>a. len() function in Python set<\/strong><\/p>\n<p>The len() function returns the length of a set. This is the number of elements in it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; len(days)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">7<\/div>\n<p><strong>b. max() function in Python set<\/strong><\/p>\n<p>This function returns the item from the set with the highest value.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; max({3,1,2})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">3<\/div>\n<p>We can make such a comparison on strings as well.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; max(days)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Wednesday&#8217;<\/div>\n<p>The Python function returned \u2018Wednesday\u2019 because W has the highest ASCII value among M, T, W, F, and S.<\/p>\n<p>But we cannot compare values of different types.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; max({1,2,'three','Three'})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#69&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>max({1,2,&#8217;three&#8217;,&#8217;Three&#8217;})<\/p>\n<p>TypeError: &#8216;&gt;&#8217; not supported between instances of &#8216;str&#8217; and &#8216;int&#8217;<\/p>\n<\/div>\n<p><strong>c. min() function in Python set<\/strong><\/p>\n<p>Like the max() function, the min() function returns the item in the Python set with the lowest value.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; min(days)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&#8216;Friday&#8217;<\/div>\n<p>This is because F has the lowest ASCII value among M, T, W, F, and S.<\/p>\n<p><strong>d. sum() function in Python set<\/strong><\/p>\n<p>The sum() functionin Python set returns the arithmetic sum of all the items in a set.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sum({1,2,3})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">6<\/div>\n<p>However, you can\u2019t apply it to a set that contains strings.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; sum(days)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#72&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>sum(days)<\/p>\n<p>TypeError: unsupported operand type(s) for +: &#8216;int&#8217; and &#8216;str&#8217;<\/p>\n<\/div>\n<p><strong>e. any() function in Python set<\/strong><\/p>\n<p>This function returns True even if one item in the set has a Boolean value of True.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; any({0})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; any({0,'0'})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>It returns True because the string \u20180\u2019 has a Boolean value of True.<\/p>\n<p><strong>f. all() function in Python set<\/strong><\/p>\n<p>Unlike the any() function, all() returns True only if all items in the Python set have a Boolean value of True. Otherwise, it returns False.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; all({0,'0'})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; all(days)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p><strong>g. sorted() function in Python set<\/strong><\/p>\n<p>The sorted() function returns a sorted python set to list. It is sorted in ascending order, but it doesn\u2019t modify the original set.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; numbers={1, 2, 3, 4, 5, 6, 3.5}\r\n&gt;&gt;&gt; sorted(numbers)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[1, 2, 3, 3.5, 4, 5, 6]<\/div>\n<h4>6. Python Methods on Sets<\/h4>\n<p>Unlike a function in Python set, a method may alter a set. It performs a sequence on operations on a set, and must be called on it.<\/p>\n<p>So far, we have learned about the methods add(), clear(), discard(), pop(), remove(), and update(). Now, we will see more methods from a more mathematical point of view.<\/p>\n<p><strong>a. union() method in Python set<\/strong><\/p>\n<p>This method performs the union operation on two or more Python sets. What it does is it returns all the items that are in any of those sets.<\/p>\n<div id=\"attachment_5774\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Pythin-union.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5774\" class=\"wp-image-5774 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Pythin-union.jpg\" alt=\"Python Union\" width=\"400\" height=\"260\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Pythin-union.jpg 400w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Pythin-union-150x98.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Pythin-union-300x195.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><p id=\"caption-attachment-5774\" class=\"wp-caption-text\">Union in Python Set<\/p><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; set1,set2,set3={1,2,3},{3,4,5},{5,6,7}\r\n&gt;&gt;&gt; set1.union(set2,set3)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 3, 4, 5, 6, 7}<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; set1\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 3}<\/div>\n<p>As you can see, it did not alter set1. A method does not always alter a set in Python.<\/p>\n<p><strong>b. intersection() method in Python set<\/strong><\/p>\n<p>This method takes as arguments sets, and returns the common items in all the sets.<\/p>\n<div id=\"attachment_5776\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-intersection.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5776\" class=\"wp-image-5776 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-intersection.jpg\" alt=\"Python intersection\" width=\"400\" height=\"260\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-intersection.jpg 400w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-intersection-150x98.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-intersection-300x195.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><p id=\"caption-attachment-5776\" class=\"wp-caption-text\">Intersection Set in Python<\/p><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; set2.intersection(set1)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{3}<\/div>\n<p>Let\u2019s intersect all three sets.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; set2.intersection(set1,set3)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">set()<\/div>\n<p>It returned an empty set because these three sets have nothing in common.<\/p>\n<p><strong>c. difference() method in Python set<\/strong><\/p>\n<p>The difference() method returns the difference of two or more sets. It returns as a set.<\/p>\n<div id=\"attachment_5775\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-difference-1.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5775\" class=\"wp-image-5775 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-difference-1.jpg\" alt=\"Python Difference\" width=\"400\" height=\"260\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-difference-1.jpg 400w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-difference-1-150x98.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-difference-1-300x195.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><p id=\"caption-attachment-5775\" class=\"wp-caption-text\">Difference Python Set<\/p><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; set1.difference(set2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2}<\/div>\n<p>This returns the items that are in set1, but not in set2.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; set1.difference(set2,set3)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2}<\/div>\n<p><strong>d. symmetric_difference() method in Python set<\/strong><\/p>\n<p>This method returns all the items that are unique to each set.<\/p>\n<div id=\"attachment_5777\" style=\"width: 410px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-symmetric-difference.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-5777\" class=\"wp-image-5777 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-symmetric-difference.jpg\" alt=\"python symmetric difference\" width=\"400\" height=\"260\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-symmetric-difference.jpg 400w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-symmetric-difference-150x98.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-symmetric-difference-300x195.jpg 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><p id=\"caption-attachment-5777\" class=\"wp-caption-text\">Symmetric difference set in Python<\/p><\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; set1.symmetric_difference(set2)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 4, 5}<\/div>\n<p>It returned 1 and 2 because they\u2019re in set1, but not in set2. This also returned 4 and 5 because they\u2019re in set2, but not in set1. It did not return 3 because it exists in both the sets.<\/p>\n<p><strong>e. intersection_update() method in Python set<\/strong><\/p>\n<p>As we discussed in intersection(), it does not update the set on which it is called. For this, we have the intersection_update() method.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; set1.intersection_update(set2)\r\n&gt;&gt;&gt; set1<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{3}<\/div>\n<p>It stored 3 in set1, because only that was common in set1 and set2.<\/p>\n<p><strong>f. difference_update() method in Python set<\/strong><\/p>\n<p>Like intersection-update(), this method updates the Python set with the difference.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; set1={1,2,3}\r\n&gt;&gt;&gt; set2={3,4,5}\r\n&gt;&gt;&gt; set1.difference_update(set2)\r\n&gt;&gt;&gt; set1<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2}<\/div>\n<p><strong>g. symmetric_difference_update() method in Python set<\/strong><\/p>\n<p>Like the two methods we discussed before this, it updates the set on which it is called with the symmetric difference.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; set1={1,2,3}\r\n&gt;&gt;&gt; set2={3,4,5}\r\n&gt;&gt;&gt; set1.symmetric_difference_update(set2)\r\n&gt;&gt;&gt; set1<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{1, 2, 4, 5}<\/div>\n<p><strong>h. copy() method in Python set<\/strong><\/p>\n<p>The copy() method creates a shallow copy of the Python set.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; set4=set1.copy()\r\n&gt;&gt;&gt; set1,set4<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">({1, 2, 4, 5}, {1, 2, 4, 5})<\/div>\n<p><strong>i. isdisjoint() method in Python set<\/strong><\/p>\n<p>This method returns True if two sets have a null intersection.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; {1,3,2}.isdisjoint({4,5,6})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>However, it can take only one argument.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; {1,3,2}.isdisjoint({3,4,5},{6,7,8})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#111&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>{1,3,2}.isdisjoint({3,4,5},{6,7,8})<\/p>\n<p>TypeError: isdisjoint() takes exactly one argument (2 given)<\/p>\n<\/div>\n<p><strong>j. issubset() method in Python set<\/strong><\/p>\n<p>This method returns true if the set in the argument contains this set.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; {1,2}.issubset({1,2,3})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; {1,2}.issubset({1,2})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>{1,2} is a proper subset of {1,2,3} and an improper subset of {1,2}.<\/p>\n<p><strong>k. issuperset() method in Python set<\/strong><\/p>\n<p>Like the issubset() method, this one returns True if the set contains the set in the argument.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; {1,3,4}.issuperset({1,2})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>False<\/p>\n<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; {1,3,4}.issuperset({1})<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h4>7. Python Operations on Sets<\/h4>\n<p>Now, we will look at the operations that we can perform on sets.<\/p>\n<p><strong>a. Membership operation<\/strong><\/p>\n<p>We can apply the \u2018in\u2019 and \u2018not in\u2019 python operators on items for a set. This tells us whether they belong to the set.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 'p' in {'a','p','p','l','e'}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 0 not in {'0','1'}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h4>8. Iterating on a Set in Python<\/h4>\n<p>As we have seen with lists and tuples, we can also iterate over a set in a for loop.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; for i in {1,3,2}:\r\n        print(i)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>12<\/p>\n<p>3<\/p>\n<\/div>\n<p>As you can see, even though we had the order as 1,3,2, it is printed in ascending order.<\/p>\n<h4>9. The frozenset<\/h4>\n<p>A frozen set is an immutable set. You cannot change its values. Also, a set can\u2019t be used as a key for a dictionary, but a frozenset can.<\/p>\n<p><strong>Characteristics of frozenset in Python:<\/strong><\/p>\n<ul>\n<li><strong>Immutable:<\/strong> Once the frozenset is formatted, you cannot add, remove, or change the elements.<\/li>\n<li><strong>Hashable:<\/strong> Frozen sets are different from other sets as they act as a key in dictionaries or an element in another set.<\/li>\n<li><strong>Unordered:<\/strong> They do not have their specific positions.<\/li>\n<li><strong>Unique feature:<\/strong> It has the ability to remove duplicate data entries.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; {{1,2}:3}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#123&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>{{1,2}:3}<\/p>\n<p>TypeError: unhashable type: &#8216;set&#8217;<\/p>\n<\/div>\n<p>Now let\u2019s try doing this with a frozenset.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; {frozenset(1,2):3}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#124&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>{frozenset(1,2):3}<\/p>\n<p>TypeError: frozenset expected at most 1 arguments, got 2<\/p>\n<\/div>\n<p>As you can see, it takes only one argument. Now let\u2019s see the correct syntax.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; {frozenset([1,2]):3}<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">{frozenset({1, 2}): 3}<\/div>\n<h3>What are Python Booleans?<\/h3>\n<p>Finally, let\u2019s discuss Booleans. A Boolean is another data type that Python has to offer.<\/p>\n<h4>1. Value of a Boolean<\/h4>\n<p>As we have seen earlier, a Boolean value may either be True or be False. Some methods like isalpha() or issubset() return a Boolean value.<\/p>\n<h4>2. Declaring a Boolean<\/h4>\n<p>You can declare a Boolean just like you would declare an integer.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; days=True<\/pre>\n<p>As you can see here, we didn\u2019t need to delimit the True value by quotes. If you do that, it is a string, not a Boolean.<\/p>\n<p>Also note that what was once a set, we have reassigned a Boolean to it.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; type('True')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">&lt;class &#8216;str&#8217;&gt;<\/div>\n<h4>3. The bool() function in Python<\/h4>\n<p>Like we have often seen earlier, the bool() function converts another value into the Boolean type.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool('Wisdom')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool([])<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<h4>4. Boolean Values of Various Constructs<\/h4>\n<p>Different values have different equivalent Boolean values. In this example, we use the bool() Python set function to find the values.<\/p>\n<p>For example, 0 has a Boolean value of False.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool(0)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>1 has a Boolean value of True, and so does 0.00000000001.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool(0.000000000001)<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>A string has a Boolean value of True, but an empty string has False.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool(' ')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool('')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>In fact, any empty construct has a Boolean value of False, and a non-empty one has<\/p>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True.<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool(())<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; bool((1,3,2))<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<h4>5. Operations on Booleans<\/h4>\n<h4>a. Arithmetic Operation<\/h4>\n<p>You can apply some arithmetic operations to a set. It takes 0 for False, and 1 for True, and then applies the operator to them.<\/p>\n<p><strong>Addition:<\/strong><\/p>\n<p>You can add two or more Booleans. Let\u2019s see how that works.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; True+False #1+0<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; True+True #1+1<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">2<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; False+True #0+1<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; False+False #0+0<\/pre>\n<p><strong>Subtraction and Multiplication:<\/strong><\/p>\n<p>The same strategy is adopted for subtraction and multiplication.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; False-True<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">-1<\/div>\n<p><strong>Division:<\/strong><\/p>\n<p>Let\u2019s try dividing Booleans.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; False\/True<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">0.0<\/div>\n<p>Remember that division results in a float.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; True\/False<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#148&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>True\/False<\/p>\n<p>ZeroDivisionError: division by zero<\/p>\n<\/div>\n<p>This was an exception that was raised. We will learn more about exceptions in a later lesson.<\/p>\n<p><strong>Modulus, Exponentiation, and Floor Division:<\/strong><\/p>\n<p>The same rules apply for modulus, exponentiation, and floor division as well.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; False%True\r\n&gt;&gt;&gt; True**False<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; False**False<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 0\/\/1<\/pre>\n<p>Try your own combinations like the one below.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; (True+True)*False+True<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">1<\/div>\n<h4>b. Relational Operation<\/h4>\n<p>The relational operators we\u2019ve learnt so far are &gt;, &lt;, &gt;=, &lt;=, !=, and ==. All of these apply to Boolean values.<\/p>\n<p>We will show you a few examples; you should try the rest of them.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; False&gt;True<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; False&lt;=True<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>Again, this takes the value of False to be 0, and that of True to be 1.<\/p>\n<h4>c. Bitwise Operation<\/h4>\n<p>Normally, the bitwise operators operate bit-by bit. For example, the following code ORs the bits of 2(010) and 5(101), and produces the result 7(111).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; 2|5<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">7<\/div>\n<p>But the bitwise operators also apply to Booleans. Let\u2019s see how.<\/p>\n<p><strong>Bitwise &amp; :<\/strong><\/p>\n<p>It returns True only if both values are True.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; True&amp;False<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; True&amp;True<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p>Since Booleans are single-bit, it\u2019s equivalent to applying these operations on 0 and\/or<\/p>\n<p><strong>Bitwise | :<\/strong><\/p>\n<p>It returns False only if both values are False.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; False|True<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<p><strong>Bitwise XOR (^) :<\/strong><\/p>\n<p>This returns True only if one value is True and one is False.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; False^True<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; False^False<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; True^True<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p><strong>Binary 1\u2019s Complement :<\/strong><\/p>\n<p>This calculates 1\u2019s complement for True(1) and False(0).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; ~True<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">-2<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; ~False<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">-1<\/div>\n<p><strong>Left-shift(&lt;&lt;) and Right-shift(&gt;&gt;) Operators :<\/strong><\/p>\n<p>As discussed earlier, these operators shift the value by specified number of bits left and right, respectively.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; False&gt;&gt;2\r\n&gt;&gt;&gt; True&lt;&lt;2<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">4<\/div>\n<p>True is 1. When shifted two places two the left, it results in 100, which is binary for 4. Hence, it returns 4.<\/p>\n<h4>d. Identity Operation<\/h4>\n<p>The identity operators \u2018is\u2019 and \u2018is not\u2019 apply to Booleans.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; False is False<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">True<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; False is 0<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<h4>e. Logical Operation<\/h4>\n<p>Finally, even the logical operators apply on Booleans.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; False and True<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">False<\/div>\n<p>This was all about the article on Python sets and booleans.<\/p>\n<h3>Python Interview Questions on Sets and Booleans<\/h3>\n<p>1. What is Boolean in Python?<\/p>\n<p>2. How do you set a Boolean value in Python?<\/p>\n<p>3. Give an example of a Python set and a Boolean?<\/p>\n<p>4. How do you use Boolean in Python?<\/p>\n<p>5. How do you check if a value is a Boolean in Python?<\/p>\n<h3>Conclusion<\/h3>\n<p>A Python set is an unordered bag of unique items, built for rapid membership tests. Put data inside curly braces and duplicates vanish automatically, leaving each element only once. This feature shines when cleaning lists of user IDs, tags, or sensor codes.<\/p>\n<p>Because sets use hashing like dictionaries, the expressions if x in my_set or my_set.add(x) runs in constant time even for ten million items. Set math\u2014union, intersection, difference\u2014maps directly to operations in logic and search filters. Booleans, on the other hand, are the tiny twins True and False that steer program flow.<\/p>\n<p>See you tomorrow. Hope you liked our article on Python sets and booleans.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So far, we have learned about various data types in Python. We dealt with strings, numbers, lists, tuples, and dictionaries. We\u2019ve also learned that we don\u2019t need to declare the type of data while&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":5772,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[2155,10836,12779,12780],"class_list":["post-5771","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-boolean-in-python","tag-python-sets-and-booleans","tag-sets-and-booleans-in-python","tag-sets-in-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Set and Booleans with Syntax and Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"Python set and Python booleans with python set examples, their features and how to create, access, update set, Bitwise and logical operators in Python booleans\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Set and Booleans with Syntax and Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Python set and Python booleans with python set examples, their features and how to create, access, update set, Bitwise and logical operators in Python booleans\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-01-09T04:34:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-21T09:10:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Sets-and-Booleans.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"14 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Set and Booleans with Syntax and Examples - DataFlair","description":"Python set and Python booleans with python set examples, their features and how to create, access, update set, Bitwise and logical operators in Python booleans","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:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/","og_locale":"en_US","og_type":"article","og_title":"Python Set and Booleans with Syntax and Examples - DataFlair","og_description":"Python set and Python booleans with python set examples, their features and how to create, access, update set, Bitwise and logical operators in Python booleans","og_url":"https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-01-09T04:34:46+00:00","article_modified_time":"2026-04-21T09:10:29+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Sets-and-Booleans.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"14 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"Python Set and Booleans with Syntax and Examples","datePublished":"2018-01-09T04:34:46+00:00","dateModified":"2026-04-21T09:10:29+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/"},"wordCount":2845,"commentCount":11,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Sets-and-Booleans.jpg","keywords":["boolean in python","Python sets and booleans","sets and booleans in python'","sets in python"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/","url":"https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/","name":"Python Set and Booleans with Syntax and Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Sets-and-Booleans.jpg","datePublished":"2018-01-09T04:34:46+00:00","dateModified":"2026-04-21T09:10:29+00:00","description":"Python set and Python booleans with python set examples, their features and how to create, access, update set, Bitwise and logical operators in Python booleans","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Sets-and-Booleans.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/01\/Python-Sets-and-Booleans.jpg","width":802,"height":420,"caption":"Python Sets and Booleans"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-set-and-booleans-with-examples\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"Python Set and Booleans with Syntax and Examples"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=5771"}],"version-history":[{"count":22,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5771\/revisions"}],"predecessor-version":[{"id":147744,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/5771\/revisions\/147744"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/5772"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=5771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=5771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=5771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}