In: Computer Science
Task 4: Tuples
# STAY HOME # STAY SAFE
# a. lists, strings and tuples
# strings are basically a sequence of characters. They are
immutable i.e once declared they cannot be changed
s1 = "Hello how are you"
print("Example of string :",s1)
# lists are very useful in python, lists are mutable i.e. we can
change the items in a list very easily
# They can be homogenous as well as non-homgenous i.e they can
contain elements of different dayatypes
li = [11,22,"hey",20+30,29.99]
print("Example of list :",li)
# tuples can alse be homogenous as well as non-homgenous i.e
they can contain elements of different dayatypes
# but indexing in tuples cannot be done i.e they cannot be changed
and are immutable
tup = (30,40,"word",34.6)
print("Example of tuple :",tup)
#
b. lets check whether we can change item in a list (lists are
mutable so they can be changed)
# you won't get any error
li = [11,22,"hey",20+30,29.99]
li[2]=9999
print("List after changing an item :",li)
#
c. lets check whether we can change item in a string (strings are
immutable so they cannot be changed)
# you will get an error 'str' object does not support item
assignment
s1 = "Hello how are you"
s1[3]='G'
print("String after changing an item :",s1)
#
d. lets check whether we can change item in a tuple (tuples are
immutable so they cannot be changed)
# you will get an error 'tuple' object does not support item
assignment
tup = (30,40,"word",34.6)
tup[1]=99
print("Tuple after changing an item :",tup)