Set<\/strong><\/p>\nA 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>\nBeing 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>\nA set can be created in many ways.<\/p>\n
Using curly braces<\/strong><\/p>\nNon-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>\nSet comprehension<\/strong><\/p>\nPython 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>\nUsing built-in function<\/strong><\/p>\nThe 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>\nDeleting set<\/strong><\/p>\nTo 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>\nLooping techniques<\/strong><\/p>\nIt 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>\nMembership operation<\/strong><\/p>\nSet 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\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n