Worksheet for Python_class 11
Worksheet for Python_class 11
Worksheet for Python_class 11
1. State True or False: “In a Python loops can also have else clause”
Ans. True
Ans. (A)hAPPY#HOUR1122#33
7.
Ans. (a)[1,2]
11.
12. Write the Python statement for each of the following tasks using BUILT-IN
functions/methods only:
(i) To insert an element 400 at the fourth position, in the list L1.
(ii) To check whether a string named, message ends with a full stop/ period or
not.
Ans.
(i) L1.insert(3,400)
(ii) message.endswith('.')
20. Which of the following statement(s) would give an error after executing the
following code?
S="Welcome to class XII" #Statement 1
print(S) # Statement 2
S="Thank you" #Statement 3
S[0]= '@' #Statement 4
S=S+"Thank you" #Statement 5
(a) Statement 3 (b) Statement 4
(b) Statement 5 (d) Statement 4 and 5
Ans. (b) Statement 4,
as string’s individual element can’t assigned new value so S[0]= '@' # Statement
4 give error.
22. (i)Write the names of any two data types available in python.
(ii)Write any 2 operators name used in python.
Ans. i) Names of any two data types available in python: int, float or any other
valid datatype in python.
ii) Any 2 operators name used in python: Arithmetic, Logical, Relational or any other
valid operator in python.
23. Write the Python statement for each of the following tasks using BUILT_IN functions/
methods only:
i) str="PYTHON@LANGUAGE"
(A) To print the above string from index 2 onwards.
(B) To initialize an empty dictionary named as d.
Ans.
(i)A) str="PYTHON@LANGUAGE"
print(str[2: : ])
B) d=dict( )
ii) Write the Python statement for each of the following tasks using BUILT_IN
functions/ methods only:
(A) s=”LANGUAGE"
To convert the above string into list.
(B) To initialize an empty tuple named as t.
Ans.
(ii)A) s=”LANGUAGE"
l=list(s)
B) t=tuple( )
24. State True or False:
The keys of a dictionary must be of immutable types.
Ans. True
29. Which of the following options will not result in an error when performed on types in
python where tp = (5,2,7,0,3) ?
(a) Tp[1] = 2 (b) tp.append(2)
(c) tp1 = tp+tp (d) tp.sum( )
Ans. (c) tp1 = tp+tp
30. If my_dict is a dictionary as defined below, then which of the following statements
will raise an exception?
my_dict = {'aman': 10, 'sumit': 20, 'suresh': 30}
(a) my_dict.get('suresh') (b) print(my_dict['aman', 'sumit'])
(c) my_dict['aman']=20 (d) print(str(my_dict))
Ans. (b) print(my_dict['aman', 'sumit'])
31. Which of the following can delete an element from a list if the index of the element
is given?
(a) pop( ) (b) remove( )
(c) clear( ) (d) all of these
Ans. (a) pop( )
32. What are immutable and mutable types? List immutable and mutable types of
python.
Ans. Immutable types are those that can never change their value in place.
These are : integers, float, string, tuple
Mutable types are those whose values can be changed in place. These are : lists,
dictionaries
34. Write the most appropriate list method to perform the following tasks.
(I) A) To delete a given element from the list L1.
B) To sort the elements of list L1 in ascending order.
Ans.
(I) A) L1.remove(4)
B) L1.sort( )
35. Rewrite the following code in Python after removing all syntax error(s). Underline
each correction done in the code.
p=30
for c in range(0,p)
If c%4==0:
print (c*4)
Elseif c%5==0:
print (c+3)
else
print(c+10)
Ans.
p=30
for c in range(0,p):
if c%4==0:
print (c*4)
elif c%5==0:
print (c+3)
else:
print(c+10)
Ans : ( C ) 65
41. What will be the output of the following code snippet? text = "Python Programming"
print(text[1 : :3])
(A) Ph oai (B) yoPgmn (C) yhnPormig (D) Pto rgamn
Ans : (B)
Ans : C )
43. Dictionary my_dict as defined below, identify type of error raised by statement
my_dict['grape']?
my_dict = {'apple': 10, 'banana': 20, 'orange': 30}
ValueError (B) TypeError (C) KeyError (D) ValueError
Ans : (C) KeyError
b)The value of b will be [1, 2, 3, 4], as b references the same list as a, which was
modified by appending 4.
The value of d will be (5, 6, 7, 8), as the expression c + (8,) creates a new tuple
combining c and (8,).
46. Give examples for each of the following types of operators in Python:
(I) Assignment Operators
(II) Identity Operators
Ans :
(I) Assignment Operators:
1. Example 1: = (Simple Assignment) Usage: x = 5 (assigns the value 5 to x)
2. Example 2: += (Add and Assign) : Usage: x += 3 (equivalent to x = x + 3)
47. If L1 = [10, 20, 30, 40, 20, 10, ...] and L2 = [5, 15, 25, ...], then:
(Answer using builtin functions only)
(I) A) Write a statement to count the occurrences of 20 in L1. OR B) Write a
statement to find the minimum value in L1.
(II) A) Write a statement to extend L1 with all elements from L2. OR B) Write a
statement to get a new list that contains the unique elements from L1.
II (A) : L1.extend(L2)
(B) : unique_elements = list(set(L1))
Ans. 0*1*2*0*1*2*3*4*0*1*2*3*4*5*6*0*1
49. Predict the output of the following code:
numbers = [10, 15, 20]
for num in numbers:
for j in range(num // 5):
print(j, "+", end="")
print()
Ans.
0 +1 +
0 +1 +2 +
0 +1 +2 +3 +
51.
55. If farm is a t as defined below, then which of the following will cause an exception?
farm={‘goat’:5,’sheep’:35,’hen’:10,’pig’:7}
i) print(str(farm))
ii) print(farm[‘sheep’,’hen’])
iii) print(farm.get(‘goat))
iv)farm[‘pig’]=17
Ans. ii) print(farm[‘sheep’,’hen’])
56. Differentiate list and tuple with respect to mutability. Give suitable example to
illustrate the same .
Ans. List is Mutable and Tuple is not (Cannot update an element, insert and delete
elements)
# code to test that lists are mutable
List = [1, 2, 4, 4, 3, 3, 3, 6, 5]
print("Original list ", List)
List[3] = 77
print("Example to show mutability ", List)
Output:
Original list [1, 2, 4, 4, 3, 3, 3, 6, 5]
Example to show mutability [1, 2, 4, 77, 3, 3, 3, 6, 5]
Output:
TypeError: 'tuple' object does not support item assignment
62. Which of the following function will help in converting a string to list with elements
separated according to delimiter passed?
A) list( ) B) split( ) C) str( ) D) shuffle( )
Ans. B) split()
63. What is the output of the following?
OCEANS=('pacific','arctic','Atlantic','southern')
print(OCEANS[4])
A) ‘southern’ B) (‘southern’) C) Error D) INDEX
Ans. C) error
output ->
{1: 'Geeks', 2: 'For', 3: 'Geeks'}
68. Explain ‘in’ operator and write a small code in Python to show the use of ‘in’
operator.
Ans. The in operator returns True if the specified value or object is found in the
sequence and False otherwise.
Eg.
A="welcome"
print('e' in A)
print('z' in A)
output ->
True
False