Here is class 12 computer science Unit 2 [Type A] solutions for Sumita Arora back exercise assignment. Below includes both textual and video solutions wherever required. View all the answers in assignment for chapter 2 and for all chapters here.Watch all tutorials for chapter 2: Python Revision Tour.
Python strings are stored as individual unit and every unit is stored in contiguous(consecutive) location.
For e.g. name = ‘Abhi’. Here all four letter ‘A’, ‘b’, ‘h’, ‘i’ will stored individually under variable ‘name’.
string =input(‘Enter the string’)for i in range(0,len(string),2): print(string[i:i+2], end=’\n’)
In above code range will give increment of 2 and hence string concecutive 2 values will be pick for print, and ‘\n’ is for next line after each iteration.
Lists in python can store different type of data under a same name under continuous location, hence can be easily accessed. These are mutable. For e.g. fruit =[‘banana’, ‘apple’, ‘mangoes’, ‘grapes’], joint =[‘BA’, 1, 2, 3, ‘hello’]
In simple words it is just like a container having various different data under it.
Mutability means ability to change the values. For e.g. sets, lists, dict.
“In place” means changing on the same location or instance or in simple words updating the same location with new value. Such as changing an element of a list is ”in place”.
#code main = [8, 9, 10] (a) main.insert(1,17) >>>> [8, 17, 9, 10] (b)main.append(4) main.append(5) main.append(6) >>>> [8, 17, 9, 10, 4, 5, 6] (c) del main[0] >>>> [17, 9, 10, 4, 5, 6] (d) sorted(main) >>>>[4, 5, 6, 9, 10, 17] (e) main*2 >>>> [4, 5, 6, 9, 10, 17, 4, 5, 6, 9, 10, 17] (f) main.insert(3, 25) >>>> [4, 5, 6, 25, 9, 10, 17, 4, 5, 6, 9, 10, 17]
a[1:1] will always return an empty string as of its indicing.
Two methods are :- append, insert. Append will add the new item at the last of the list. Insert will add the new item at the desired position.
For e.g. f = [‘mango’, ‘apples’, ‘guava’] f.insert(2,’grapes’)output – [‘mango’, ‘apples’, ‘grapes’, ‘guava’]fruit.append(‘banana’)output – [‘mango’, ‘apples’, ‘guava’, ‘banana’]
‘del’ and ‘pop’ are the two ways to remove the item from list.
del will delete the item from the position or also through the slicing method.pop will always remove the last item from the list.
fruit = [‘mango’, ‘grapes’, ‘guava’, ‘banana’]fruit.pop()output – [‘mango’, ‘grapes’, ‘guava’]del fruit[1:]output – [‘mango’]
List is mutable while the tuple is non-mutable once created.List is comparative larger in memory size than tuple.
>>>> states = [] >>>> states.append('Delhi') >>>> states.append('Punjab') >>>> states2 =['Rajasthan', 'Gujrat', 'Kerala'] >>>> states.insert(0, 'Odisha') >>>> states.insert(2, 'Tripura') >>>> states2.insert(states2.index('Gujrat'), 'Haryana') >>>> states.pop(4)
Tuples are the immutable array data type that contain data of various type. Its immutable nature provides a specific and important role to have the same property through along the program and condition check.
(a) a*3 will increase the length i.e. size of tuple by replicating the element of tuple 3 times while (a,a,a) will create a nested tuple. >>> a*3 >>> (1, 2, 3, 1, 2, 3, 1, 2, 3) >>> (a,a,a) >>> ((1, 2, 3), (1, 2, 3), (1, 2, 3)) (b) Yes, it will behave in same way and resulting data type is also same. (c) It is the slicing operation on tuple but in a[1:1] resulting an empty tuple. (d) a[1:1] will give an empty tuple while a[1:2] will result to a non-empty tuple.
(30) is integer type value while (30,) is a tuple type.
Dictionary is unordered collection of objects because it stores data of various type reference to immutable key. Here only key and value relation is followed.
Immutable type of object can be used as key.Such as float, tuple, string etc.
The condition is that tuple must only have the immutable data elements in it only to satisfy the immutable property of the dictionary keys.
Yes, we can modify the value of respective keys in dictionary and also we can add or remove key value pair, but we can’t modify the existing keys in dictionary.
del D will delete the complete dictionary named D, and we can’t able to access it again while del D[<key>] will only delete the mentioned key and respective values of that key from D.
For the non-existing key an error will be generated named ‘KeyError: ‘. In assign to a non-existent key a new key value pair will be initialized.
Sorting is technique to arrange the element in either decreasing or increasing order.
Some techniques are:Bubble sortingInsertion sortingMerge Sorting etc.
Bubble sorting just compare the adjacent element and place them in their proper order of requirement.
Insertion sorting just traverse through the element and place that at the proper place according to the order.
Clear Doubts with Computer TutorIn case you’re facing problems in understanding concepts, writing programs, solving questions, want to learn fun facts | tips | tricks or absolutely anything around computer science, feel free to join CTs learner-teacher community: students.computertutor.in
You cannot copy content of this page