Tuesday, June 12, 2018

Sets in Python



What is Set?


Set is an unindexed and unordered collection of unique item.

Set are written with curly brackets.

Many operations perform on sets

intersection

union

difference

symmetric_difference

issuperset

issubset

isdisjoint


we learn all operation of set later chapter now we learn simple set. Because of it a very complicated once you can learn python basic then we jump on advance part of this course . 


Creating sets:


mySet = {5, 4, 6, 8, 9, 1}

print(myset) # output: {5, 4, 6, 8, 9, 1} 




Check Type :


print(type(mySet)) # Output:<class 'set'> 




In-built Function: set() 



Use Set() function to create set:

mySet = set((5, 4, 6, 8, 9, 1))

print(mySet) # Output:{5, 4, 6, 8, 9, 1} 




In-built Function: len()


To get the length of set

mySet = {5, 4, 6, 8, 9, 1}

print(len(mySet)) #Output: 6 




In-built Function: add()


use to add item into the set

mySet = {5, 4, 6, 8, 9, 1}

mySet.add(7)

print(mysSet) # Output: {7, 5, 4, 6, 8, 9, 1} 




In-built Function: remove()


mySet = {5, 4, 6, 8, 9, 1}

mySet.remove(1)

print(mySet) # Output: {5, 4, 6, 8, 9}





Prev: Tuples In Python

Next Dictionaries in Python

No comments:

Post a Comment

Google+ Followers