Sunday, September 16, 2018

Python Ninja Bootcamp 11-Set

Learn Python like a Professional! Take You from 0 to Hero




In this Python lecture, we will learn about the Set.

"Set is an unordered collection of unique(no duplicate) and immutable items". 

Let's look at Set example-


Create Set

Set is defined by placing all the elements inside curly braces { }, separated by comma or by using the built-in function set( ).

s={1,2,3,4,5}
print(s)
>> { 1,2,3,4,5 }



Another method of Creating Set


We have built-in set( ) function to create a set.


s= set() # create a empty set
print(s)
>> set( )

We can add a single element using the add( ) method.
And multiple elements using the update( ) method.

# set can hold different object type (integer, float, string etc)
s.add(1) # add an element to the set s.add(1.2) # 1.2 is added in the set s.add('string') # 'string' is added in the set

print(s)

>> {1, 'string', 1.2}

Notice element may appear in a different order then the order followed while adding the element

We can update the set with the union of list, tuple & set

s.update( [3,4,5] , ('three','four') , {'five',2} ) 


print(s)
>> { 1, 'string', 3, 4, 1.2, 5, 'four', 2, 'five', 'three' }

# Try to add the same element
s.add(1)
print(1)
>> { 1, 'string', 3, 4, 1.2, 5, 'four', 2, 'five', 'three' }

Notice element 1 is not added again. This happened because a set is the collection of a unique element.

We can cast a list with multiple repeated elements to the set to get the unique elements.

l=[1,1,2,2,3,3,3,4,4,4,4]

print( l )
>> [1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4]

#cast as set to get unique element
print(set(l))
>> {1,2,3,4}

      .
Remove the element from the set

# we have a given set
s={1,2,3,4,5}
print(s)
>> {1,2,3,4,5}

s.remove(1) # remove an element from set
print(s)
>>  {2,3,4,5}

Now try to remove 1 again from the set s
s.remove(1)
>> keyError: 1

Notice remove( ) method returns error on removing non-existing element.

We have discard() method to remove an element. If the item does not exist in the set, it remains unchanged. 

s.discard(2)
print(s)
>> {3,4,5}

Again try to remove 2.

s.discard(2)
print(s)
>> {3,4,5}

print(s.pop()) # remove and return an arbitrary element 
>> 3

print(s) 
>> {4,5}


s.clear(2) # remove the all the element from set
print(s)
>> set( )



Set Operation

Let's create the two sets:


#Create a even set between 0 t0 10
even={ 2,4,6,8,10 }
print(even) 
>> {2,4,6,8,10}

#Create a prime set between 0 t0 10
prime={ 2,3,5,7 }
print(prime) 
>> {2,3,5,7 }

1. Union

Union of even and prime is a set of all elements from both sets





In Python, union is performed by using union() method.


even.union(prime) # return the union of sets 
>> {2, 3, 4, 5, 6, 7, 8, 10}


2. Intersection

Intersection of even and prime is a set of common elements from both sets.





In Python, intersection is performed by using intersection() method


even.intersection(prime)  # return the intersection of sets
>> {2}

3. Difference

Difference of even and prime is a set of all elements in the even set but not the prime set.





In Python, difference is performed by using difference( ).

even.difference(prime) # return the difference of two or more set
>> {4, 6, 8, 10}


EDITS ARE WELCOMED!!

In the next Blog, we will discuss Boolean.

https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-12-boolean.html

......................................................................................................................................

Follow the link below to access Free Python Lectures-
https://www.youtube.com/sngurukul

Instagram-
https://www.instagram.com/python.india/

View the Jupyter Notebook for this lecture

Download the Jupyter Notebook for this lecture 


Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com

No comments:

Post a Comment