Skip to main content

9.Python Sets

PYTHON SETS

    A set is a collection which is unordered and unindexed. In Python sets are written with curly brackets. A set is also defined as an unordered collection of items. Every set element is unique  and must be immutable.However, a set itself is mutable. We can add or remove items from it.Sets can also be used to perform mathematical set operations like union, intersection, symmetric difference, etc.Python’s set class represents the mathematical notion of a set. The major advantage of using a set, as opposed to a list, is that it has a highly optimized method for checking whether a specific element is contained in the set. It is impossible to access the  items in a set by referring to an index, since sets are unordered items without indexes.


     

    Once a set is created, It is only possible to add new items you cannot change the existing items. add() method is used to insert the item into the set. update() method is used to add more than one item to the set. len() method is used find the length of the set. the remove() or the discard() method is used to delete an item from the set. The clear() method is used to empty the set. The del keyword will delete the set completely: You can use the union() method that returns a new set containing all items from both sets, or the update() method that inserts all the items from one set into another. 

    A set is created by placing all the items (elements) inside curly braces {}, separated by comma, or by using the built-in set() function. It is also possible to use the set() constructor to make a set. Empty curly braces {} will make an empty dictionary in Python. To make a set without any elements, we use the set() function without any argument.

Program 

wizard = {"Harry", "Ron", "Hermione"} # set creation with {}
print(wizard)
wizard2 = set(("malfoy", "cedric", "luna")) # set() creation
print(wizard2)
for x in wizard:
        print(x)
wizard.add("Ginny") #adding Ginny to wizard
print(wizard)
wizard2.update(["hagrid", "Dumbledore"]) # adding 2 to wizard2
print(wizard2)
print(len(wizard)) #count of elements in wizard set
wizard2.remove("cedric") # removing cedric from wizard2
print(wizard2)
wizard.discard("Ginny") #discard ginny from wizard
print(wizard)
wizard3 = wizard.union(wizard2) # union of 2 sets
print(wizard3)
wizard3.clear() #making the set empty
print(wizard3)
del wizard3 # set is deleted
print(wizard3) # since set is deleted it will produce error
Output 
{'Ron', 'Hermione', 'Harry'}
{'cedric', 'luna', 'malfoy'}
Ron
Hermione
Harry
{'Ron', 'Hermione', 'Harry', 'Ginny'}
{'cedric', 'hagrid', 'luna', 'Dumbledore', 'malfoy'}
4
{'hagrid', 'luna', 'Dumbledore', 'malfoy'}
{'Ron', 'Hermione', 'Harry'}
{'Ron', 'luna', 'Dumbledore', 'Harry', 'hagrid', 'Hermione', 'malfoy'}
set()

Traceback (most recent call last):
File "main.py", line 21, in <module>
print(wizard3)
NameError: name 'wizard3' is not defined

Watch the video below to learn about python sets!!

Happy Learning!!

Comments

Popular posts from this blog

Get Ethical Hacking Project #1

Thanks for your interest in our course This is a paid course, 150 rs for Indian people, 5$ for international people Instructions to get course: Indian people can get the course by paying using this UPI id   tamilhacks@ybl  (KARTHIKEYAN T) send 150 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)     The above mentioned method is most preferable. if you don't have a upi id app, install  phonepe  or  google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100 Rs for tax and other charges here )   After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that . the course is in zip format, so after downloading the file extra

Learn c++ in Tamil | Complete guide and tutorial | Beginner to Advance all concepts explained.

hi guys welcome , all the coding used in video are available here : /* hi guys welcome to learn c++ in tamil what is C++ how what software your own program schools,clg,institute.... start 1.interest 2.patience 3.fun 4.practise computer program - app - programming source code - hardware - fb running , executing machine language :  101010 assembly lang : mov h1; high lang : c , c++....java , js , python  eg :  a+b .....1001010  compiler :  human to machine              machine to human  rules :  a+b;  usage:  games, manufacturing , embedded , finance , graphics , etc...  Guide: 1.problem 2.solution - alternative 3.program  - C++ 4.compile  - turboc , ide , codeblocks , cmd 5.result   - library file - linker : #include<iostream.h>    input , output std::cout<<"welcome";   standard character out << " "; primary section : 1.statements   ;  end 2.functions   main() 3.syntax  , syntax error pra

Get Bug Bounty Course

Thanks for your interest in our course Limited Offer This is a paid course, Rs 10000 ,Rs 1000  for Indian people, 75$ 15$ for international people , 50$ 10$ for Srilankan people Instructions to get course: Indian people can get the course by paying using this UPI id  tamilhacks@ybl  send 1000 Rs to this upi id with subject bug bounty and after payment take a screenshot and send that to email id tamilhackstech@gmail.com - you will get the course within 24 hours. (there will be no tax, when paying using upi id)    The above mentioned method is most preferable. if you don't have a upi id app, install phonepe or google pay   app and register there and pay easily  or  pay here using the alternate method to get this course, this link cost 18% gst for the course . (nearly 30-100Rs for tax and other charges here ) After payment you will receive a payment confirmation mail along with a downlink for the course, you can download the course file using that. As for international peopl