Saturday, October 14, 2017

Sequences in Python

BDSP_Sequences
In [1]:
#
# Praxis Business School 
# Prithwis Mukerjee
#
# Data Structures in Python
#

print("Hello to Data Structures")
Hello to Data Structures
In [2]:
pList1 = [1,"This is a list"]
print(pList1)
[1, 'This is a list']
In [3]:
pInt = 2
pReal = 2.318281828
pString = "some Test String"
pList2 = [ pInt, pReal, pString, pList1]
print(pList2)
[2, 2.318281828, 'some Test String', [1, 'This is a list']]
In [4]:
print("Members ---------------------")

print(pList2[0])
print(pList2[1])
print(pList2[2])
print(pList2[3])

print(pList2[3][0])
print(pList2[3][1])
Members ---------------------
2
2.318281828
some Test String
[1, 'This is a list']
1
This is a list
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)
Methods ---------------------
[1, 'This is a list', 2]
[1, 2.318281828, 'This is a list', 2]
[1, 'This is a list', 2]
[1, 'This is a list', 2, 2, 2.318281828, 'some Test String', [...]]
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)
More Methods ---------------------
1 2
2 5
[[...], 'some Test String', 2.318281828, 2, 2, 'This is a list', 1]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-f2cb7b6b5425> in <module>()
      7 pList1.reverse()
      8 print(pList1)
----> 9 pList1.sort()
     10 print(pList1)

TypeError: unorderable types: str() < list()
In [7]:
# List as Stack

print("Stack ---------------------")

pStack1 = [12, 20, 7]
pStack1.append(45)
print(pStack1)
pPopped = pStack1.pop()
print(pPopped, pStack1)
Stack ---------------------
[12, 20, 7, 45]
45 [12, 20, 7]
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)
Q ---------------------
Ram
deque(['Shyam', 'Jadu'])
deque(['Shyam', 'Jadu', 'Sita'])
In [9]:
# Sets

print("Sets ---------------------")

pList10 = ["Apple", "Orange", "Pear", "Mango", "Apple", "Orange", "Pineapple"]
print(pList10)

pSet = set(pList10)
print(pSet)
Sets ---------------------
['Apple', 'Orange', 'Pear', 'Mango', 'Apple', 'Orange', 'Pineapple']
{'Apple', 'Mango', 'Pineapple', 'Pear', 'Orange'}
In [10]:
# Tuples

print("Tuples ---------------------")

pTuple1 = 1, 34, "king", "queen"
print(pTuple1)
print(pTuple1[0], pTuple1[3])
pTuple2 = 23, "new fellow", pTuple1
print(pTuple2)
Tuples ---------------------
(1, 34, 'king', 'queen')
1 queen
(23, 'new fellow', (1, 34, 'king', 'queen'))
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)
Dictionaries ---------------------
{'charan': 8077, 'prithwis': 2066, 'jaydeep': 9324, 'govind': 9234}
dict_keys(['charan', 'prithwis', 'jaydeep', 'govind'])
dict_values([8077, 2066, 9324, 9234])
{'SDG': 9056, 'charan': 8077, 'prithwis': 2066, 'jaydeep': 9324, 'govind': 9234}
{'SDG': 9056, 'charan': 8077, 'prithwis': 6602, 'jaydeep': 9324, 'govind': 9234}
True
False
In [19]:
# Looping

print("Looping ..........................")

for key, val in dTel.items():
    print (key, val)
Looping ..........................
SDG 9056
charan 8077
prithwis 6602
jaydeep 9324
govind 9234
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)
Enumerate ..........................
0 [[...], 'some Test String', 2.318281828, 2, 2, 'This is a list', 1]
1 some Test String
2 2.318281828
3 2
4 2
5 This is a list
6 1
0 1
1 34
2 king
3 queen
0 SDG
1 charan
2 prithwis
3 jaydeep
4 govind
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)
Zip ..........................
prithwis  teaches  bigdata
charan  teaches  communications
jaydeep  teaches  stats
subhasis  teaches  datamining
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))
True
True
True
True
False
{'mango', 'apple'}
{'grape', 'apple', 'orange', 'pear', 'mango', 'banana'}
In [ ]:
 

No comments:

Post a Comment