Sequential types in python
A) Tuple –
If I use () this kind of braces then it makes a tuple.
Is immutable and when u going to change that element a specific position it gives an error.
x=(1,2,3,)
>>> x
(1, 2, 3)
>>> type(x)
b) List
When we use [ ] this kind of braces then it makes a list and it is mutable. So you can modify by appending a value. Also, list can
contain list inside this also.
a=[[10,20],[10,20,30]] ——list inside list
>>> y=[1,2,3]
>>> y
[1, 2, 3]
>>> y[1]=sagar
>>> y
[1, Sagar, 3]
>>> type(y)
Index – is used to find out index of element in list
c.index(2)
append- it append or insert an element at last position of the list
>>> c.append(5)
Insert – it is used to insert an element at a specific position in the list and you have to give 2 parameters to insert method one is a position where to insert and other is a specific element.
c.insert(4,5)
c) Dictionary
Use curly brace notation for declaring it. In programming world or real-time example, it is hardly possible to remember the index of the element in the array so we need to storage from that we can access the values based on keys and for this dictionary is introduced. It is a collection of list element but unlike the list to retrieve the value from the dictionary, it uses key while list uses an index of the element. A list has an order but dictionary don’t have an order. 3 methods of dictionary keys(),values() and items()
By using key you can retrieve the value of dictionary but if key does not present in dictionary then it gives error so for that purpose you can use get method of dictionary and it requires 2 parameters one is key itself and another default value or fall back value which is return when value does not present in dictionary.
d.get(‘key’,default value)
>>> x=dict(a=1,b=2,c=3)
>>> x
{‘a’: 1, ‘b’: 2, ‘c’: 3}
>>> d={“one”:1,”two”:2,”3″:”three”}
>>> d
{‘one’: 1, ‘two’: 2, ‘3’: ‘three’}
>>> d[“one”]
1
>>> d.keys()
dict_keys([‘one’, ‘two’, ‘3’])
>>> d.values()
dict_values([1, 2, ‘three’])
If I use () this kind of braces then it makes a tuple.
Is immutable and when u going to change that element a specific position it gives an error.
x=(1,2,3,)
>>> x
(1, 2, 3)
>>> type(x)
b) List
When we use [ ] this kind of braces then it makes a list and it is mutable. So you can modify by appending a value. Also, list can
contain list inside this also.
a=[[10,20],[10,20,30]] ——list inside list
>>> y=[1,2,3]
>>> y
[1, 2, 3]
>>> y[1]=sagar
>>> y
[1, Sagar, 3]
>>> type(y)
Index – is used to find out index of element in list
c.index(2)
append- it append or insert an element at last position of the list
>>> c.append(5)
Insert – it is used to insert an element at a specific position in the list and you have to give 2 parameters to insert method one is a position where to insert and other is a specific element.
c.insert(4,5)
c) Dictionary
Use curly brace notation for declaring it. In programming world or real-time example, it is hardly possible to remember the index of the element in the array so we need to storage from that we can access the values based on keys and for this dictionary is introduced. It is a collection of list element but unlike the list to retrieve the value from the dictionary, it uses key while list uses an index of the element. A list has an order but dictionary don’t have an order. 3 methods of dictionary keys(),values() and items()
By using key you can retrieve the value of dictionary but if key does not present in dictionary then it gives error so for that purpose you can use get method of dictionary and it requires 2 parameters one is key itself and another default value or fall back value which is return when value does not present in dictionary.
d.get(‘key’,default value)
>> d={“one”:1,”two”:2,”3″:”three”}The second way of creating a dictionary is.
>>> x=dict(a=1,b=2,c=3)
>>> x
{‘a’: 1, ‘b’: 2, ‘c’: 3}
>>> d={“one”:1,”two”:2,”3″:”three”}
>>> d
{‘one’: 1, ‘two’: 2, ‘3’: ‘three’}
>>> d[“one”]
1
>>> d.keys()
dict_keys([‘one’, ‘two’, ‘3’])
>>> d.values()
dict_values([1, 2, ‘three’])
Comments
Post a Comment