{"id":2644,"date":"2019-07-17T10:23:53","date_gmt":"2019-07-17T10:23:53","guid":{"rendered":"https:\/\/prwatech.in\/blog\/?p=2644"},"modified":"2023-07-14T09:12:35","modified_gmt":"2023-07-14T09:12:35","slug":"sets-in-python-tutorial","status":"publish","type":"post","link":"https:\/\/prwatech.in\/blog\/python\/python-data-types\/sets-in-python-tutorial\/","title":{"rendered":"Sets in Python"},"content":{"rendered":"<h1><\/h1>\n<h1 style=\"text-align: center;\">Sets in Python Tutorial<\/h1>\n<p>&nbsp;<\/p>\n<p><strong>Sets in Python Tutorial<\/strong>, In this tutorial, we will learn what are python sets different methods for sets in Python to your code. Are you looking for the Python sets tutorial with examples or Are you dreaming to become to certified Pro <a href=\"https:\/\/prwatech.in\/python-training-institute-in-bangalore\/\">Python Developer<\/a>, then stop just dreaming, get your <a href=\"https:\/\/prwatech.in\/python-training-institute-in-bangalore\/\">Python certification course<\/a> from India\u2019s Leading <a href=\"https:\/\/prwatech.in\/python-training-institute-in-bangalore\/\">Python training institute<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>A Python set is a built-in data type that stores a group of elements without ordering elements. In a given set duplicate elements are not allowed. we need to include all the elements in the set in the form of an iterable object such as a string when we are defining a set. Do you want to know about sets in python with examples, then just follow the below mentioned Python sets tutorial for Beginners from <a href=\"https:\/\/prwatech.com\/\">Prwatech<\/a> and take advanced <a href=\"https:\/\/prwatech.in\/python-training-institute-in-bangalore\/\">Python training<\/a> like a Pro from today itself under 10+ years of hands-on experienced Professionals.<\/p>\n<p>&nbsp;<\/p>\n<h2>What are Python Sets?<\/h2>\n<p>&nbsp;<\/p>\n<p>A set is a collection of unordered and unindexed elements enclosed with curly brackets.<\/p>\n<p>Ex) x={1,2,4,45}<\/p>\n<p>You cannot access an element in a set by referring to its index since sets are unordered the elements don\u2019t have an index. You can traverse through set using a for a loop.<\/p>\n<p>Ex) x={1,2,3,4,5,6}<\/p>\n<p>for z in x:<\/p>\n<p>print(z)<\/p>\n<p>You can check whether an element is present in a set using keyword \u201cin\u201d<\/p>\n<p>Ex) x={1,2,3,4,5,6}<\/p>\n<p>if(1 in x):<\/p>\n<p>print(z)<\/p>\n<p>Once a set is created, you cannot change its elements, but you can add new items.<\/p>\n<h2>Methods for Sets in Python<\/h2>\n<p>&nbsp;<\/p>\n<h3><strong>add(): <\/strong>Used to add a single element to a set.<\/h3>\n<p>&nbsp;<\/p>\n<p>Ex) x={1,2,3,4,5,6}<\/p>\n<p>x.add(9)<\/p>\n<p>print(x)<\/p>\n<h3><strong>update(): <\/strong>Used to add multiple elements to a set.<\/h3>\n<p>&nbsp;<\/p>\n<p>Ex) x={1,2,3,4,5,6}<\/p>\n<p>x.update(8,11,44,22)<\/p>\n<p>print(x)<\/p>\n<h3><strong>len():<\/strong> Used to determine the total number of elements present in set.<\/h3>\n<p>&nbsp;<\/p>\n<p>Ex) x={1,2,3,4,5,6}<\/p>\n<p>print(len(x))<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>remove() or discard(): <\/strong>Used to remove a specific element from a set.<\/h3>\n<p>&nbsp;<\/p>\n<p>Ex) x={1,2,3,4,5,6}<\/p>\n<p>x.remove(2)<\/p>\n<p>print(x)<\/p>\n<h3><strong>pop(): <\/strong>It removes an element from the end of a set.<\/h3>\n<p>&nbsp;<\/p>\n<p>Ex) x={1,2,3,4,5,6}<\/p>\n<p>print(x.pop())<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>clear():<\/strong> It is used to clear all the elements from the entire set.<\/h3>\n<p>&nbsp;<\/p>\n<p>Ex) x={1,2,3,4,5,6}<\/p>\n<p>x.clear()<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>del(): <\/strong>It will delete the set completely.<\/h3>\n<p>&nbsp;<\/p>\n<p>Ex) x={1,2,3,4,5,6}<\/p>\n<p>x.delete()<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>copy():<\/strong> It returns a copy of a set.<\/h3>\n<p>&nbsp;<\/p>\n<p>Ex) x={1,2,3,4,5,6}<\/p>\n<p>z= x.copy()<\/p>\n<p>print(z)<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>union(): <\/strong>It combines two sets without repeating the common elements<\/h3>\n<p>&nbsp;<\/p>\n<p>Ex) x={1,2,3,4,5,6}<\/p>\n<p>y={4,5,6,8,9,123}<\/p>\n<p>z= x.union(y)<\/p>\n<p>print(z)<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>intersection(): <\/strong>It returns only the common elements between the two sets.<\/h3>\n<p>&nbsp;<\/p>\n<p>Ex) x={1,2,3,4,5,6}<\/p>\n<p>y={4,5,6,8,9,123}<\/p>\n<p>z= x.intersection(y)<\/p>\n<p>print(z)<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>issubset():<\/strong> Used to identify whether the given set is part of another set or not.<\/h3>\n<p>&nbsp;<\/p>\n<p>Ex) x={1,2,3,4,5,6}<\/p>\n<p>y={4,5,6 }<\/p>\n<p>z= x.issubset(y)<\/p>\n<p>print(z)<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>issuperset():<\/strong> Used to identify whether the given set is a superset of another set or not.<\/h3>\n<p>&nbsp;<\/p>\n<p>Ex) x={1,2,3,4,5,6}<\/p>\n<p>y={4,5,6 }<\/p>\n<p>z= y.issubset(x)<\/p>\n<p>print(z)<\/p>\n<p>&nbsp;<\/p>\n<p>We hope you understand sets in Python tutorial with examples and methods for sets in the <a href=\"https:\/\/prwatech.in\/python-training-institute-in-bangalore\/\">Python programming<\/a> concept. Get success in your career as a <a href=\"https:\/\/prwatech.in\/python-training-institute-in-bangalore\/\">Python programmer<\/a> by being a part of the <a href=\"https:\/\/prwatech.com\/\">Prwatech<\/a>, India&#8217;s leading <a href=\"https:\/\/prwatech.in\/python-training-institute-in-bangalore\/\">Python training institute in Bangalore<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sets in Python Tutorial &nbsp; Sets in Python Tutorial, In this tutorial, we will learn what are python sets different methods for sets in Python to your code. Are you looking for the Python sets tutorial with examples or Are you dreaming to become to certified Pro Python Developer, then stop just dreaming, get your [&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>Sets in Python Tutorial with Examples | Methods for sets - Prwatech<\/title>\n<meta name=\"description\" content=\"Explore sets in python tutorial with examples concepts and learn what are python sets, methods for sets in python from Prwatech.\" \/>\n<meta name=\"robots\" content=\"noindex, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sets in Python Tutorial with Examples | Methods for sets - Prwatech\" \/>\n<meta property=\"og:description\" content=\"Explore sets in python tutorial with examples concepts and learn what are python sets, methods for sets in python from Prwatech.\" \/>\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=\"2019-07-17T10:23:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-07-14T09:12:35+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=\"3 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\":\"Sets in Python Tutorial with Examples | Methods for sets - Prwatech\",\"isPartOf\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#website\"},\"datePublished\":\"2019-07-17T10:23:53+00:00\",\"dateModified\":\"2023-07-14T09:12:35+00:00\",\"author\":{\"@id\":\"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3\"},\"description\":\"Explore sets in python tutorial with examples concepts and learn what are python sets, methods for sets in python from Prwatech.\",\"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\":\"Sets in Python\"}]},{\"@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":"Sets in Python Tutorial with Examples | Methods for sets - Prwatech","description":"Explore sets in python tutorial with examples concepts and learn what are python sets, methods for sets in python from Prwatech.","robots":{"index":"noindex","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Sets in Python Tutorial with Examples | Methods for sets - Prwatech","og_description":"Explore sets in python tutorial with examples concepts and learn what are python sets, methods for sets in python from Prwatech.","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":"2019-07-17T10:23:53+00:00","article_modified_time":"2023-07-14T09:12:35+00:00","author":"Prwatech","twitter_card":"summary_large_image","twitter_creator":"@Eduprwatech","twitter_site":"@Eduprwatech","twitter_misc":{"Written by":"Prwatech","Est. reading time":"3 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":"Sets in Python Tutorial with Examples | Methods for sets - Prwatech","isPartOf":{"@id":"https:\/\/prwatech.in\/blog\/#website"},"datePublished":"2019-07-17T10:23:53+00:00","dateModified":"2023-07-14T09:12:35+00:00","author":{"@id":"https:\/\/prwatech.in\/blog\/#\/schema\/person\/db90baff7744090b2288bbc98fea87f3"},"description":"Explore sets in python tutorial with examples concepts and learn what are python sets, methods for sets in python from Prwatech.","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":"Sets in Python"}]},{"@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":6,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/2644\/revisions"}],"predecessor-version":[{"id":4506,"href":"https:\/\/prwatech.in\/blog\/wp-json\/wp\/v2\/posts\/2644\/revisions\/4506"}],"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}]}}