Type Here to Get Search Results !

Write Python program to perform following operations on Set: Create Set, Access Set elements, Update Set, Delete Set

 Write Python program to perform following operations on Set: Create Set, Access Set elements, Update Set, Delete Set


Mathematically a set is a collection of items not in any particular order. A Python set is

similar to this mathematical definition with below additional conditions.

The elements in the set cannot be duplicates.

#Total Programming Code

 Write Python program to perform following operations on Set: Create Set, Access Set elements, Update Set, Delete Set


The elements in the set are immutable (cannot be modified) but the set as a whole

is mutable.

There is no index attached to any element in a Python set. So they do not support

any indexing or slicing operation.

The sets in Python are typically used for mathematical operations like union,

intersection, difference and complement etc.


a) Creating a set:

A set is created by using the set() function or placing all the elements within a pair of

curly braces.

Example:

>>> a={1,3,5,4,2}

>>> print("a=",a)

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

>>> print(type(a))

<class 'set'>


b) Accessing values in a set: We cannot access individual values in a set. We can only

access all the elements together. But we can also get a list of individual elements by

looping through the set.

Example:

Num=set([10,20,30,40,50])

for n in Num:

print(n)

Output:

10

20

30

40

50


c) Updating items in a set: We can add elements to a set by using add() method. There is

no specific index attached to the newly added element.

Example:

Num=set([10,20,30,40,50])

Num.add(60)

print(Num)

Output:

{10,20,30,40,50,60}


d) Removing items in set:

We can remove elements from a set by using discard() method. There is no specific

index attached to the newly added element.

Example:

Num=set([10,20,30,40,50])

Num.discard(50)

Print(Num)

Output:

{10,20,30,40}


Examples


1. Write a Python program to create a set, add member(s) in a set and remove oneitem from set.


no = set([20,30,50,80,90])
print("Default set is :",no)
no.add(100)
print("After adding 100 in set :",no)
no.remove(30)
print("After remove 30 in set :",no)


2. Write a Python program to perform following operations on set: intersection ofsets, union of sets, set difference, symmetric difference, clear a set.


set1 = set([20,30,50,60])
set2 = set([90,30,20,100])

print("Intersection of set :",set1&set2)
print("Union of set :",set1|set2)
print("Difference of set :",set1-set2)
print("Symmetric difference of set :",set1^set2)
set1.clear()
print("set clear",set1)


3. Write a Python program to find maximum and the minimum value in a set.


set1 = set([20,30,90,100,10])

print("Minimum value of set :",min(set1))
print("Maximum value of set :",max(set1))


4. Write a Python program to find the length of a set.


set1 = set([20,30,40,50,60,70])
print("Length of set is :",len(set1))
Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Below Post Ad