Computer Science - Informatics Practices Class XI/XII Python List Solved Test Part 5
Q1. Index value of first element in list is ___
Q2. Index value of last element in list is ____
Q3. Write one difference between indexing and
slicing.
Q4. The syntax of slicing is:
List[start : stop : step]
Which argument is optional
out of start,
stop and step?
Q5. Write the output of the following:
b = "Informatics"
a=list(b)
1.
print(a)
2.
print(len(a))
3.
print(a[1:4])
4.
print(a[1:7])
5.
print(a[3:])
6.
print(a[:5])
7.
print(a[4:17])
8.
print(a[-2:-5:-1])
9.
print(a[1:7:1])
10. print(a[1:7:2])
11. print(a[-5:])
12. print(a[:4])
13. print(a[-2:-5:-2])
14. print(a[11:15])
15. print(a[:])
16. print(a[::2])
17. print(a[-5:-1])
18. print(a[7:1:-1])
19. print(a[3:-3])
20. print(a[30:40])
21. print(a[17:-1])
22. print(a[10::-1])
Q6. Write a code to make copy of following list using copy function.
a = [1,2,3,4]
Q7. What do you mean by append() function?
Q8. Write the output of the following:
code |
output |
>>> a =
[1,2,3,4] >>>
a.append(7) >>> a |
|
>>> a =
[1,2,3,4] >>>
a.append([7,8]) >>> a |
|
Solutions
Ans 1 : 0
Ans 2 : -1
Ans 3 : By indexing we can extract only one element of list while by
slicing we can fetch a sub list from main list.
Ans 4 : Step
Ans 5 :
1)
['I', 'n', 'f', 'o', 'r', 'm', 'a', 't', 'i', 'c', 's']
2)
11
3)
['n', 'f', 'o']
4)
['n', 'f', 'o', 'r', 'm', 'a']
5)
['o', 'r', 'm', 'a', 't', 'i', 'c', 's']
6)
['I', 'n', 'f', 'o', 'r']
7)
['r', 'm', 'a', 't', 'i', 'c', 's']
8)
['c', 'i', 't']
9)
['n', 'f', 'o', 'r', 'm', 'a']
10) ['n', 'o', 'm']
11) ['a', 't', 'i',
'c', 's']
12) ['I', 'n', 'f',
'o']
13) ['c', 't']
14) []
15) ['I', 'n', 'f',
'o', 'r', 'm', 'a', 't', 'i', 'c', 's']
16) ['I', 'f', 'r',
'a', 'i', 's']
17) ['a', 't', 'i',
'c']
18) ['t', 'a', 'm',
'r', 'o', 'f']
19) ['o', 'r', 'm',
'a', 't']
20) []
21) []
22) ['s', 'c', 'i',
't', 'a', 'm', 'r', 'o', 'f', 'n', 'I']
Ans 6.
>>> import copy
>>> a = [1,2,3,4]
>>> b=copy.copy(a)
Ans 7. Append function add one element
or a list at the end of the list.
Ans 8.
code |
output |
>>> a =
[1,2,3,4] >>>
a.append(7) >>> a |
[1,2,3,4,7] |
>>> a =
[1,2,3,4] >>>
a.append([7,8]) >>> a |
[1,2,3,4,[7,8]] |
No comments:
Post a Comment