Site icon

Learn C – Python data structure

This  is in continuation of  Learn C-Python programming .

Key Takeaway : Understanding of data structure elements(Strings,Lists,Tuples) in Python

Strings 

Slicing  or Extract a part using positive & negative indexing  

Array vs Lists 

>>> a=[True,1,5,6,"name","class"]
>>> print(a)
[True, 1, 5, 6, 'name', 'class']
>>> a[0]
True
>>> a[-1]
'class'
>>> b=[8,'marks']
>>> c=a+b
>>> print(c)
[True, 1, 5, 6, 'name', 'class', 8, 'marks']
#Try type(c[0])

 List can be used as stack(LIFO)

>>> stack=[1,2,3,"test",7]
>>> stack
[1, 2, 3, 'test', 7]
>>> stack.append(9)
>>> stack
[1, 2, 3, 'test', 7, 9]
>>> stack.pop()    
9
>>> stack
[1, 2, 3, 'test', 7]

 List can also be used as queue(FIFO)

>> from collections import deque
>>> queue=deque(["test",1,5,6"OK",7])
>>> queue
deque(['test', 1, 5, 6, 'OK', 7])
>>> queue.popleft()
'test'
>>> queue
deque([1, 5, 6, 'OK', 7])   
#queue.append() will add element to right end
>>> del queue[3]
>>> queue
deque([1, 5, 6, 7])

Tuples

>>> a=2,3,"test",4,True
>>> a
(2, 3, 'test', 4, True)
>>> type(a[-1])
<class 'bool'>
>>> a[2:-1]
('test', 4)
>>> a[2:4]
('test', 4)
#Try type(a) 


I have discussed this at   Learn C-Python Video  in detail.

I am trying to improve usability of my  site.If you find something missing, Any feedback in comment box will help to  take corrective action.  Please subscribe to YouTube channel Embedkari  for additional embedded related stuff.

Embedkari  provides  100 mins videos on step-by-step Python uses with Cloud and Machine Learning. These videos are under low cost training and kept at Rs500 only.. Please check the detail  of Low Cost Training Option1.

Exit mobile version