Basic Data Structure in Python 3
Some of the basic python data structure namely list, dictionary, tuple, and set. This is very useful when you write your program algorithm. I will explain how to create and initialize these data structures one by one.List - It contains mixed types of data like string, float, and an integer. It can also contain the same kind of data and as well as the list of a list like 2-dimensional arrays. Furthermore, it can contain set and tuple as its elements.
How to create and initialise it?names=[] or names=list()
Examples:
names=['Pema', 'Sangay', 'Dorji']
info=list(['sam',12, 'height',6.6])
It comes along with many useful functions like append, count, sort, reverse, remove and so on. In order to view all the available functions or methods, you have to go into the python command line and use the following commands.
>>>dir(list) - it will list all the available functions
>>>help(list.append) - It will give you the hint about what the function does.
Dictionary/Dict - It contains the data in key-value pair like that of a dictionary. Where a word is there and its meaning is written by side.
How to create and initialise it?name_age={} or name_age=dict()
Example:
name_age={'Nima':21,'John':26,'Dorji':12} or
name_age=dict({'Nima':21,'John':26,'Dorji':12})
It also has built-in functions like get, keys, values and so on.
>>>dir(dict) - it will list all the available functions
>>>help(dict.get) - It will give you the hint about what the function does.
Tuple - It contains values which can not be changed like the hash values.
How to create and initialise it?
id_number=() or id_number=tuple()
Example:
id_number=(1234,3121,1231)id_number=tuple((1234,3121,1231))
Some of the functions available are count, index and so on.
>>>dir(tuple) - it will list all the available functions
>>>help(tuple.count) - It will give you the hint about what the function does.
Set - It will contain only unique items. There will be no duplication entry available in the set.
How to create and initialise it?
name=set()
Example:
name={'sam', 'ram'}
age=set({12,22,11})
It also has the built-in function available such as pop, union, remove and so on.
>>>dir(set) - it will list all the available functions
>>>help(set.pop) - It will give you the hint about what the function does.
>>>help(set.pop) - It will give you the hint about what the function does.