Set data type:
It is a collection of elements
of different datatypes without duplicate elements where order is not important.
Examples:
1. Set is Unordered List
1. Set is Unordered List
>>>
s={10,20,30,"Hello"}
>>> type(s)
<class 'set'>
>>> print(s)
{'Hello', 10, 20, 30}
(or)
s=set([10,20,30]) # create
2. Set may contain different data types
(or)
s=set([10,20,30]) # create
2. Set may contain different data types
>>>
a={10,2.5,True,3+4j,’Ram’}
>>> type(a)
<class 'set'>
>>> print(a)
{True, 2.5, 10, 'Ram', (3+4j)}
3. Since it unordered list so index concept is not available and slicing operation is also not available
3. Since it unordered list so index concept is not available and slicing operation is also not available
>>> s={10,30,50}
>>> s[0]=20 #error
>>> s[1:3] #error there
is no slicing operation since index is
not available
4. Dynamically grow able
4. Dynamically grow able
>>>
s={10,20,False,2.3}
>>> s.add(40)
>>> print(s)
{False, 40, 2.3, 10, 20}
>>> s.remove(2.3)
>>> print(s)
{False, 40, 10, 20}
5. Duplication is not allowed
5. Duplication is not allowed
>>>
s={'h','e','l','l','o','o'}
>>> print(s)
{'o', 'e', 'l', 'h'}
By using del keyword we can delete entire set
By using del keyword we can delete entire set
Ex:
s={'h','e','l','l','o','o'}
>>> del s
>>> print(s) #it is
deleted entire set
Assignments:
1. Which of the following is not the correct syntax for creating a set?
a) set([[1,2],[3,4]])
b) set([1,2,2,3,4])
c) set((1,2,3,4))
d) {1,2,3,4}
Answer: a
2. Which of the following statements is used to create an empty set?
a) { }
b) set()
c) [ ].
d) ( )
Answer: b
Explanation: { } creates a dictionary not a set. Only set() creates an empty set.
3. What is the output of the following piece of code when executed in the python shell?
>>> a={5,4}
>>> b={1,2,4,5}
>>> a<b
a) {1,2}
b) True
c) False
d) Invalid operation
Answer: b
Explanation: a<b returns True if a is a proper subset of b
4. If a={5,6,7,8}, which of the following statements is false?
a) print(len(a))
b) print(min(a))
c) a.remove(5)
d) a[2]=45
Answer: d
Explanation: The members of a set can be accessed by their index values since the elements of the set are unordered.
5. If a={5,6,7}, what happens when a.add(5) is executed?
a) a={5,5,6,7}
b) a={5,6,7}
c) Error as there is no add function for set data type
d) Error as 5 already exists in the set
Answer: b
Explanation: There exists add method for set data type. However 5 isn’t added again as set consists of only non-duplicate elements and 5 already exists in the set. Execute in python shell to verify.
Assignments:
1. Which of the following is not the correct syntax for creating a set?
a) set([[1,2],[3,4]])
b) set([1,2,2,3,4])
c) set((1,2,3,4))
d) {1,2,3,4}
Answer: a
2. Which of the following statements is used to create an empty set?
a) { }
b) set()
c) [ ].
d) ( )
Answer: b
Explanation: { } creates a dictionary not a set. Only set() creates an empty set.
3. What is the output of the following piece of code when executed in the python shell?
>>> a={5,4}
>>> b={1,2,4,5}
>>> a<b
a) {1,2}
b) True
c) False
d) Invalid operation
Answer: b
Explanation: a<b returns True if a is a proper subset of b
4. If a={5,6,7,8}, which of the following statements is false?
a) print(len(a))
b) print(min(a))
c) a.remove(5)
d) a[2]=45
Answer: d
Explanation: The members of a set can be accessed by their index values since the elements of the set are unordered.
5. If a={5,6,7}, what happens when a.add(5) is executed?
a) a={5,5,6,7}
b) a={5,6,7}
c) Error as there is no add function for set data type
d) Error as 5 already exists in the set
Answer: b
Explanation: There exists add method for set data type. However 5 isn’t added again as set consists of only non-duplicate elements and 5 already exists in the set. Execute in python shell to verify.
Net topic : frozenset
datatype
Previous Topic: TupleAssignment 4
No comments:
Post a Comment