In [1]:
#
# Praxis Business School
# Prithwis Mukerjee
#
# Data Structures in Python
#
print("Hello to Data Structures")
In [2]:
pList1 = [1,"This is a list"]
print(pList1)
In [3]:
pInt = 2
pReal = 2.318281828
pString = "some Test String"
pList2 = [ pInt, pReal, pString, pList1]
print(pList2)
In [4]:
print("Members ---------------------")
print(pList2[0])
print(pList2[1])
print(pList2[2])
print(pList2[3])
print(pList2[3][0])
print(pList2[3][1])
In [5]:
# Methods operating on Lists
print("Methods ---------------------")
pList1.append(pInt)
print(pList1)
pList1.insert(1,pReal)
print(pList1)
pList1.remove(pReal)
print(pList1)
pList1.extend(pList2)
print(pList1)
In [6]:
# More Methods
print("More Methods ---------------------")
print(pList1.count("some Test String"), pList1.count(pInt))
print(pList1.index(pInt), pList1.index("some Test String"))
pList1.reverse()
print(pList1)
pList1.sort()
print(pList1)
In [7]:
# List as Stack
print("Stack ---------------------")
pStack1 = [12, 20, 7]
pStack1.append(45)
print(pStack1)
pPopped = pStack1.pop()
print(pPopped, pStack1)
In [8]:
# List as Q
print("Q ---------------------")
from collections import deque
pList3 = ["Ram", "Shyam", "Jadu"]
pQ = deque(["Ram", "Shyam", "Jadu"])
a = pQ.popleft()
print(a)
print(pQ)
pQ.append("Sita")
print(pQ)
In [9]:
# Sets
print("Sets ---------------------")
pList10 = ["Apple", "Orange", "Pear", "Mango", "Apple", "Orange", "Pineapple"]
print(pList10)
pSet = set(pList10)
print(pSet)
In [10]:
# Tuples
print("Tuples ---------------------")
pTuple1 = 1, 34, "king", "queen"
print(pTuple1)
print(pTuple1[0], pTuple1[3])
pTuple2 = 23, "new fellow", pTuple1
print(pTuple2)
In [11]:
# Dictionaries
print("Dictionaries ---------------------")
dTel = { "prithwis": 2066, "charan" : 8077, "govind" : 9234, "jaydeep" : 9324}
print(dTel)
print(dTel.keys())
print(dTel.values())
dTel['SDG'] = 9056
print(dTel)
dTel['prithwis'] = 6602
print(dTel)
print("prithwis" in dTel)
print("Prithwis" in dTel)
In [19]:
# Looping
print("Looping ..........................")
for key, val in dTel.items():
print (key, val)
In [15]:
print("Enumerate ..........................")
for ix, val in enumerate(pList1):
print (ix,val)
for ix, val in enumerate(pTuple1):
print (ix,val)
for ix, val in enumerate(dTel):
print (ix,val)
In [16]:
print("Zip ..........................")
teachers = ["prithwis","charan","jaydeep","subhasis"]
subjects = ["bigdata", "communications", "stats", "datamining"]
for t,s in zip(teachers,subjects):
print(t," teaches ",s)
In [18]:
# Membership
print(pInt in pList2)
print(2 in pList2)
print(33 not in pList2)
fruit1 = ["apple", "pear", "orange", "mango"]
fruit2 = ["apple", "grape", "banana","mango"]
print("pear" in fruit1)
print("pear" in fruit2)
print (set(fruit1).intersection(fruit2))
print (set(fruit1).union(fruit2))
In [ ]:
No comments:
Post a Comment