You are on page 1of 4

1. What is the internal structure of Python strings?

String in python are stored as individual character in contiguous locations ,


with two – way index
For which each location,
Example =
0 1 2 3 4 5
P Y T H O N
- -
2. Q. Write a -6 5 4 -3 -2 -1 Python script that
traverses through an input string and prints its characters in different lines-
two characters per line.

a = input("Enter a string = ")


for i in range (0 , len(a)+1 , 2 ) :
print( a [ i - 2 : i])
Output :-
Enter a string = Path
Pa
th
>>>
Enter a string = Portal
Po
rt
al
>>>
3.Discuss the utility and significance of Lists, briefly.
List are the arrays. In python list play very important role. in python list is a type of
container of data which is used to store many type of data. they are mutable, so their
elements can be changed.
It is large in many size of in comparison of tuple.
4. What do you understand by mutability? What does "in place" memory
updation mean?
Mutability means that can be change.
Example:- list, sets, dictionary
"In place" memory updation mean that we can change elements of list.
5. Start with the list [3, 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
(a) l.insert(2,17)
(b) l.append(4,5,6)
(c) l.pop(0)
(d) l.sort()
(e) l.extend(l)
(f) l.insert(3,25)
6. What's a[1 : 1] if a is a string of at least two characters? And what if string is
shorter?
a[1:1] will print blank list.
Whenever if list is shorter, then it will print blank list.
7.What are the two ways to add something to a list? How are they different?
The two ways to add something to a list that are :-
1 = list . append (<item>)
2 = list . insert (<pos>,<item>)
Difference :-
list . append (<item>) add the value as last of list .
example =
>>>name = [“ram”, “shyam”, “sita”]
>>>name . append (“ravi”)
>>>name
>>>[“ram”, “shyam”, “sita”, “ravi”]
While list.insert (<pos>,<item>) add the value by providing index number .
Example =
>>>name = [“ram”, “shyam”, “sita”]
>>>name . insert (1,“ravi”)
>>>name
>>> [“ram”, “ravi” “shyam”, “sita”]
8.What are the two ways to remove something from a list? How are they
different?
del operator and pop method both are used to delete the value from list .
del is used to delete the value by slice while pop is used to delete value by particular
index number and also it return the deleted value.
9.What is the difference between a list and a tuple?
List is mutable while tuple is immutable in python.
List is large in memory size while tuple is short in memory size.
10.In the Python shell, do the following:
(i) Define a variable named states that is an empty list.
(ii) Add 'Delhi' to the list.
(ii) Now add 'Punjab' to the end of the list.
(iv) Define a variable states2 that is initialized with 'Rajasthan', 'Gujrať, and
'Kerala'.
(v) Add 'Odisha' to the beginning of the list.
(vi) Add ‘Tripura’ so that it is the third state in the list. (vii) Add 'Haryana' to
the list so that it appears before 'Gujrať. Do this as if you DO NOT KNOW where
'Gujrať is in the list.
Hint. See what states2.index("Rajasthan") does. What can you conclude about
listname.index(item) does ? what
(viii) Remove the 5th state from the list and print that state's name.
Answer =
1 = states = []
2 = states.append (“Delhi”)
3 = states.append (“Punjab”)
4 = states2 = ['Rajasthan', „Gujrat‟, 'Kerala']
5 = states2.insert (0, „Odisha‟)
6= states2.insert (2, „Tripura‟)
7 = states2.insert (3, 'Haryana')
8 = states2.pop(4)
11. Q. Discuss the utility and significance of Tuples, briefly.
It is a type of arrays . it play very important role in python . in python it is immutable
type of container which store any kind of data types it is short in memory size in
comparison of list .
12. Q. 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 between a[1:2] and a[1:1] ?
A = a*3 will print three times of tuple in a single tuple
>>>(1,2,3,1,2,3,1,2,3)
(a,a,a) will print nested tuple.
>>>((1,2,3),(1,2,3),(1,2,3))
B = yes
C = it mean that it will not give any e value because it is start from index number 1 but
stop at index number 1 but as we know that in slicing it is start from 1 and stop up at
1 so it will print empty tuple.
D = a[1:2] will print value at index number 1 while,
a[1:1] will print empty tuple.
13. What is the difference between (30) and (30,)?
When we use type function then (30) is type of 'int' class where (30,) is a type of tuple
which contain one element.
14.Why is a dictionary termed as an unordered collection of objects?
Dictionary termed as an unordered collection of objects because in dictionary we can use
any type of datatypes, but in key datatype must be immutable type.
15. What type of objects can be used as keys in dictionaries?
Immutable type of object can be used in dictionary.
Example = integer, string, tuple
16.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 to use tuples as a key in a dictionary that tuple must be contain
immutable datatype / element.
17. How is del D and del D[<key>] different from one another if D is a
dictionary?
del D delete the all dictionary so that when we want to find the value of D then it will
give error tat D is not defined.
Where del D[<key>] delete the particular key which is given by user.
18. . Create a dictionary named D with three entries, for keys 'a' ‘b’ and ‘c’.
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 a (e.g., D['d']='spam')?
It will give error . it will add as new key : value pair.
19What is sorting? Name some popular sorting techniques.
Arranging element of sequence in ascending or descending order is called sorting.
Some name of sorting techniques:-
1 = selection sort
2 = insertion sort
3 = bubble sort
4 = heap sort, e.t.c.
20. Q. Discuss Bubble sort and Insertion sort techniques.
Bubble sort:-
The basic idea of bubble sort is to compare two adjoining values and exchange them if
they are not in proper order .
Insertion sort :-
Each successive element is picked and inserted at an appropriate position in the
previously sorted array.

You might also like