Python Lists, Tuples and Sets

List, tuples and sets are some of the  data structures that are available on python.

List contains comma separated element that are changeable. List are enclosed in square brackets []

Tuples contains comma separated elements that are not changeable. Tuples elements are enclosed in parenthesis ()

Set contain elements that are unique. Set can be changed but uniqueness is maintained. Set elements are enclosed in braces{}

In the video below, I discussed these data structures:







Below are the codes I used for the demo:

		
	
import sys
print (sys.version)

print ("************ List ************")
print("List is a collection of comma separated values which are changeable")
print ("List allows duplicate members. It is enclosed in a square bracket")
list1=[1,2,3,4]
print(list1)
print()

print ("************ Tuple ************")
print("Tuple is a collection of comma separated values which is not changeable")
print ("Tuple allows duplicate members. It is enclosed in a parenthesis")
tuple1=(4,5,6,7)
print (tuple1)
print()

print ("************Sets ************")
print("Sets is a collection of comma separated values which are changeable and unique")
print ("Set does allows duplicate members. It is enclosed in a brackets {}")
set1={8,9,10,11}
print (set1)
print()

print ("************use of index************")
print("you cn make use of index on list and tuple. set doe not support indexing")
print ("to get the element of the index make use of the systax print (list1[3])")
print ("the index is zero based which means the first element is list1[0]")
print (list1[0])
print (tuple1[0])
#print (set1[0])  ## this will cause an error


print ("************Slicing list and tuples************")
print("you can extract data from list and tuples using slicing")
print ("the format is list1[0:3] ")
print (list1[0:3])
print (tuple1[0:3])

print ("************Slicing List and tuples with step interval************")
print("you can make use of steps when slicing your data")
print ("the format is list1[0:3:2] ")
print (list1[0:3:2])
print (tuple1[0:3:2])

print ("************Changing the value of list using index************")
print("you can change the value in the list using index. you cannot modify the value in tuples")
print ("the format is list1[0]=9 ")
list1[0]=9 
#tupple1[0]=9 #this will cause an error
print (list1[0:])

print ("************creating a big list with range ********************")
list2=list(range(0,150))
tuple2=tuple(range(0,100,5))
print("listing 0 to 150")
print (list2)
print("tuples 0 to 100 step 5")
print (tuple2)

print ("************Getting last element with negative index********************")
print (list2[-1])
print (tuple2[-1])


print ("************slicing with negative index*******************")
print (list2[:-1])
print (tuple2[:-1])

print ("************last five element ******************")
print (list2[-6:-1])
print (tuple2[-6:-1])


set2=set(range(1,5))
print (set2)

list2=list(range(0,150))
tuple2=tuple(range(0,100,5))

print ("************add element to list and set ****************")
list2.append(170)
print (list2)
set2.add(20)
print (set2)

print ("************Removing from the list and set ****************")
list2.remove(170)
print (list2)
set2.remove(20)
print (set2)

print ("************Removing from the list and set with discard ****************")
#list2.discard(170) 'there is no discard method in the list
print (list2)
set2.discard(25)
print (set2)

print ("************ Reversing****************")
list3=list(range(1,20))
print(list3)
list3.reverse()
print (list3)

print ("************sum and average****************")
list4=list(range(1,20))
print (list4)
print("sum of the list",sum(list4))
print("average of the list",sum(list4)/len(list4))
print("minimum value", min(list4))
print("maximum value", max(list4))

print("********set operation *********************")
set3=set(range(1,10))
print(set3)
set4=set(range(5,15))
print (set4)
print("****intersection****")
print(set3.intersection(set4))
print("****union****")
print(set3.union(set4))
print("****difference****")
print(set3.difference(set4))

print("repition")
list4=list(range(1,5))*3
print(list4)

		

Leave a Reply

Your email address will not be published. Required fields are marked *