[Type A] Chapter – 2 Class 12 CS – Sumita Arora Assignment | Q/A

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.

Q1: What is the internal structure of Python strings?

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’.

0123
‘A’”b’‘h’‘i’
-4-3-2-1
Addressing of string in python
Q2: Write a Python script that traverses through an input string and prints its characters in different lines – two characters per line.

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.

Q3: Discuss the utility and significance of Lists, briefly.

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.

Q4: What do you understand by mutability? What does “in place” task mean?

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”.

Q5: Start with the list [8, 9, 10]. Do the following :
(a) Set the second entry (index 1) to 17
(b) Add 4, 5 and 6 to the end of the list
(c) Remove the first entry from the list
(d) Sort the list
(e) Double the list
(f) Insert 25 at index 3
#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]

Q6: What is a[1:1] if a is a string of at least two characters? And what if a string is shorter?

a[1:1] will always return an empty string as of its indicing.

Q7: What are the two ways to add something to a list? How are they different?

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.

  • List_name.append(<item>)
  • List_name.insert(<pos>, <item>)

For e.g. f = [‘mango’, ‘apples’, ‘guava’]
f.insert(2,’grapes’)
output – [‘mango’, ‘apples’, ‘grapes’, ‘guava’]

fruit.append(‘banana’)
output – [‘mango’, ‘apples’, ‘guava’, ‘banana’]


Q8: What are the two ways to remove something from a list? How are they different?

‘del’ and ‘pop’ are the two ways to remove the item from list.

  • del [list_name[<pos>] ]
  • list_name.pop()

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’]

Q9: What is the difference between a list and a tuple?

List is mutable while the tuple is non-mutable once created.
List is comparative larger in memory size than tuple.

Q10: In the Python shell, do the following:
(i) Define a variable named states that is an empty list.
(ii) Add ‘Delhi’ to the list.
(iii) Now add ‘Punjab’ to the end of list.
(iv) Define a variable states2 that is initialized with ‘Rajasthan’, ‘Gujrat’ and ‘Kerala’.
(v) Add ‘Odisha’ to the beginning of the list.
(vi) Add ‘Tripura’ so that it is third state in list.
(vii) Add ‘Harayana’ to the list so that it appears before ‘Gujrat’. Do this as if you DO NOT KNOW where ‘Gujrat’ is in the list.
(viii) Remove the 5th state from the list and print that state’s name.
>>>> 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)
Q11: Discuss the utility and significance of Tuples, briefly.

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.

Q12: If a is (1,2,3)
(a) what is the difference (if any) between a*3 and (a,a,a)?
(b) is a*3 equivalent to a+a+a?
(c) what is the meaning of a[1:1]?
(d) what is the difference a[1:2] and a[1:1]

(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.
Q13: What is the difference between (30) and (30,)?

(30) is integer type value while (30,) is a tuple type.

Q14: Why is a dictionary termed as an unordered collection of objects?

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.

Q15: What type of objects can be used as keys in dictionaries?

Immutable type of object can be used as key.
Such as float, tuple, string etc.

Q16: Though tuples are immutable type, yet they cannot always be used as keys in a dictionary. What is the condition to use tuples as a key in a dictionary?

The condition is that tuple must only have the immutable data elements in it only to satisfy the immutable property of the dictionary keys.

Q17: Dictionary is a mutable type, which means you can modify its contents? What all is modifiable in a dictionary? Can you modify the keys of a dictionary?

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.

Q18: How is del D and del D[] different from one another if D is a 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.

Q19: Create a dictionary named D with three entries, for keys ‘a’, ‘b’ and V . What happens if you try to index a nonexistent key (D[‘d’])? What does Python do if you try to assign to a nonexistent key d (e.g., D[‘d’]=’ spam’)?

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.

Q20: What is sorting? Name some popular sorting techniques.

Sorting is technique to arrange the element in either decreasing or increasing order.

Some techniques are:
Bubble sorting
Insertion sorting
Merge Sorting etc.

Q21: Discuss Bubble sort and Insertion sort techniques.

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 Tutor
In 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