top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Python Sets

Writer's picture: Kala Maya SanyasiKala Maya Sanyasi

Python sets are unordered collection of items with no duplicates and the elements cannot be changed. It can be used to perform mathematical set operations.

Sets are created by placing the elements inside the curly brackets { }, separated by commas. Empty set is created with set( ) function.

#creating Sets
#set of integers
my_set1 = {1, 2, 3, 4}

#set of mixed datatype 
my_set2 = {5.4, (1,2,3), 'python'}

#empty set
my_set3 = set()

#set created from list
my_set4 = set([1, 2, 3])

Since sets are unordered, indexing makes no meaning to set.

To add elements we use add() and update() methods, where add() is used to add single element and update() is used to add multiple elements to the set.

#Modifying set
#adding elements
#add() method to add single element
my_set = {1, 3, 4}
my_set.add(2)
print(my_set)
{1, 2, 3, 4}


#update() method to add multiple elements
my_set1 = {1, 3, 5, 7}
my_set1.update([2, 4], {6, 8, 9})
print(my_set1)
{1, 2, 3, 4, 5, 6, 7, 8, 9}

An item can be removed by discard() and remove() methods. Where discard() method leaves a set unchanged if the element is absent. Whereas remove() will raise an error if the element is absent in the set.

#removing elements by discard() and remove() methods

#discard() method leaves the set unchanged if the element is absent in the set
my_set = {1, 2, 3, 4, 6}
my_set.discard(4)
print(my_set)
{1, 2, 3, 6}

my_set.discard(5)
print(my_set)
{1, 2, 3, 6}


#remove() method will raise error if the element is absent in the set
my_set1= {1, 2, 3, 5, 6}
my_set1.remove(3)
print(my_set1)
{1, 2, 5, 6}

my_set1.remove(4)
print(my_set1)
KeyError: 4




0 comments

Recent Posts

See All

Commentaires


COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page