SETS IN PYTHON

SAIROSHNI

Set

  • In python,a set is a type of collection in data type.
  • A set is a mutable(changeable),unordered data. It doesn't allow duplicate values.
  • Sets are useful in doing set operation.
  • The provided with operations like,intersection,union, difference and symmetric.
  • Items in set are enclosed by curly brackets { }.
  • Each item is separated by comma (,).

Syntax:

Identifier = { item1, item2, item3}

Creating A Set

  • A set is created by placing all the elements separated by comma within a pair of curly brackets.
  • Since sets are unordered and unindexed ,you cannot be sure in which the items can be integer ,float, string or tuple. 
  • It allows the heterogeneous (different type) type of values in one set. 
  • It automatically eliminates the duplicate items. The set( ) function can also be used to create sets in python.

Syntax:

 Set_Variable = {E1 , E2 , E3 ........En}

Set Methods In Python

The in-built methods of set are used to:

  • create
  • insert
  • remove
  • delete the set 
  • set items

Example:

Using all method program in set

Program:


# create an empty set

my_set = set()


# add items to the set

my_set.add('apple')

my_set.add('banana')

my_set.add('orange')


# print the set

print('Set after adding items:', my_set)


# remove an item from the set

my_set.remove('banana')


# print the set

print('Set after removing an item:', my_set)


# delete the entire set

del my_set


# try to print the set (will result in a NameError)

print(my_set)


Output:


Set after adding items: {'orange', 'banana', 'apple'}
Set after removing an item: {'orange', 'apple'}
Traceback (most recent call last):
   File "set_program.py", line 16, in <module>
     print(my_set)
NameError: name 'my_set' is not defined

Set Operations

  • The set class supports mathematical set operations like union,intersection,difference and symmetric.
  • These operations can be performed either using in-built functions or using operators.
  • The operators work only on onset data structure.
  • The function works on any iterable data structure like list and tuple.

 1. Union Operation

      Union operation combines the elements of sets single  list after eliminating the repeated items .The union operation can be done using single pipe operation (|). 

Syntax:

  NewSet = Set1 | Set2  | Set3.

Program:


# define sets

set1 = {1, 2, 3, 4, 5}

set2 = {3, 4, 5, 6, 7}

# perform the union operation using the | operator

union_set = set1 | set2

print(union_set) 

 


Output:


{1, 2, 3, 4, 5, 6, 7}


2. Union Function

The method union() is used to do union operation.

 Syntax:

   NewSer = Set.union( Set2 ,Set3...)

Program:


# define two sets

set1 = {1, 2, 3, 4, 5}

set2 = {3, 4, 5, 6, 7}


# perform the union operation using the union() method

union_set = set1.union(set2)

print(union_set)



Output:


{1, 2, 3, 4, 5, 6, 7}



3. Intersection Operation

      The intersection operation is to get the elements commonly available in two sets The intersection operation can be done using the logical 'and' (&) operator.

Syntax:

 NewSet = Set1 & Set2

Program:


# define two sets

set1 = {1, 2, 3, 4, 5}

set2 = {3, 4, 5, 6, 7}


# perform the intersection operation using the & operator

intersection_set = set1 & set2

print(intersection_set)



Output:


{3, 4, 5}


4. Intersection Function

      The function intersection () of set class is used to identify the common elements among the given sets The function accepts more than one parameter.The parameter of the function can be or list or tuple.

Syntax:

  NewSet = Set1 intersect(Set2 , Set3 , Set4...)

Program:


# define two sets

set1 = {1, 2, 3, 4, 5}

set2 = {3, 4, 5, 6, 7}


# perform the intersection operation using the intersection() method

intersection_set = set1.intersection(set2)

print(intersection_set)



Output:


{3, 4, 5}



5. Difference Operations

The difference operation gets the set of elements only in the first set, not in the second set. This operation can be performed using the minus(-)operator.

Syntax:

 NewSer = Set1 - Set2

Program:


# define two sets

set1 = {1, 2, 3, 4, 5}

set2 = {3, 4, 5, 6, 7}


# perform the difference operation using the - operator

diff_set = set1 - set2

print(diff_set)



Output: 


{1, 2}



6. Difference Function

 The method difference () gets the set of elements only in the invoking set but not in sets passed as parameters.

Syntax:

 NewSet = Set1 difference (Set2 , Set3 , Set4 ...)

Program:


# define two sets

set1 = {1, 2, 3, 4, 5}

set2 = {3, 4, 5, 6, 7}


# perform the difference operation using the difference() method

diff_set = set1.difference(set2)

print(diff_set) 



Output:


{1, 2}



7. Symmetric Difference Operation

The symmetric  difference operation is to get the elements of two sets after excluding the common elements .This operation can be performed using the exponential operator.

Syntax:

newset= set1 ^ set2

Program:


# define two sets

set1 = {1, 2, 3, 4, 5}

set2 = {3, 4, 5, 6, 7}

# perform the symmetric difference operation using the ^ operator

sym_diff_set = set1 ^ set2

print(sym_diff_set)



Output:

{1, 2, 6, 7}





More topic in Python

Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send