Discord Server Red Security Twitter Donation to Red Security Red Security Youtube Channel Red Security Tumblr Profile
Login or Register to Hide ads and Accessing all features on the forum

Tutorial 

Python 2.3: Sets

2 Replies, 1352 Views

[Image: BFMMlbcQvml9HSqXcvNp]
Sets:
           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"}

print(mySet)


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}

mySet.add("Covid")

print(mySet)


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}
mySet1 = {1,2,3,4}

mySet.update(mySet1)

print(mySet)


We can also add other data structures to sets

Code:
mySet = {"apple", 4 , True}
mySet1 = [1,2,3,4]

mySet.update(mySet1)

print(mySet)


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"}
print(myset)
myset.pop()
print(myset)
myset.remove("apple")
print(myset)
myset.clear()
# This print statement will print an empty set
print(myset)
del(myset)
# This line will generate an error as no
# set named myset exist in program
print(myset)


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"}
print("apple" in myset)

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}

set2 = {1,3,5,7,9,10}

# this is for union
print(set1 | set2)
# for intersection
print(set1 & set2)
# difference of two sets
print(set1 -set2)
# set1 - set2 != set2 - set1
# those element which are not common in both sets
print(set1 ^ set2)

Idea 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"
Love me like you do Heart
(This post was last modified: 04-30-2022, 05:22 PM by Covid-19.)
The thing is how can we differentiatr between them?
Sets, Tuples, Lists and Arrays?
Rs
* Thankful to Allah *
Kurdy
(05-01-2022, 09:27 AM)Mr.Kurd Wrote: The thing is how can we differentiatr between them?
Sets, Tuples, Lists and Arrays?

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.
Love me like you do Heart
(This post was last modified: 05-05-2022, 07:06 PM by Covid-19.)

Possibly Related Threads…
Thread Author Replies Views Last Post
  Tutorial Doubly Linked List in Python Covid-19 0 2,247 07-30-2022, 09:15 PM
Last Post: Covid-19
  Tutorial Linked List in Python Covid-19 2 2,638 07-30-2022, 08:25 PM
Last Post: Covid-19
  Tutorial File Handling in Python Covid-19 2 2,670 06-16-2022, 06:17 PM
Last Post: Covid-19
  Tutorial Exceptions in Python Covid-19 0 1,843 06-08-2022, 09:19 PM
Last Post: Covid-19
  Free Automate the boring stuff with Python Covid-19 0 1,607 06-07-2022, 06:58 PM
Last Post: Covid-19
  Tutorial Turtle in Python Covid-19 0 1,841 06-02-2022, 10:14 PM
Last Post: Covid-19
  Tutorial Python 3.5: Class Methods Covid-19 0 1,792 05-29-2022, 01:07 PM
Last Post: Covid-19
  Tutorial Python 3.2: Inheritance in Python Covid-19 2 2,833 05-27-2022, 06:47 PM
Last Post: Covid-19
  Tutorial Python 3.4: Encapsulation and Abstraction Covid-19 0 1,825 05-27-2022, 06:45 PM
Last Post: Covid-19
  Tutorial Python 3.3: Operator Overloading Covid-19 2 2,827 05-26-2022, 08:50 PM
Last Post: Covid-19



Users browsing this thread: 1 Guest(s)