{"id":3398,"date":"2023-10-21T16:15:41","date_gmt":"2023-10-21T10:45:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=3398"},"modified":"2023-11-10T11:52:05","modified_gmt":"2023-11-10T06:22:05","slug":"python-programming-set","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-programming-set\/","title":{"rendered":"Python Programming \u2013 Set"},"content":{"rendered":"

In this Page, We are Providing Python Programming \u2013 Set. Students can visit for more Detail and Explanation of Python Handwritten Notes<\/a>\u00a0Pdf.<\/p>\n

Python Programming \u2013 Set<\/h2>\n

Set<\/strong><\/p>\n

A set is an unordered collection with no duplicate elements. Basic uses include membership testing, eliminating duplicate entries from sequence, mathematical operations like union, intersection, difference, symmetric difference etc. As mentioned in chapter 2, sets are of two types: set (mutable set) and frozenset (immutable set).<\/p>\n

>>> a=set ( [ \u2019 spam \u2019 , ' eggs ' , 100 , 1234 ] ) \r\n>>> a\r\nset ( [ ' eggs ' , 100 , 1234 , ' spam ' ] ) \r\n>>> a=set ( ' abracadabra ' )\r\n>>> a\r\nset ( [ ' a ' , ' r ' , ' b ' , ' c ' , ' d ' ] )\r\n>>> a-frozenset ( [ ' spain ' , ' eggs ' , 100 , 1234 ] ) \r\n>>> a\r\nfrozenset ( [ ' eggs ' , 100 , 1234 , ' spam ' ] )<\/pre>\n

Being an unordered collection, sets do not record element position or order of insertion. Accordingly, sets do not support indexing, slicing, or other sequence-like behavior.<\/p>\n

Set creation<\/strong><\/p>\n

A set can be created in many ways.<\/p>\n

Using curly braces<\/strong><\/p>\n

Non-empty set (not frozenset) can be created by placing a comma-separated list of elements within braces. Curly braces cannot be used to create an empty set, because it will create an empty dictionary that will be discussed in the next section.<\/p>\n

>>> { ' spam ' , ' eggs ' , 100 , 1234 } \r\nset ( [ 1234 , 100 , ' eggs ' , ' spam ' ] )<\/pre>\n

Set comprehension<\/strong><\/p>\n

Python also supports set comprehension:<\/p>\n

>>> a= { x for x in ' abracadabra ' if x not in ' abc ' }\r\n>>> a\r\nset ( [ ' r ' , ' d ' ] )<\/pre>\n

Using built-in function<\/strong><\/p>\n

The built-in functions set ( ) and frozenset ( ) are used to create set and frozenset, respectively, whose elements are taken from iterable. If iterable is not specified, a new empty set is returned.<\/p>\n

>>> a=set ( ( ' spam ' , ' eggs ' , 100 , 1234 ) )\r\n>>> a\r\nset ( [ ' eggs ' , 100 , 1234 , ' spam ' ] )\r\n>>> set ( )\r\nset ( [ ] ) \r\n>>> a=frozenset ( ( ' spam ' , ' eggs ' , 100 , 1234 ) )\r\n>>> a\r\nfrozenset ( [ ' eggs ' , 100 , 1234 , ' spam ' ] )\r\n>>> frozenset ( ) \r\nfrozenset ( [ ] ) \r\n>>> set ( ' abc ' )==frozenset ( ' abc ' )\r\nTrue<\/pre>\n

Deleting set<\/strong><\/p>\n

To explicitly remove an entire set, just use the del statement.<\/p>\n

>>> ss=set ( ' abracadabra ' )\r\n>>> ss\r\nset ( [ ' a ' , ' r ' , ' b ' , ' c ' , ' d ' ] )\r\n>>> del SS \r\n>>> SS\r\nTraceback ( most recent call last ) :\r\nFile \"<stdin>\", line 1 , in <module>\r\nNameError: name ' ss ' is not defined<\/pre>\n

Looping techniques<\/strong><\/p>\n

It is also possible to iterate over each element of a set. However, since set is unordered, it is not known which order the iteration will follow.<\/p>\n

>>> ss=set ( ' abracadabra ' )\r\n>>> ss\r\nset ( [ ' a ' , ' r ' , ' b ' , ' c ' , ' d ' ] ) \r\n>>> for item in ss:\r\n. . . print item\r\na\r\nr\r\nb\r\nc\r\nd<\/pre>\n

Membership operation<\/strong><\/p>\n

Set also support membership operation.<\/p>\n

>>> a=set ( ' abracadabra ' )\r\n>>> a\r\nset ( [ ' a ' , \" r ' r ' b ' , ' c ' , ' d ' ] )\r\n>>> ' r ' in a \r\nTrue \r\n>>> ' rrr ' not in a \r\nTrue \r\n>>> a=frozenset ( ' abracadabra ' )\r\n>>> a\r\nfrozenset ( [ ' a ' , ' r ' , ' b ' , ' c ' , ' d ' ] ) \r\n>>> ' r ' in a\r\nTrue\r\n>>> ' rrr ' not in a \r\nTrue<\/pre>\n","protected":false},"excerpt":{"rendered":"

In this Page, We are Providing Python Programming \u2013 Set. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Set Set A set is an unordered collection with no duplicate elements. Basic uses include membership testing, eliminating duplicate entries from sequence, mathematical operations like union, intersection, difference, symmetric difference …<\/p>\n

Python Programming \u2013 Set<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Programming \u2013 Set - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-programming-set\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Programming \u2013 Set - Python Programs\" \/>\n<meta property=\"og:description\" content=\"In this Page, We are Providing Python Programming \u2013 Set. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Set Set A set is an unordered collection with no duplicate elements. Basic uses include membership testing, eliminating duplicate entries from sequence, mathematical operations like union, intersection, difference, symmetric difference … Python Programming \u2013 Set Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-programming-set\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-21T10:45:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:22:05+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prasanna\" \/>\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\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-programming-set\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-programming-set\/\",\"name\":\"Python Programming \u2013 Set - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-10-21T10:45:41+00:00\",\"dateModified\":\"2023-11-10T06:22:05+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-set\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-programming-set\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-programming-set\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Programming \u2013 Set\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-programming-set\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-set\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\"},\"headline\":\"Python Programming \u2013 Set\",\"datePublished\":\"2023-10-21T10:45:41+00:00\",\"dateModified\":\"2023-11-10T06:22:05+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-set\/#webpage\"},\"wordCount\":243,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-programming-set\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\",\"name\":\"Prasanna\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"caption\":\"Prasanna\"},\"url\":\"https:\/\/python-programs.com\/author\/prasanna\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Programming \u2013 Set - Python Programs","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:\/\/python-programs.com\/python-programming-set\/","og_locale":"en_US","og_type":"article","og_title":"Python Programming \u2013 Set - Python Programs","og_description":"In this Page, We are Providing Python Programming \u2013 Set. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Set Set A set is an unordered collection with no duplicate elements. Basic uses include membership testing, eliminating duplicate entries from sequence, mathematical operations like union, intersection, difference, symmetric difference … Python Programming \u2013 Set Read More »","og_url":"https:\/\/python-programs.com\/python-programming-set\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-21T10:45:41+00:00","article_modified_time":"2023-11-10T06:22:05+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Prasanna","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-programming-set\/#webpage","url":"https:\/\/python-programs.com\/python-programming-set\/","name":"Python Programming \u2013 Set - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-10-21T10:45:41+00:00","dateModified":"2023-11-10T06:22:05+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-programming-set\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-programming-set\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-programming-set\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Programming \u2013 Set"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-programming-set\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-programming-set\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb"},"headline":"Python Programming \u2013 Set","datePublished":"2023-10-21T10:45:41+00:00","dateModified":"2023-11-10T06:22:05+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-programming-set\/#webpage"},"wordCount":243,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-programming-set\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb","name":"Prasanna","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","caption":"Prasanna"},"url":"https:\/\/python-programs.com\/author\/prasanna\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3398"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=3398"}],"version-history":[{"count":1,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3398\/revisions"}],"predecessor-version":[{"id":3399,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3398\/revisions\/3399"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=3398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=3398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=3398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}