Tutorial Python 2.3: Sets - Printable Version +- Red Security (https://redsecurity.info/cc) +-- Forum: Programming (https://redsecurity.info/cc/forumdisplay.php?fid=5) +--- Forum: Python (https://redsecurity.info/cc/forumdisplay.php?fid=9) +--- Thread: Tutorial Python 2.3: Sets (/showthread.php?tid=7906) |
Python 2.3: Sets - Covid-19 - 04-30-2022 Sets are the data structure in python like lists and tuples but the difference between set and other built-in data structures is that sets are used when we want to add unique elements because set does not allow repetition of elements. Code: mySet = {"apple", "banana", "pine apple", "apple"} In this code it will print apple just one time. Sets are unordered, unindexed and unchangeable. Once a set is created we can not change the value of the item but we can add items to it. Now let’s see what operations can we perform on sets? First let’s add some new items in our set. We use the add() function to add items to the set. Code: mySet = {"apple", 4 , True} We can also add sets to sets but in this case the values present in both sets will be printed once. We use the update() function to add sets. Code: mySet = {"apple", 4 , True} We can also add other data structures to sets Code: mySet = {"apple", 4 , True} discard() and remove function is used to remove an item from the set the pop() function is used to remove the last item of set the clear() function is used to remove all items in sets and del() function is used to delete the entire set Let’s code each and every function and see their effect on program Code: myset = {"apple","banana","orange","pineapple"} Sets are unindexed and it is faster to check whether an item is present in a set or not. Code: myset = {"apple","banana","orange","pineapple"} Let’s see some other operations on sets which we perform on sets in mathematics like union, intersection etc For union of two sets we use ‘|’ operator For intersection of two sets we use ‘&’ operator For difference of two sets we use ‘-’ operator And to get different values of both sets and ignore similar ones we use the ‘^’ operator. Now let’s write it in code for better understanding of these operators. Code: set1 = {1,2,3,4,5,6,7,8,9} Key point: Use sets when you want uniqueness in Items "Keep Good care of your Health Allah Hafiz and Good Bye Till the next post" "Keep Learning and Keep exploring" RE: Python 2.3: Sets - Mr.Kurd - 05-01-2022 The thing is how can we differentiatr between them? Sets, Tuples, Lists and Arrays? RE: Python 2.3: Sets - Covid-19 - 05-05-2022 (05-01-2022, 09:27 AM)Mr.Kurd Wrote: The thing is how can we differentiatr between them? Python has no built-in arrays. Sets are unordered, unchangeable and unindexed. and represented by curly brackets and do not allow duplicate. Lists are used instead of arrays in python and represented by square brackets. Tuple are are ordered and unchangeable and represented by parenthesis Dictionaries are written with curly brackets, and store data values in keys : value pairs, and do not allow duplicates. Hope this helps. |