Python data types
pythonStr = "Learning PySpark is fun"
pythonStr
pythonStr[9]
'P'
pythonStr[9]
pythonStr.find("Py"),pythonStr.find("py")
(9, -1)
pythonStr.startswith("Learning"),pythonStr.endswith("fun")
(True, True)
List
pythonList = [2.3,3.4,4.3,2.4,2.3,4.0]
pythonList1 = ["a",1]
pythonList1
type(pythonList1),type (pythonList1[0]),type (pythonList1[1])
(list, str, int)
pythonList = [2.3,3.4,4.3,2.4,2.3,4.0]
pythonList.sort()
print(pythonList)
pythonList.sort(reverse=True)
print(pythonList)
[2.3, 2.3, 2.4, 3.4, 4.0, 4.3]
[4.3, 4.0, 3.4, 2.4, 2.3, 2.3]
Tuple
pythonTuple = (2.0,9,"a",True,"a")
type(pythonTuple), pythonTuple[2]
(tuple, 'a')
print(pythonTuple.index('a'))
print(pythonTuple.count("a"))
2
2
Set
pythonSet = {'Book','Pen','NoteBook','Pencil','Book'}
pythonSet
{'Book', 'NoteBook', 'Pen', 'Pencil'}
pythonSet.add("Eraser")
pythonSet
{'Book', 'Eraser', 'NoteBook', 'Pen', 'Pencil'}
pythonSet1 = {'NoteBook','Pencil','Diary','Marker'}
pythonSet1
pythonSet.union(pythonSet1)
{'Book', 'Diary', 'Eraser', 'Marker', 'NoteBook', 'Pen', 'Pencil'}
pythonSet.intersection(pythonSet1)
{'NoteBook', 'Pencil'}
Dict
pythonDict = {'item1':'Pencil','item2':'Pen', 'item3':'NoteBook'}
pythonDict['item1'], pythonDict.get('item1'), pythonDict.get('item4')
('Pencil', 'Pencil', None)
Lambda function
isEvenInteger = lambda ourNum : ourNum%2 == 0
isEvenInteger(4),isEvenInteger(5)
(True, False)