{"id":2644,"date":"2026-10-19T10:23:53","date_gmt":"2026-10-19T10:23:53","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=2644"},"modified":"2026-10-20T05:27:05","modified_gmt":"2026-10-20T05:27:05","slug":"sets-in-python-tutorial","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/","title":{"rendered":"Python Sets"},"content":{"rendered":"<h1><strong><b>Sets in Python Tutorial<\/b><\/strong><\/h1>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Imagine you collect the email addresses of everyone who signs up for an event. Some people sign up twice, and some use the same address at different times. You want each address exactly once. Or imagine two lists of students, one for the maths club and one for the science club, and you want to know who belongs to both. A Python set is made for exactly these jobs.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A set is a collection of unique items. It throws away duplicates for you, it can tell you in an instant whether something is inside it, and it supports the operations you remember from school maths: union, intersection and difference. Sets are small, fast and very handy for cleaning data and comparing groups.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">In this tutorial, we explain what a set is and how to create one, including the empty set trap that catches almost every beginner. We cover how to add and remove items, with the difference between remove and discard. Then we look at the set operations: union, intersection, difference, and the subset and superset checks, with all the directions shown. We end with removing duplicates, frozen sets, common mistakes and the full program. Every output comes from a real run on Python 3.13.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">What are Python Sets?<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A set is an unordered collection of unique items written in curly brackets.<\/p>\n<ul style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px; padding-left: 24px;\">\n<li style=\"margin: 0 0 8px;\"><strong>Unique.<\/strong> A value can appear only once. Adding a duplicate does nothing.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Unordered.<\/strong> Items have no position. You cannot ask for the first one.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Mutable.<\/strong> You can add and remove items. A set can change, but the items inside it must be unchangeable.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Fast to search.<\/strong> Checking whether a value is in a set is much quicker than searching a list.<\/li>\n<\/ul>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Creating a Set<\/h2>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">x = {1, 2, 4, 45}\r\nprint(x)\r\nprint({3, 1, 3, 2, 1})\r\nprint(set([5, 5, 6, 7]))\r\nprint(sorted(set(\"hello\")))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{1, 2, 4, 45}\r\n{1, 2, 3}\r\n{5, 6, 7}\r\n['e', 'h', 'l', 'o']<\/pre>\n<\/div>\n<ul style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px; padding-left: 24px;\">\n<li style=\"margin: 0 0 8px;\">The second line had five numbers, but the set holds only three, because the repeats were dropped.<\/li>\n<li style=\"margin: 0 0 8px;\"><code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">set()<\/code> turns another collection, such as a list, into a set.<\/li>\n<li style=\"margin: 0 0 8px;\">A string passed to <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">set<\/code> splits into single characters, and the repeated l in hello appears once. We used <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">sorted<\/code> here because the order of text items in a set can change from run to run.<\/li>\n<\/ul>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">The Empty Set Trap<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Empty curly brackets make a dictionary, not a set. To get an empty set, use <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">set()<\/code>.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">empty_wrong = {}\r\nempty_right = set()\r\nprint(type(empty_wrong), type(empty_right))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">&lt;class 'dict'&gt; &lt;class 'set'&gt;<\/pre>\n<\/div>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">No Order, No Indexing<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Since a set has no order, you cannot get an item by position. Small whole numbers often print in sorted order, but never rely on that.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">x = {1, 2, 4, 45}\r\ntry:\r\n    x[0]\r\nexcept TypeError as error:\r\n    print(\"TypeError:\", error)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">TypeError: 'set' object is not subscriptable<\/pre>\n<\/div>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">Looping and Checking Membership<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">for<\/code> loop visits every item. The <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">in<\/code> word checks whether a value is present.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">x = {1, 2, 3, 4, 5, 6}\r\nfor z in x:\r\n    print(z, end=\" \")\r\nprint()\r\nprint(1 in x, 9 in x, 9 not in x)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">1 2 3 4 5 6 \r\nTrue False True<\/pre>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Methods for Sets in Python<\/h2>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 700px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Set methods at a glance<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 260px;\" scope=\"col\">Method<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">What it does<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Returns<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">add(item)<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Adds one item<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">None<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">update(iterable)<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Adds many items at once<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">None<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">remove(item)<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Removes an item, error if missing<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">None<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">discard(item)<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Removes an item, no error if missing<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">None<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">pop()<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Removes and returns an arbitrary item<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">The item<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">clear()<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Removes every item<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">None<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">copy()<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Makes a copy<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">A new set<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">union(other)<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">All items from both<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">A new set<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">intersection(other)<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Items found in both<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">A new set<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">difference(other)<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Items in the first but not the second<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">A new set<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">symmetric_difference(other)<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Items in one but not both<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">A new set<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">issubset(other)<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Is every item also in the other set?<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">True or False<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">issuperset(other)<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Does it contain every item of the other?<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">True or False<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\" scope=\"row\">isdisjoint(other)<\/th>\n<td style=\"padding: 12px 16px;\">Do the two share nothing?<\/td>\n<td style=\"padding: 12px 16px;\">True or False<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">add()<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\"><code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">add<\/code> puts in one item. Adding something that is already there changes nothing.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">x = {1, 2, 3, 4, 5, 6}\r\nx.add(9)\r\nprint(x)\r\nx.add(9)\r\nprint(len(x))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{1, 2, 3, 4, 5, 6, 9}\r\n7<\/pre>\n<\/div>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">update()<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\"><code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">update<\/code> adds many items from an iterable, such as a list, a tuple, or another set. Put the numbers in a list. Separate numbers, as in <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">x.update(8, 11)<\/code>, fail.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">x = {1, 2, 3, 4, 5, 6, 9}\r\nx.update([8, 11, 44, 22])\r\nprint(x)\r\nx.update([100], {200, 300})\r\nprint(sorted(x))\r\ntry:\r\n    x.update(8, 11)\r\nexcept TypeError as error:\r\n    print(\"TypeError:\", error)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{1, 2, 3, 4, 5, 6, 8, 9, 11, 22, 44}\r\n[1, 2, 3, 4, 5, 6, 8, 9, 11, 22, 44, 100, 200, 300]\r\nTypeError: 'int' object is not iterable<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">You can pass more than one iterable, as the second line shows.<\/p>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">len()<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\"><code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">len<\/code> counts the items. It is a built in function, not a set method.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">print(len({1, 2, 3}))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">3<\/pre>\n<\/div>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">remove() and discard()<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Both take out an item. The difference is what happens when the item is missing. <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">remove<\/code> raises a KeyError, and <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">discard<\/code> stays silent.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">x = {1, 2, 3, 4, 5, 6}\r\nx.remove(2)\r\nprint(x)\r\nx.discard(99)\r\ntry:\r\n    x.remove(99)\r\nexcept KeyError as error:\r\n    print(\"KeyError:\", error)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{1, 3, 4, 5, 6}\r\nKeyError: 99<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">discard<\/code> call on 99 did nothing and raised no error. The <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">remove<\/code> call on 99 raised the KeyError.<\/p>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">pop()<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\"><code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">pop<\/code> removes one item and hands it back. A set has no order, so it does not remove from the end. It removes an arbitrary item, and you cannot choose which one.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">x = {1, 3, 4, 5, 6}\r\npopped = x.pop()\r\nprint(popped in {1, 3, 4, 5, 6}, len(x))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">True 4<\/pre>\n<\/div>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">clear()<\/h3>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">x = {1, 3, 4, 5}\r\nx.clear()\r\nprint(x, len(x))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">set() 0<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">An empty set prints as <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">set()<\/code>, not as <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">{}<\/code>, to avoid mixing it up with an empty dictionary.<\/p>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">del<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">There is no <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">x.delete()<\/code> method. <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">del<\/code> is a statement, not a method. It removes the name itself, so using the set afterwards fails.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">gone = {1, 2}\r\ndel gone\r\ntry:\r\n    print(gone)\r\nexcept NameError as error:\r\n    print(\"NameError:\", error)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">NameError: name 'gone' is not defined<\/pre>\n<\/div>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">copy()<\/h3>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">a = {1, 2, 3}\r\nb = a.copy()\r\nb.add(4)\r\nprint(a, b)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{1, 2, 3} {1, 2, 3, 4}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The copy is independent, so adding to <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">b<\/code> did not touch <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">a<\/code>.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Set Operations<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Take two sets, <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">x<\/code> and <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">y<\/code>. Each operation has a method and a shortcut operator.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">x = {1, 2, 3, 4, 5, 6}\r\ny = {4, 5, 6, 7, 8}\r\nprint(x.union(y), x | y)\r\nprint(x.intersection(y), x &amp; y)\r\nprint(x.difference(y), x - y)\r\nprint(x.symmetric_difference(y), x ^ y)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{1, 2, 3, 4, 5, 6, 7, 8} {1, 2, 3, 4, 5, 6, 7, 8}\r\n{4, 5, 6} {4, 5, 6}\r\n{1, 2, 3} {1, 2, 3}\r\n{1, 2, 3, 7, 8} {1, 2, 3, 7, 8}<\/pre>\n<\/div>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 600px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Set operators<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 220px;\" scope=\"col\">Operation<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Operator<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">In plain words<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">union<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">|<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Everything in either set<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">intersection<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">&amp;<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Only what both sets have<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">difference<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">&#8211;<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">In the first set but not in the second<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">symmetric difference<\/th>\n<td style=\"padding: 12px 16px; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">^<\/td>\n<td style=\"padding: 12px 16px;\">In one set but not in both<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">Think of the maths club and the science club. The union is everyone in either club. The intersection is the students who joined both. The difference is the students in maths who are not in science.<\/p>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">issubset() and issuperset()<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A set is a subset of another if every one of its items is also in the other. The other set is then a superset of it. The direction matters. Here <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">x<\/code> has 6 items and <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">small<\/code> has 3 of them:<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">x = {1, 2, 3, 4, 5, 6}\r\nsmall = {4, 5, 6}\r\nprint(x.issubset(small))\r\nprint(small.issubset(x))\r\nprint(x.issuperset(small))\r\nprint(small.issuperset(x))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">False\r\nTrue\r\nTrue\r\nFalse<\/pre>\n<\/div>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 640px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Subset and superset checks<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 260px;\" scope=\"col\">Question<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Answer<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Why<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Is x a subset of small?<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">False<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">x has items that small lacks<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Is small a subset of x?<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">True<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Every item of small is in x<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Is x a superset of small?<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">True<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">x contains all of small<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Is small a superset of x?<\/th>\n<td style=\"padding: 12px 16px;\">False<\/td>\n<td style=\"padding: 12px 16px;\">small is missing items of x<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 21px; line-height: 1.35; font-weight: bold; color: #0f2a4a; margin: 28px 0 10px;\">isdisjoint()<\/h3>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\"><code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">isdisjoint<\/code> is True when two sets share no items.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">print({1, 2}.isdisjoint({3, 4}), {1, 2}.isdisjoint({2, 3}))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">True False<\/pre>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Items in a Set Must Be Unchangeable<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A set can only hold hashable items, which in practice means items that cannot change. Numbers, text and tuples work. A list does not.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">try:\r\n    bad = {[1, 2], 3}\r\nexcept TypeError as error:\r\n    print(\"TypeError:\", error)\r\nprint({(1, 2), 3} == {3, (1, 2)})<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">TypeError: unhashable type: 'list'\r\nTrue<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The last line shows a tuple inside a set, and that two sets with the same items are equal in any order.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Removing Duplicates With a Set<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">This is the most common real use of a set. Turn a list into a set and back.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">names = [\"Asha\", \"Ravi\", \"Asha\", \"Meena\", \"Ravi\"]\r\nprint(sorted(set(names)))\r\nprint(list(dict.fromkeys(names)))<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">['Asha', 'Meena', 'Ravi']\r\n['Asha', 'Ravi', 'Meena']<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The first line removes the duplicates but loses the original order, so the output is sorted here. If order matters, use the second form. A dictionary remembers the order of first appearance, and it gave Asha, Ravi, Meena, the order they came in.<\/p>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Set Comprehension and Frozen Sets<\/h2>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">print({n % 3 for n in range(10)})<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">{0, 1, 2}<\/pre>\n<\/div>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A set comprehension builds a set in one line. Ten numbers gave only three remainders, because the repeats collapsed.<\/p>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">A frozenset is a set that cannot be changed. Because it is unchangeable, it can be used as a dictionary key or placed inside another set.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">fs = frozenset([1, 2, 3])\r\ntry:\r\n    fs.add(4)\r\nexcept AttributeError as error:\r\n    print(\"AttributeError:\", error)<\/pre>\n<div style=\"background: #1f2937; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">output<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #ffffff;\">AttributeError: 'frozenset' object has no attribute 'add'<\/pre>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Set Compared With List, Tuple and Dictionary<\/h2>\n<div style=\"border: 1px solid #d9e0ea; border-radius: 8px; overflow-x: auto; margin: 20px 0; -webkit-overflow-scrolling: touch;\">\n<table style=\"width: 100%; min-width: 780px; border-collapse: collapse; font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 1.6; color: #1f2937;\">\n<caption style=\"caption-side: top; text-align: left; padding: 14px 16px; font-weight: bold; font-size: 17px; color: #0f2a4a;\">Four collection types compared<\/caption>\n<thead>\n<tr>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38; width: 140px;\" scope=\"col\">Type<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Written as<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Ordered<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Duplicates<\/th>\n<th style=\"background: #0f2a4a; color: #ffffff; text-align: left; padding: 12px 16px; border-bottom: 2px solid #0b1f38;\" scope=\"col\">Best for<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">List<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">[1, 2, 3]<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Allowed<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">A collection that grows or changes<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Tuple<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">(1, 2, 3)<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Yes<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Allowed<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">A fixed group of values<\/td>\n<\/tr>\n<tr style=\"background: #ffffff;\">\n<th style=\"text-align: left; padding: 12px 16px; border-bottom: 1px solid #e5e9f0; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Set<\/th>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">{1, 2, 3}<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">No<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Not allowed<\/td>\n<td style=\"padding: 12px 16px; border-bottom: 1px solid #e5e9f0;\">Unique items and group comparisons<\/td>\n<\/tr>\n<tr style=\"background: #f5f8fc;\">\n<th style=\"text-align: left; padding: 12px 16px; color: #0f2a4a; white-space: nowrap;\" scope=\"row\">Dictionary<\/th>\n<td style=\"padding: 12px 16px; font-family: 'Courier New',Courier,monospace; font-size: 15px; white-space: nowrap;\">{&#8220;a&#8221;: 1}<\/td>\n<td style=\"padding: 12px 16px;\">Yes<\/td>\n<td style=\"padding: 12px 16px;\">Keys must be unique<\/td>\n<td style=\"padding: 12px 16px;\">Looking up values by key<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Common Mistakes to Avoid<\/h2>\n<ul style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px; padding-left: 24px;\">\n<li style=\"margin: 0 0 8px;\"><strong>Using empty curly brackets for an empty set.<\/strong> That makes a dictionary. Use <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">set()<\/code>.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Expecting order.<\/strong> A set has no order and no index.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Passing separate numbers to update.<\/strong> Pass one iterable, such as a list.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Using remove for an item that may be missing.<\/strong> It raises a KeyError. Use <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">discard<\/code>.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Getting the direction of subset and superset wrong.<\/strong> <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">A.issubset(B)<\/code> asks whether A fits inside B.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Putting a list inside a set.<\/strong> Lists cannot be hashed. Use a tuple.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Using a set to remove duplicates when order matters.<\/strong> Use <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">dict.fromkeys<\/code> instead.<\/li>\n<li style=\"margin: 0 0 8px;\"><strong>Writing set.delete().<\/strong> It does not exist. Use <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">del<\/code>, or <code style=\"font-family: 'Courier New',Courier,monospace; font-size: 15px; background: #eef2f7; padding: 2px 6px; border-radius: 4px;\">clear<\/code> to empty it.<\/li>\n<\/ul>\n<h2 style=\"font-family: Arial,Helvetica,sans-serif; font-size: 26px; line-height: 1.3; font-weight: bold; color: #0f2a4a; margin: 36px 0 12px;\">Try It Yourself: The Full Program<\/h2>\n<p style=\"font-family: Arial,Helvetica,sans-serif; font-size: 17px; line-height: 1.7; color: #1f2937; margin: 0 0 16px;\">The program below holds every example in this tutorial. Copy it into a file, run it, and compare your output with what you see above.<\/p>\n<div style=\"margin: 20px 0; border: 1px solid #d9e0ea; border-radius: 8px; overflow: hidden;\">\n<div style=\"background: #0f2a4a; color: #ffffff; font-family: Arial,Helvetica,sans-serif; font-size: 14px; font-weight: bold; padding: 8px 16px;\">python<\/div>\n<pre style=\"margin: 0; padding: 16px; color: #1f2937; font-family: 'Courier New',Courier,monospace; font-size: 14px; line-height: 1.7; overflow-x: auto; white-space: pre; background: #f5f8fc;\">import sys\r\nprint(\"Python\", sys.version_info.major, sys.version_info.minor)\r\n\r\n# Creating sets\r\nx = {1, 2, 4, 45}\r\nprint(x)\r\nprint({3, 1, 3, 2, 1})\r\nprint(set([5, 5, 6, 7]))\r\nprint(sorted(set(\"hello\")))\r\nempty_wrong = {}\r\nempty_right = set()\r\nprint(type(empty_wrong), type(empty_right))\r\n\r\n# No order and no indexing\r\ntry:\r\n    x[0]\r\nexcept TypeError as error:\r\n    print(\"TypeError:\", error)\r\n\r\n# Looping and membership\r\nx = {1, 2, 3, 4, 5, 6}\r\nfor z in x:\r\n    print(z, end=\" \")\r\nprint()\r\nprint(1 in x, 9 in x, 9 not in x)\r\n\r\n# Adding\r\nx.add(9)\r\nprint(x)\r\nx.add(9)\r\nprint(len(x))\r\nx.update([8, 11, 44, 22])\r\nprint(x)\r\nx.update([100], {200, 300})\r\nprint(sorted(x))\r\ntry:\r\n    x.update(8, 11)\r\nexcept TypeError as error:\r\n    print(\"TypeError:\", error)\r\n\r\n# Removing\r\nx = {1, 2, 3, 4, 5, 6}\r\nx.remove(2)\r\nprint(x)\r\nx.discard(99)\r\ntry:\r\n    x.remove(99)\r\nexcept KeyError as error:\r\n    print(\"KeyError:\", error)\r\npopped = x.pop()\r\nprint(popped in {1, 3, 4, 5, 6}, len(x))\r\nx.clear()\r\nprint(x, len(x))\r\ngone = {1, 2}\r\ndel gone\r\ntry:\r\n    print(gone)\r\nexcept NameError as error:\r\n    print(\"NameError:\", error)\r\n\r\n# Copy\r\na = {1, 2, 3}\r\nb = a.copy()\r\nb.add(4)\r\nprint(a, b)\r\n\r\n# Set operations\r\nx = {1, 2, 3, 4, 5, 6}\r\ny = {4, 5, 6, 7, 8}\r\nprint(x.union(y), x | y)\r\nprint(x.intersection(y), x &amp; y)\r\nprint(x.difference(y), x - y)\r\nprint(x.symmetric_difference(y), x ^ y)\r\n\r\n# Subset and superset\r\nsmall = {4, 5, 6}\r\nprint(x.issubset(small))\r\nprint(small.issubset(x))\r\nprint(x.issuperset(small))\r\nprint(small.issuperset(x))\r\nprint({1, 2}.isdisjoint({3, 4}), {1, 2}.isdisjoint({2, 3}))\r\n\r\n# Items must be unchangeable\r\ntry:\r\n    bad = {[1, 2], 3}\r\nexcept TypeError as error:\r\n    print(\"TypeError:\", error)\r\nprint({(1, 2), 3} == {3, (1, 2)})\r\n\r\n# Removing duplicates, and what it costs\r\nnames = [\"Asha\", \"Ravi\", \"Asha\", \"Meena\", \"Ravi\"]\r\nprint(sorted(set(names)))\r\nprint(list(dict.fromkeys(names)))\r\n\r\n# Set comprehension and frozenset\r\nprint({n % 3 for n in range(10)})\r\nfs = frozenset([1, 2, 3])\r\ntry:\r\n    fs.add(4)\r\nexcept AttributeError as error:\r\n    print(\"AttributeError:\", error)<\/pre>\n<\/div>\n<p>&nbsp;<\/p>\n<p>Sets do one job very well: they keep things unique and make group comparisons easy. Run the examples, change the numbers, and watch how union, intersection and difference behave. When they feel natural, you will reach for a set whenever you hear the words unique, duplicate or overlap. Then move on to the other Python data types to see how all five fit together.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sets in Python Tutorial Imagine you collect the email addresses of everyone who signs up for an event. Some people sign up twice, and some use the same address at different times. You want each address exactly once. Or imagine two lists of students, one for the maths club and one for the science club, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,1671],"tags":[358],"class_list":["post-2644","post","type-post","status-publish","format-standard","hentry","category-python","category-python-data-types","tag-sets-in-python-tutorial"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Python Sets Tutorial for Beginners With Examples | Prwatech<\/title>\n<meta name=\"description\" content=\"Learn Python sets step by step: create them, add and remove items, use union, intersection and difference, and remove duplicates, with tested outputs.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Sets Tutorial for Beginners With Examples | Prwatech\" \/>\n<meta property=\"og:description\" content=\"Learn Python sets step by step: create them, add and remove items, use union, intersection and difference, and remove duplicates, with tested outputs.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/\" \/>\n<meta property=\"og:site_name\" content=\"Prwatech\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/prwatech.in\/\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-19T10:23:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-10-20T05:27:05+00:00\" \/>\n<meta name=\"author\" content=\"Prwatech\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:site\" content=\"@Eduprwatech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prwatech\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/\",\"url\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/\",\"name\":\"Python Sets Tutorial for Beginners With Examples | Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2026-10-19T10:23:53+00:00\",\"dateModified\":\"2026-10-20T05:27:05+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Learn Python sets step by step: create them, add and remove items, use union, intersection and difference, and remove duplicates, with tested outputs.\",\"breadcrumb\":{\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/prwatech.in\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Sets\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/prwatech.in\/blog\/#website\",\"url\":\"https:\/\/prwatech.in\/blog\/\",\"name\":\"Prwatech\",\"description\":\"Share Ideas, Start Something Good.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/prwatech.in\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\",\"name\":\"Prwatech\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g\",\"caption\":\"Prwatech\"},\"url\":\"https:\/\/prwatech.in\/blog\/author\/prwatech123\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Sets Tutorial for Beginners With Examples | Prwatech","description":"Learn Python sets step by step: create them, add and remove items, use union, intersection and difference, and remove duplicates, with tested outputs.","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:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/","og_locale":"en_US","og_type":"article","og_title":"Python Sets Tutorial for Beginners With Examples | Prwatech","og_description":"Learn Python sets step by step: create them, add and remove items, use union, intersection and difference, and remove duplicates, with tested outputs.","og_url":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/","og_site_name":"Prwatech","article_publisher":"https:\/\/www.facebook.com\/prwatech.in\/","article_published_time":"2026-10-19T10:23:53+00:00","article_modified_time":"2026-10-20T05:27:05+00:00","author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/","url":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/","name":"Python Sets Tutorial for Beginners With Examples | Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2026-10-19T10:23:53+00:00","dateModified":"2026-10-20T05:27:05+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Learn Python sets step by step: create them, add and remove items, use union, intersection and difference, and remove duplicates, with tested outputs.","breadcrumb":{"@id":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/prwatech.in\/blog\/"},{"@type":"ListItem","position":2,"name":"Python Sets"}]},{"@type":"WebSite","@id":"https:\/\/prwatech.in\/blog\/#website","url":"https:\/\/prwatech.in\/blog\/","name":"Prwatech","description":"Share Ideas, Start Something Good.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/prwatech.in\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3","name":"Prwatech","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c00bafc1b04045f31eda917de39891456c44fa47c092b9bb6be0f860a3a30a2f?s=96&d=mm&r=g","caption":"Prwatech"},"url":"https:\/\/prwatech.in\/blog\/author\/prwatech123\/"}]}},"_links":{"self":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/2644","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/comments?post=2644"}],"version-history":[{"count":8,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/2644\/revisions"}],"predecessor-version":[{"id":12038,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/2644\/revisions\/12038"}],"wp:attachment":[{"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/media?parent=2644"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/categories?post=2644"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/tags?post=2644"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}