Important Notice:

Course Content
Set Data Type in Python (Chapter-14) M3-R5
About Lesson

Creating Set

            set के elements को curly braces({}) के अन्दर लिखा जाता है और हर एक element को comma(,) से seperate किया जाता है।

set1 = {“H”, “e”, “l”, “l”, “o”}

#elements in curly braces 

set2 = set(“Hello”)

#passing string to set function 

set3 = set([“H”, “e”, “l”, “l”, “o”])

#passing list to set function 

set4 = set((“H”, “e”, “l”, “l”, “o”))

#passing tuple to set function 

set5 = set()

#empty set

Mixed Data Type Set

  • set mutable items को support नहीं करता है। set में [1, 2] ये mutable item है।

   set = {1, “H”, 2.6, [1,2]}      

  Output                                  

set = {1, “H”, 2.6, [1,2]}                                  

TypeError: unhashable type: ‘list’

  • set में tuple का इस्तेमाल किया जा सकता है। क्योंकि set और tuple दोनों immutable होते हैं।

         set = {1, “H”, 2.6, (1,2)}

Iterating Over a Set

              हर element को iterate through print करने के लिए ‘for_in’ loop का इस्तेमाल किया जाता है।

For Example,

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

for i in set:   

      print(i)

 

 

Creating Set

               The elements of a set are written inside curly braces ({}) and each element is separated with a comma (,).

set1 = {“H”, “e”, “l”, “l”, “o”}

#elements in curly braces 

set2 = set(“Hello”)

#passing string to set function 

set3 = set([“H”, “e”, “l”, “l”, “o”])

#passing list to set function 

set4 = set((“H”, “e”, “l”, “l”, “o”))

#passing tuple to set function 

set5 = set()

#empty set

Mixed Data Type Set

  •  set does not support mutable items. This is a mutable item in the set [1, 2].        

set = {1, “H”, 2.6, [1,2]}      

  Output                                  

set = {1, “H”, 2.6, [1,2]}                                  

TypeError: unhashable type: ‘list’

 

  • Tuple can be used in set. Because both set and tuple are immutable.        

set = {1, “H”, 2.6, (1,2)}

Iterating Over a Set

          A ‘for_in’ loop is used to iterate through each element and print it.

For Example,

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

for i in set:   

         print(i)

error: Content is protected !!