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: Mohamed AdelMohamed Adel

Sets are used to store multiple items in a single variable.

Set is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Tuple, and Dictionary, all with different qualities and usage.

A set is a collection which is both unordered and unindexed.

Sets are written with curly brackets.






Creating sets :

now we want to create a set call "my set" and define it from the beginning

that set includes strings. how we can do that ??

#creating sets
#set of strings
myset = {"apple", "banana", "cherry"}
print(myset)
{'banana', 'cherry', 'apple'}

A set can be strings, integers, and boolean values:

set1 = {"abc", 34, True, 40, "male"}

Type


we can use here type() function to tell us what is the type of the variable called myset

print(type(myset))
<class 'set'>

Access items


How we can access items of the set "set1" ??


first, we declare it .second we make a for loop to loop into every item of the set and print it out in the output

#Access items
set1= {True ,34 ,40 ,"male" ,"abc"}
for x in set1:
  print(x)

#output
True
34
40
male
abc

Python - Add Set Items

Adding item in the set can be done in many ways first we can do it with add function second we can do it with an update() function


Add


set1.add("orange")

print(set1)

#output
{True, 34, 40, 'male', 'abc', 'orange'}

update


thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}

thisset.update(tropical)

print(thisset)

#output
{'papaya', 'cherry', 'apple', 'pineapple', 'mango', 'banana'}
thisset = {"apple", "banana", "cherry"}
mylist = ["kiwi", "orange"]

thisset.update(mylist)

print(thisset)

#output
{'banana', 'cherry', 'apple', 'orange', 'kiwi'}

remove items


we can remove an item from the set be remove() function which removes the specific item from the set

thisset = {"apple", "banana", "cherry"}
​
thisset.remove("banana")
​
print(thisset)


#output
{'cherry', 'apple'}

thisset = {"apple", "banana", "cherry"}
​
thisset.remove("banana")
​
print(thisset)

#output
{'cherry', 'apple'}

we can also use pop() to remove the first item in the set

#pop()
​
thisset = {"apple", "banana", "cherry"}
​
x = thisset.pop()
​
print(x)
​
print(thisset)

#output
banana
{'cherry', 'apple'}



0 comments

Recent Posts

See All

Comments


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