0% found this document useful (0 votes)
13 views

Comparison BW Some of Python Datatypes

Uploaded by

js8055470
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Comparison BW Some of Python Datatypes

Uploaded by

js8055470
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Python Data Types

Lists v/s Tuples v/s Sets

By:
Pranjal Bansal
Lecturer, CSE
GWPC, Bharatpur (Raj.)
Overview
LIST TUPLE SET

➢ Ordered ➢ Ordered ➢ UnOrdered


➢ Changeable ➢ UnChangeable ➢ UnChangeable* (Remove/Add)
➢ Allow Duplicates ➢ Allow Duplicates ➢ No Duplicates
➢ Indexed (0 to n-1) ➢ Indexed (0 to n-1) ➢ UnIndexed

[] () {}
mylist = mytuple = myset =
["apple", "banana", "cherry"] ("apple", "banana", "cherry") {"apple", "banana", "cherry"}

print(mylist) print(mytuple) print(myset) RANDOM

len() len() len()


print(len(mylist)) print(len(mytuple)) print(len(myset))

type() type() type()


print(type(mylist)) print(type(mytuple)) print(type(myset))
<class 'list'> <class 'tuple’> <class 'set’>
Access Items
LIST TUPLE SET

[0]/[-1]/range [2:5] [0]/[-1]/range [2:5] Unindexed

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

thislist = thistuple = Unindexed


["apple", "banana", "cherry"] ("apple", "banana", "cherry")
i = 0 i = 0
while i < len(thislist): while i < len(thistuple):
print(thislist[i]) print(thistuple[i])
i = i + 1 i = i + 1
Access Items (using for Loop)
LIST TUPLE SET

thislist = thistuple = thisset =


["apple", "banana", "cherry"] ("apple", "banana", "cherry") {"apple", "banana", "cherry"}
for x in thislist: for x in thistuple: for x in thisset:
print(x) print(x) print(x)

RANDOM
Change Items
LIST TUPLE SET

thislist = UnChangeable UnChangeable


["apple", "banana", "cherry"] But (Add &Remove Allow)
Tuple -> List -> Tuple
thislist[1] = "kiwi"
Print(thislist) x =
("apple", "banana", "cherry")

y = list(x) [Tuple -> List]

y[1] = "kiwi"

x = tuple(y) [List -> Tuple]

print(x)
Add Items
LIST TUPLE SET

a = UnChangeable UnChangeable* (Can Add)


["apple", "banana", "cherry"]
b = Tuple -> List -> Tuple a =
["mango", "pineapple", "papay {"apple", "banana", "cherry“}
a"] thistuple = b =
("apple", "banana", "cherry") {"mango", "pineapple", "papay
y = list(thistuple) a“}
y.append("orange")
thistuple = tuple(y) RANDOM

a.append("orange") a.add("orange")

a.insert(1, "orange") a.insert(1, "orange")

a.extend(b) a.update(b)

Extend -> Any iterable Update -> Any iterable


Remove Items
LIST TUPLE SET

a = UnChangeable UnChangeable* (Can Remove)


["apple", "banana", "cherry"]
Tuple -> List -> Tuple a =
{"apple", "banana", "cherry“}
thistuple =
("apple", "banana", "cherry")
y = list(thistuple)
y.remove("apple")
a.remove(“banana") thistuple = tuple(y) thisset.remove("banana")
thisset.discard("banana")

a.pop(1) a.pop(1)

a.pop() (TOP) a.pop() (RANDOM)

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

vii) list1.insert(4,"G") x) list1.pop()


ii) print(list1[1])
print(list1) print(list1)
20
[10, 20, 30, 'A', 'G', 'WP', 'C', 'Bharat’] [10, 30, 'A', 'G', 'WP', 'C', 'Bharat', 'P', 'U',
'R']
iii) print(list1[2:5])
[30, 'A', 'B'] viii) list2 = ["P","U","R","A"]
list1.extend(list2) xi) for x in list1:
print(list1) print(x, end=' ')
iv) print(list1[-3:-1])
[10, 20, 30, 'A', 'G', 'WP', 'C', 'Bharat', print()
['A', 'B']
'P', 'U', 'R', 'A'] 10 30 A G WP C Bharat P U R

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

i) Display all values of the Set.


ii) Display only 30.
iii) Replace “B” by “WP”.
iv) Add “Bharatpur” to the set.
v) Create a list and add to this set.
vi) Remove “B” from the set.
vii) Remove one item from the set.
viii) Display all items using for loop. (in single line)
ix) Remove all items from the set.
More Exercise
For List:
https://www.w3schools.com/python/exercise.asp?filename=exercise_lists1

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)

list1 = ["a", "b" , "c"] set1 = {"a", "b" , "c"}


list2 = [1, 2, 3] set2 = {1, 2, 3}
list1.extend(list2) set1.update(set2)
print(list1) print(set1)

Allow Duplicate Allow Duplicate No Duplicate


Join (SET)
Join Two Sets Keep ONLY the Duplicates Keep All, But NOT the
Duplicates
set1 = {"a", "b" , "c"} x = x =
set2 = {1, 2, 3} {"apple", "banana", "cherry"} {"apple", "banana", "cherry"}
set3 = set1.union(set2) y = y =
print(set3) {"google", "microsoft", "apple"} {"google", "microsoft", "apple"}
z = x.intersection(y) z = x.symmetric_difference(y)
print(z) print(z)

set1 = {"a", "b" , "c"} x = x =


set2 = {1, 2, 3} {"apple", "banana", "cherry"} {"apple", "banana", "cherry"}
set1.update(set2) y = y =
print(set1) {"google", "microsoft", "apple"} {"google", "microsoft", "apple"}
x.intersection_update(y) x.symmetric_difference_update(y)
print(x) print(x)

You might also like