As we have gone through details related to list
and set
, now let us get an overview of dict
and tuple
in Python.
-
dict
- Group of heterogeneous elements
- Each element is a key-value pair.
- All the keys are unique in the
dict
. dict
can be created by enclosing elements in{}
. Key-Value pairs in each element are separated by:
- example{1: 'a', 2: 'b', 3: 'c', 4: 'd'}
- Empty
dict
can be initialized using{}
ordict()
.
-
tuple
- Group of heterogeneous elements.
- We can access the elements in
tuple
only by positional notation (by using an index). tuple
can be created by enclosing elements in()
- example(1, 2, 3, 4)
.
d = {'id': 1, 'first_name': 'Scott', 'last_name': 'Tiger', 'amount': 1000.0} # dict
d
type(d)
d = dict() # Initializing an empty dict
d
d = {} # d will be of type dict
type(d)
t = (1, 'Scott', 'Tiger', 1000.0) # tuple
type(t)
t
t = ()
t
type(t)
t = tuple()
t