Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Set in Python

Set is a collection of finite elements, It does not contain any sequence(index) hence we can display set elements randomly.

Set always display unique elements, It will be declared using    {}.


x = {"C","CPP","DS","PHP","Java",".NET"}



Example of Set:-

x = {"C","CPP","DS","PHP","Java",".NET","C","CPP"}

for data in x:
   print(data)



How to add elements in Set from User Input?

Note:- Python use {} to declare dictionary and set both but {} is especially for dictionary hence to declare empty set we should use set().

x = set()

size = int(input("enter size of set"))

for i in range(0,size):
    item = input("Enter item")
    x.add(item)


print("Set elements is ")
for i in x:
    print(i)


Where we use to set in python:-

1) When we want to display random elements then we can use set


2)  When we want to display unique elements then we can use set


for example, OTP is a random number it can be implemented by Set.


Predefined Method Of Set:-

x = {"C","CPP","DS","PHP","Java",".NET","C","CPP"}

1)  add() :-   It is used to add elements in set

      x.add("PYTHON")

2)  remove():-  It is used to remove an element in the set

      x.remove("C")

3)  max(),min():-  it return max and minimum element

     print(max(x))
     print(min(x))

4) sum():-    It is used to calculate the sum of set elements

     print(sum(x))

5)   union():-   It is used to combine two different subsets in a set.

    a = {1,2,3,4}
    b =  {2,3,7,8,9}
    c= a.union(b)

6)  intersection():- It will return common elements from set

      a = {1,2,3,4}
      b =  {2,3,7,8,9}

      c= a.intersection(b)


7) difference():-   It is used to use minus operation in the set means the elements which will exist under x will be removed from y.

      a = {1,2,3,4}
      b =  {2,3,7,8,9}
      c= a.difference(b)

   

Complete Example of Set Using Program:-

x = {1,12,23,34,45,11,3}
y = {2,3}
x.add(9)
x.remove(1)
print(max(x))
print(min(x))
print(sum(x))
print(x.union(y))
print(y.union(x))
print(x.intersection(y))
print(x.difference(y))
print("data is ")
for data in x:
   print(data)
 

ASSIGNMENT of SET:-

1)  WAP to find the max element in a set not using max()?

2)  WAP to reverse set elements (if random then display actual random and reverse of random elements)?

      s={3,11,23,34,56,1,78,19,17,98,1}

s1 = [int]*len(s)
c=0
for i in s:
   s1[c]=i
   c=c+1
for i in range(len(s)-1,-1,-1):
  print(s1[i])


3)  WAP to display prime element in Set?







12 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. #WAP to find the max element in a set not using max()?
    m=0
    s=set()
    item=int(input("enter value that you want to insert"))
    for i in range(0,item):
    value=input("enter item")
    s.add(value)
    for x in s:
    if x>m:
    m=x
    print("max value is=",m)




    ReplyDelete
  2. #WAP to display prime element in Set?
    s=set()
    c=0
    item=int(input("enter value want to check prime number"))

    for i in range(0,item):
    item=input("enter value")
    s.add(item)
    for i in s:

    if i%2==0:
    c=c+1
    break
    if c==0:
    print("This is a prime number",i)
    else:
    print('This is a not number.')

    ReplyDelete
  3. Q:- WAP to find the max element in a set not using max() ??
    Solution:-
    a=0
    s=set()
    item=int(input("enter value that you want to insert"))
    for i in range(0,item):
    value=input("enter item")
    s.add(value)
    for b in s:
    if b>a:
    a=b
    print("max value is=",a)

    ReplyDelete
  4. Q:- WAP to display prime element in Set ??
    Solution:-
    a=set()
    x=0
    item=int(input("enter value want to check prime number"))
    for i in range(0,item):
    item=input("enter value")
    a.add(item)
    for i in a:
    if i%2==0:
    x=x+1
    break
    if x==0:
    print("This is a prime number",i)
    else:
    print('This is a not number.')

    ReplyDelete
  5. DOUBT------

    Program to find max in set-->>

    x=set()
    h=0
    size=int(input("enter size of set"))
    for i in range(0,size):
    element=input("enter element")
    x.add(element)
    if x>h:
    h=x
    print("max is",h)

    Error-->>

    if x>h:
    TypeError: '>' not supported between instances of 'set' and 'int'

    Sir kaise execute kare????

    ReplyDelete
  6. x=set()
    h=0
    size=int(input("enter size of set"))
    for i in range(0,size):
    element=input("enter element")
    x.add(element)

    for x1 in x:
    if x1>h:
    h=x1
    print("max is",h)

    ReplyDelete
  7. DOUBT----->Program of max element in set-->

    x=set()
    h=0
    size=int(input("enter size of set"))
    for i in range(0,size):
    element=input("enter element")
    x.add(element)

    for x1 in x:
    if x1>h:
    h=x1
    print("max is",h)

    Error-->

    if x1>h:
    TypeError: '>' not supported between instances of 'str' and 'int'

    ReplyDelete
  8. DOUBT-->>Program to display prime number in a set-->>



    x=set()
    p=0
    size=int(input("enter the size of set"))
    for i in range(0,size):
    num=input("enter the number")
    x.add(num)
    for i in x:
    if i%2==0:
    p=p+1
    if p==2:
    print("it is prime number")
    else:
    print("it is not a prime number")

    ERROR-->>
    if i%2==0:
    TypeError: not all arguments converted during string formatting

    ReplyDelete
  9. deependra singh jadaunNovember 27, 2020 at 6:12 PM

    wap tofind max. element from given no. of set elements:
    x={1,2,3,4,5,6,7,8,9,56,50,0}
    m=0
    for i in x:
    if i>m:
    m=i
    print(m)

    ReplyDelete
  10. deependra singh jadaunNovember 27, 2020 at 11:23 PM

    wap to display prime elements from a given set:

    x={2,3,4,5,6,7,8,9,10}
    for j in x:

    for i in range (2,j):
    if j%i ==0:
    print("this number is not prime ", j)
    break
    else:
    print("this is prime element", j)

    ReplyDelete
    Replies
    1. x={2,3,4,5,6,7,8,9,10}
      for j in x:
      for i in range (2,j):
      if j%i ==0:
      break
      else:
      print(j)

      Delete
    2. x={2,3,4,5,6,7,8,9,10}
      y=set()
      for j in x:
      for i in range (2,j):
      if j%i ==0:
      break
      else:
      y.add(j)


      print(y)

      Delete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post