Thursday 16 January 2020

CBSE Class 11 - Informatics Practices - Python Basics - List Manipulation (Part-2) - Question and Answers (#eduvictors)(#class11Python)

Python Basics - List Manipulation (Part-2) - Question and Answers

CBSE Class 11 - Informatics Practices

CBSE Class 11 - Informatics Practices - Python Basics - List Manipulation (Part-2) - Question and Answers (#eduvictors)(#class11Python)

Based on syllabus of Class 11 Informatics Practices, here are important Questions and Answers covered for the chapter List Manipulation.

The questions and answers will familiarise you with basics concepts of Python Lists and provide a framework for understanding the programming concepts.Software development isn’t any one thing, but a myriad of practices that coalesce into software in the end. Python has been a versatile language and is used by companies to write all sorts of applications.

List is an important data structure, part of a core Python. Lists have a variety of uses including data science, statistics and scientific computing. List is considered as a collection of objects and is ordered and changeable(mutable). It allows duplicate members.


Q1: What is the output of the following code?

list =['I','N','D','I','A']
print(list[0:3])
print(list[3:])
print(list[:])

Answer:
['I', 'N', 'D']
['I', 'A']
['I','N','D','I','A']

CBSE Class 11 - Informatics Practices - Python Basics - List Manipulation (Part-2) - Question and Answers (#eduvictors)(#class11Python)
Display Lists


Q2: What is the output of the following code? What does the '+' operator' do in the code snippet?

list1 = [1,2,3,4,5]
list2 = [5,4,3,2,1]
list3 = list1 + list2
print(list3)


Answer
[1,2,3,4,5,1,2,3,4,5,]

The + operator concatenates two lists and creates a new list.

CBSE Class 11 - Informatics Practices - Python Basics - List Manipulation (Part-2) - Question and Answers (#eduvictors)(#class11Python)
List Concatenation Using + operator


Q3: Name the operator used to replicate a Python list. Give an example.

Answer: * operator
e.g.
list1 = [1,2,3]
print(list1 * 3) # displays [1,2,3,1,2,3,1,2,3]


Python List Replicate CBSE Class 11 - Informatics Practices - Python Basics - List Manipulation (Part-2) - Question and Answers (#eduvictors)(#class11Python)
Python List Replicate



Q4: Name the method used to add a new item in list.

Answer: append() method is used to add an Item to a List.
e.g. 
list1 = [1,2,3]
print('List before append', list1)
list1.append(10)
print('List after append', list1)

Output is:
List before append [1,2,3]
List after append [1,2,3, 10]


CBSE Class 11 - Informatics Practices - Python Basics - List Manipulation (Part-2) - Question and Answers (#eduvictors)(#class11Python)
Python List append method



Q5: What is the output of the following Python code snippet? Give reasons why is the output of list1 and list2 same or different?
list1 = [1,2,3]
list2 = list1
print('List1 before append', list1)
print('List2 before append', list2)
list1.append(10)
print('List1 after append', list1)
print('List2 after append', list2)

Answer:  
List1 before append [1,2,3]
List2 before append [1,2,3]
List1 after append [1,2,3,10]
List2 after append [1,2,3,10]

Both variables points to same same list object in the memory. Any changes made using either reference will always change the same list object in the memory.


Q6: What is the function of extend( ) method?

Answer: extend() method is used to add multiple item at a time. It accepts only one argument that must be iterable type.

e.g. 
    mylist = ['one', 'two', 'three', 'four']
    list1 = ['five', 'six']
    mylist.extend(list1)
    print(mylist)
    mylist.extend([1,2])
    print(mylist)

Python List.append method CBSE Class 11 - Informatics Practices - Python Basics - List Manipulation (Part-2) - Question and Answers (#eduvictors)(#class11Python)
Python List extend method example


Q7: Name any four properties of Python lists.

Answer: Properties of Python lists are as follows:

∘ They are ordered.
∘ They contain objects of arbitrary types.
∘ The elements of a list can be accessed by an index.
∘ They are arbitrarily nestable, that is, they can contain other lists as sub-lists.
∘ They have variable sizes.
∘ They are mutable, that is, the elements of a list can be changed.


Q8: Name a keyword which is used to delete an item at a given index in a list.

Answer: The del keyword removes the specified index. i.e. del list[index]
e.g.
lst1 = [3,4,5,6]
del lst[2] // deletes 5 at position lst[2]


CBSE Class 11 - Informatics Practices - Python Basics - List Manipulation (Part-2) - Question and Answers (#eduvictors)(#class11Python)


Q9: Name the method that removes all the items in a list.

Answer: The list.clear() method removes all items from a list. An alternative to this method would be del a[:].


Q10: How can we check if an item is a member of a given list? Give an example.

Answer: We can use member operators i.e. in or not in to see if an item is in a list. The in operator let’s you find an item in a list and returns True if it does and False otherwise.

e.g. 
myHobbies = ['Reading', 'Basketball', 'Running', 'Football' ]
if "Singing" in myHobbies:
print("Not my hobby.")
else:
print("It is my hobby.")

Output:
It is my hobby 

Python Lists - In operator - CBSE Class 11 - Informatics Practices - Python Basics - List Manipulation (Part-2) - Question and Answers (#eduvictors)(#class11Python)
Python Lists - In operator


Q11: What is the difference between list.insert() method and list.append() method?

Answer: list.append() method adds an Item at end of a list.
list.insert() method inserts an Item at a defined index.

Python Lists - append vs insert CBSE Class 11 - Informatics Practices - Python Basics - List Manipulation (Part-2) - Question and Answers (#eduvictors)(#class11Python)
Python Lists - append vs insert


Q12: What is the output of the following code?
mylist = [30,14,15,70]
mylist.append(20)
print(mylist) 
mylist.insert(2, 33)
print(mylist)

Answer:
[30,14,15,70, 20]
[30,14,33,15,70, 20] 



Q13: What is the difference between pop() and del statement?

Answer: del statement removes an item or list-slice from the given list.
pop() removes only single element, not slices. It also returns removed item.



Q14: What is the output of the following code? Explain each pop() statement.
>>> list1 = [1,2,3,4,5]
>>> list1.pop()
>>> list1.pop(0)
>>> list1.pop(1)

Answer:
list1.pop() removes the last item. Now list1=[1,2,3,4]
list1.pop(0) removes the first item. Now list1 = [2,3,4]
list1.pop(1) removes the second item. Now list1 = [2,4]


Quote on Python Language From Happy Python User:
"Python is fast enough for our site and allows us to produce maintainable features in record times, with a minimum of developers," said Cuong Do, Software Architect, YouTube.com.


Q15: What is the purpose of list.index() method?

Answer: The index() method returns the index of first occurrence of the item from the list.
Syntax is : List.index(elem) 

e.g.
>>> list1 = [12, 13, 24,13,  45]
>>> list1.index(13)
1

It returns 1 the index value of the first matched value.


Q16: What's the output of the following?
>>> list1 = [12, 13, 24,13,  45]
>>> list1.index(60)

Answer: Since 60 is not present in the list, it will raise exception valueError.

CBSE Class 11 - Informatics Practices - Python Basics - List Manipulation (Part-2) - Question and Answers (#eduvictors)(#class11Python)
Value Exception  

 Q17: What is the function of List.count( ) method? Give an example.

Answer: List.count( ) method returns the number of times the given item value present in the list. If the item is not in the list, it returns 0.

 e.g.
>>> mylist = [1,2,3,4,8,2,7,2, 9]
>>> mylist.count(2)
3 # returns 3 as 2 occurs three times
>>> mylist.count(28)
0 # returns 0 as 28 does not exist in the list.



Q18: What is the purpose of List.reverse( ) method?  Give an example.

Answer: The reverse() method reverses the order of the elements in a list. 


CBSE Class 11 - Informatics Practices - Python Basics - List Manipulation (Part-2) - Question and Answers (#eduvictors)(#class11Python)
Python List reverse method


Q19: Write the functions of the following methods.
(a) min(list)
(b) max(list)
(c) len(list)
(d) list.sort()

Answer:
(a) min(list)
Returns item with minimum value in the list.

(b) max(list)
Returns item with maximum value in the list.

(c) len(list)
Return size (number of items) of the list.

(d) list.sort()
Sort the items of a list in ascending or descending order


Q20: What is the output of the following?
>>> list1 = ['m', 'o', 'n', 'd', 'a', 'y']
>>> list1. sort()
>>> list1

Answer: ['a', 'd', 'm', 'n', 'o', 'y']


Q21: Is a string same as a list of characters?

Answer: Strings and lists are similar but not same. String is a set or sequence of characters which can be accessed as index value, like in lists.

String is immutable i.e. you cannot change character value at any position, while list is mutable.


Q22: Can a string be converted into a list? Show an example.

Answer: Yes a string can be converted to a list and vice versa. 
e.g.
   str1 = 'Hello'
   list1 = list(str1) # coverts string into list of characters
   print(list1) # displays ['H', 'e', 'l', 'l', 'o']
   list2 = ['Welcome', 'To', 'Python', 'World']
   str2 = ' ' # str2 stores one space character
   str2 = str2.join(list2) # join concatenates the string using space character in str2 as separater.
   print(str2) # displays Welcome To Python World

Python Lists - String To List and vice Versa(cbse.eduvictors.com)
Python Lists - String To List and vice Versa


Q23: (i)consider the lists L1 and L2 answer the questions that follows
L1=[1,3,5] 
L2=[6,7,8]
i.print(L1+L2)
ii.print(L2*3)   

Answer
i. [7,10,13]
ii.[6,7,8, 6,7,8, 6,7,8] 


Q24: What will be the output?
numbers = [11, 12, 13, 14]
numbers.append([15,16,17,18])
print(len(numbers))

Answer: 5
Note numbers.append is adding entire list as one number i.e. numbers = [11, 12, 13, 14, [15,16,17,18]]



Q25: Write a program to display the sum and average of integers numbers given in a list. 
Say numList = [12,10, 8, 9, 19, 23, 28] 


Answer

#
#    Diplay sum and average of numbers in a list
#
numList = [12,10, 8, 9, 19, 23, 28] 
sumOfNums = sum(numList)
avg = sumOfNums/len(numList)
print('Sum of Numbers is: ', sumOfNums)
print('Average is: ', round(avg,2))

Compute average using Python Lists
Compute average using Python Lists


Q26: How we can sort the elements of the list.

Answer: Using List.sort( ) method


Q27: What is a sequence in Python? Is list a sequence?

Answer: A sequence is succession of values bound together by a single name. List is a type of sequence. Other sequence types supported by Python are:  strings, lists, tuples etc.


Q28: What is the function of range() method?

Answer: The range() function is a special type of list, for a given intger n, it generates a list of integers from 0 to n − 1.
Its syntax is: range(start, stop, step)
where,
start : Optional integer value to start with. Default is 0
stop : Required integer value specifying at which position to end.
step : Optional integer value specifying the incrementing value. Default is 1

Example:
list(range(5)) # makes a list of 10 integers from 0 to 4

list(range(3, 10)) # creates a list of integers from 3 to 9

list(range(0,10, 2)) # creates a list of integers from 0 to 10 with step value 2

list(range(5,0)) # creates an empty list

list(range(5,0,-1)) # creates a list of intgers from 5 to 1 decrementing by 1

Python range Method- CBSE Class 11 - Informatics Practices - Python Basics - List Manipulation (Part-2) - Question and Answers (#eduvictors)(#class11Python)


Q29: What is the difference between range() and list?

Answer: In Python 3.x the range( ) method generates a iteratable sequence. It does not make any list. Using
list(range(n)), one can generate a list of sequence generated by range() function.


Q30: Show an example to iterate a range(0, 5)

Answer: for - loop can be used to iterate range sequence values.

for x in range(0,5):
     print(x)

Iterate using range()
Iterate using range()

Q31: What is the output of the following?

for x in range(0,5):
     print(x+x, end='-')


Answer: 0-2-4-6-8-




No comments:

Post a Comment

We love to hear your thoughts about this post!

Note: only a member of this blog may post a comment.