type():
- A pre-defined function that returns identity of class if we specify an Object.
- We can create collection object in 2 ways.
List construction:
#1. Initializing with empty list
l1 = [ ]
print(type(l1))
output:
<class 'list'>
#2. Calling constructor
l2 = list()
print(type(l2))
output:
<class 'list'>
#Tuple construction:
#1. Initializing with empty list
t1 = ()
print(type(t1))
<class 'tuple'>
#2. Calling constructor
t2 = tuple()
print(type(t2))
<class 'tuple'>
Set construction:
- Empty set can be constructed only through constructor.
- Direct assignment of empty set becomes dictionary.
- We represent the elements of Dictionary using { }
s = set()
print(type(s))
s = { }
print(type(s))
Output:
<class 'set'>
<class 'dict'>
When we remove all elements from List or Tuple it will show the empty collection using symbols
l = [10, 20, 30, 40]
print(l)
l.clear()
print(l)
Output:
[10, 20, 30, 40]
[]
But when we empty a set it will show the collection type using its identity.
s = {10, 20, 30, 40}
print(s)
s.clear()
print(s)
Output:
{40, 10, 20, 30}
set()
- Set is allowed to store only Immutable objects such as Numbers, Strings and Tuples.
- List should not be the element of Set because it is Mutable object.
s = {10,20,(30,40)}
print(s)
s = {10,20,[30,40]}
print(s)
Output:
{10, 20, (30, 40)}
TypeError: unhashable type: 'list'