Comparison BW Some of Python Datatypes
Comparison BW Some of Python Datatypes
By:
Pranjal Bansal
Lecturer, CSE
GWPC, Bharatpur (Raj.)
Overview
LIST TUPLE SET
[] () {}
mylist = mytuple = myset =
["apple", "banana", "cherry"] ("apple", "banana", "cherry") {"apple", "banana", "cherry"}
thislist = thistuple =
["apple", "banana", "cherr ("apple", "banana", "cherr
y", "orange", "kiwi", "mel y", "orange", "kiwi", "mel
on", "mango"] on", "mango")
print(thislist[1]) print(thistuple[1])
print(thislist[2:5]) print(thistuple[2:5])
print(thislist[:4]) print(thistuple[:4])
print(thislist[2:]) print(thistuple[2:])
print(thislist[-1]) print(thistuple[-1])
print(thislist[-4:-1]) print(thistuple[-4:-1])
Access Items (using while Loop)
LIST TUPLE SET
RANDOM
Change Items
LIST TUPLE SET
y[1] = "kiwi"
print(x)
Add Items
LIST TUPLE SET
a.append("orange") a.add("orange")
a.extend(b) a.update(b)
a.pop(1) a.pop(1)
del a del a
a.clear() a.clear()
Exercise
Write a Python code to create a list for given items and perform the following operations:
list items = 10, 20, 30, A, B, C
i) Display total number of items in the list.
ii) Display only 20.
iii) Display 30 to B.
iv) Display A to B using negative indexing.
v) Replace “B” by “WP”.
vi) Add “Bharat” to end of the list.
vii) Add “G” at index 4.
viii) Create another list and Concatenate both.
ix) Remove 20 from the list.
x) Remove last item from the list.
xi) Display all items using for loop. (in single line)
xii) Remove all items from the list.
Solution
vi) list1.append("Bharat") ix) list1.remove(20)
list1 = [10,20,30,"A","B","C"]
print(list1) print(list1)
[10, 20, 30, 'A', 'WP', 'C', 'Bharat'] [10, 30, 'A', 'G', 'WP', 'C', 'Bharat', 'P', 'U',
i) print("Total items in list = ",len(list1))
'R', 'A’]
Total items in list = 6
v) list1[4] = "WP"
xii) list1.clear()
print(list1)
print(list1)
[10, 20, 30, 'A', 'WP', 'C']
[]
Assignment
Write a Python code to create a Set for given values and perform the following operations:
Set items = 10, 20, 30, A, B, C
For Tuple:
https://www.w3schools.com/python/exercise.asp?filename=exercise_tuples1
For Set:
https://www.w3schools.com/python/exercise.asp?filename=exercise_sets1
Solution - Assignment
set1 = {10,20,30,"A","B","C"} v) list1 = [100,3.14,"$$$"] viii) for x in set1:
set1.update(list1) print(x,end=' ')
i) print(set1) print(set1) print()
{'C', 20, 'A', 'B', 10, 30} {'C', 3.14, 10, '$$$', 20, 3.14 10 $$$ 20 Bharatpur 30 100 A
'Bharatpur', 30, 100, 'A', 'B'}
ii) #print(set1[2]) ix) set1.clear()
TypeError: 'set' object is not subscriptable vi) set1.remove("B") print(set1)
print(set1) set()
iii) #set1[4] = "WP" {'C', 3.14, 10, '$$$', 20,
#print(set1) 'Bharatpur', 30, 100, 'A'}
TypeError: 'set' object is not subscriptable
vii) set1.pop()
iv) set1.add("Bharatpur") print(set1)
print(set1) {3.14, 10, '$$$', 20, 'Bharatpur',
{'C', 20, 'A', 'Bharatpur', 'B', 10, 30} 30, 100, 'A'}
Join
LIST TUPLE SET
list1 = ["a", "b", "c"] tuple1 = ("a", "b" , "c") set1 = {"a", "b" , "c"}
list2 = [1, 2, 3] tuple2 = (1, 2, 3) set2 = {1, 2, 3}
list3 = list1 + list2 tuple3 = tuple1 + tuple2 set3 = set1.union(set2)
print(list3) print(tuple3) print(set3)