NCERT solutions for class 11 ip chapter 4 best explanation

In this article, we will discuss NCERT solutions for class 11 ip chapter 4 working with lists and dictionaries. So here we go!

NCERT solutions for class 11 ip chapter 4

[1] What will the output of the following statements:

a) list1 = [12,32,65,26,80,10]
list1.sort()
print(list1)

Ans.: [10,12,26,32,65,80]

Explanation:

Line 1: Creation of list with the list object list1 and values 12,32,65,26,80,10

Line 2: list1.sort() function sort all values in ascending order.

Line3: Finally the list is printed.

c) list1 = [1,2,3,4,5,6,7,8,9,10]
list1[::-2]
list1[:3] + list1[3:]

Ans.:

[10,8,6,4,2]

[1,2,3,4,5,6,7,8,9,10]

Explanation:

Line2:

List is sliced from the reverse and skipping alternate position with -2.

Line3:

As the first slice return, the first three elements, and the next slice returns all elements except the first 3 elements.

b) list1 = [12,32,65,26,80,10]
sorted(list1)
print(list1)

Ans.: [12,32,65,26,80,10]

Explanation:

Line 1: Creation of list with the list object list1 and values 12,32,65,26,80,10

Line 2: sorted(list1) function sort all valuesbut not modify the list.

Line3: Print the list1 as it is.

d) list1 = [1,2,3,4,5]
list1[len(list1)-1]

Ans.: 5

Explanation:

Line2:

The len function returns total number of elements from the list. So the len(list1) = 5-1= 4

Therefore list1[4] i.e. 5.

[2] Consider the following list myList. What will be the elements of myList after each of the following operations?

myList = [10,20,30,40]
a) myList.append([50,60])
b) myList.extend([80,90])

a) myList.append([50,60])

The append() function adds the given list at the end of the existing list. Therefore the output will be: [10,20,30,40,[50,60]]

b) myList.extend([80,90])

The extend() function also adds the elements in the existing list, it adds each element individually. Therefore the output will be: [10,20,30,40,[50,60],80,90]

[3] What will be the output of the following code segment?

myList = [1,2,3,4,5,6,7,8,9,10]
for i in range(0,len(myList)):
        if i%2 == 0:
              print(myList[i])

In this code the list is traversed by loop. The loop is started with 0 and continues upto the length of list i.e. 10.

Inside loop one if condition given which check the index values divisible by 2. Therefore the indexes are 0,2,4,6, and 8. Consider following table:

IndexValue
01
12
23
34
45
56
67
78
89
910

So the output will be:

1

3

5

7

9

[4] What will be the output of the following code segment?

(a)

myList = [1,2,3,4,5,6,7,8,9,10]
del myList[3:]
print(myList)

Ans.: [1,2,3]

Explanation: When a number is used in a slice before colon and nothing will be there followed by a colon, it means that the first n number of elements will be accessed from the list. Therefore the output will be [1,2,3].

(b)

myList = [1,2,3,4,5,6,7,8,9,10]
del myList[:5]
print(myList)

Ans.: [6,7,8,9,10]

Explanation: When a number is used in a slice after the colon it means that the last n number of elements will be accessed from the list. Therefore the output will be[6,7,8,9,10].

(c)

myList = [1,2,3,4,5,6,7,8,9,10]
del myList[::2]
print(myList)

Ans.: [2,4,6,8,10]

In slicing whenever two slices are coming it means that the number elements will be skipped and next value will be accessed. As 2 is given in slicing with del keyword. Therefore it will delete the values 1,3,5,7,9. In next statement, the remaining values are printed

[5] Differentiate between append() and extend() methods of list.

append()extend()
Append add one element to a list at a time.extend add multiple elements to a list at a time.
The multiple values can be added using a list as a parameter but append adds them as a list only.If user adds multiple values in form of list as parameter, it will add separate values.
ExampleExample
l1=[1,2,3]
l1.append([4,5])
print(l1)
The output will be: [1,2,3,[4,5]]
l1=[1,2,3]
l1.append([4,5])
print(l1)
The output will be: [1,2,3,4,5]

[6] Consider a list:
list1 = [6,7,8,9]
What is the difference between the following operations on list1:
a) list1 * 2
b) list1 *= 2
c) list1 = list1 * 2

a) The * operator treated as a replication operator when the list object is referred to with any numeric value. So here it will replicate the list values. Therefore it returns [6,7,8,9,6,7,8,9]

b) It is shorthand operator and as explain earlier it will also produce the similar result like question a).

c) It also produce the similar result as the statement is just like similar.

[7] The record of a student (Name, Roll No, Marks in five subjects and percentage of marks) is stored in the following list:
stRecord = [‘Raman’,’A-36′,[56,98,99,72,69],78.8]

Write Python statements to retrieve the following information from the list stRecord.
a) Percentage of the student

As percentage is given to the end of the list which is at the 3rd index. So the statement will be written in this manner:

perc=stRecord[3]
print(perc)

b) Marks in the fifth subject

As the marks are given at 2nd index and it is nested list with 5 subjects. So the marks list index will be 4th. So the statement will be something like this:

marks_sub5 = stRecord[2][4]
print(marks_sub5)

c) Maximum marks of the student

As we need to access marks only here and return the maximum value. To do this use max() function:

marks_max = max(stRecord[2])
print(marks_max)

d) Roll No. of the student

Access the index directly which is given at 1st index in the list.

rno = stRecord[1]
print(rno)

e) Change the name of the student from ‘Raman’ to ‘Raghav’

Access the index directly which is given at 0th index in the list.

sname = stRecord[0]
print(sname)

[8] Consider the following dictionary stateCapital:
stateCapital = {“Assam”:”Guwahati”, “Bihar”:”Patna”,”Maharashtra”:”Mumbai”, “Rajasthan”:”Jaipur”}

Find the output of the following statements:
a) print(stateCapital.get(“Bihar”))

The get() method is used to return the value from the dictionary key: value pair. So here the output will be – Patna


b) print(stateCapital.keys())

The keys() method will return dict_keys object with the list of keys from dictionary. So the output will be – dict_keys([‘Assam’, ‘Bihar’, ‘Maharashtra’, ‘Rajasthan’])


c) print(stateCapital.values())

The keys() method will return dict_values object with the list of values from dictionary. So the output will be – dict_values([‘Guwahati’, ‘Patna’, ‘Mumbai’, ‘Jaipur’])


d) print(stateCapital.items())

The items() method will returns the list of key and values in dict_items object. So the output will be – dict_items([(‘Assam’, ‘Guwahati’), (‘Bihar’, ‘Patna’), (‘Maharashtra’, ‘Mumbai’), (‘Rajasthan’, ‘Jaipur’)])


e) print(len(stateCapital))

It will return the length() of the dictionary. The output will be – 4


f) print(“Maharashtra” in stateCapital)

This statement will check that Maharashtra is available in dictionary object or not. If it is there it returns True otherwise False. So here it is present in the dictionary. So the output will be – True


g) print(stateCapital.get(“Assam”))

Refer the answer (a).


h) del stateCapital[“Assam”]
print(stateCapital)

It will delete information related to Assam. and print other keys and values.

{‘Bihar’: ‘Patna’, ‘Maharashtra’: ‘Mumbai’, ‘Rajasthan’: ‘Jaipur’}

Notes Creating a List

Programming Problems – ncert solutions for class 11 ip chapter 4

[1] Write a program to find the number of times an element occurs in the list.

By using loops

l = []
n = int(input("Enter the no. of elements in the list:"))
cnt=0
for i in range(0,n):
    lval=int(input("Enter the value to add in list:"))
    l.append(lval)
search_item=int(input("Enter the value to search:"))
for i in range(0,len(l)):
    if l[i]==search_item:
        cnt+=1
print(search_item, "Found in the list", cnt, " times")

With eval

lval = eval(input("Enter the values for the list:"))
search_item=int(input("Enter the value to search:"))
print(search_item, "Found in the list", lval.count(search_item), " times")

[2] Write a program to read a list of n integers (positive as well as negative). Create two new lists, one having all positive numbers and the other having all negative numbers from the given list. Print all three lists.

lval = eval(input("Enter the values for the list:"))
pl =[]
nl =[]
for i in range(len(lval)):
    if lval[i]>0:
        pl.append(lval[i])
    elif lval[i]<0:
        nl.append(lval[i])
print("Original List:", lval)
print("Poitive List:", pl)
print("Negative List:", nl)

[3] Write a program to find the largest and the second-largest elements in a given list of elements.

Do yourself.

[4] Write a program to read a list of n integers and find their median.
Note: The median value of a list of values is the middle one when they are arranged in order. If there are two middle values then take their average.
Hint: Use an inbuilt function to sort the list.

import statistics as s
lval = eval(input("Enter the values for the list:"))
m = s.median(lval)
print("The median value is:",m)
l = eval(input("Enter the values:"))
l.sort()
len1 =len(l)
mv = len1//2
if len1%2 ==0:
    m1, m2 = mv-1, mv
    md = (l[m1]+l[m2])/2
else:
    md=l[mv]
print("The median value is:",md)

[5] Write a program to read a list of elements. Modify this list so that it does not contain any duplicate elements i.e. all elements occurring multiple times in the list should appear only once.

l = eval(input("Enter the values:"))
temp=[]
for i in l:
    if i not in temp:
        temp.append(i)
l=list(temp)
print("List after removing duplicate elements:",l)

[6] Write a program to create a list of elements. Input an element from the user that has to be inserted in the list. Also input the position at which it is to be inserted.

l = [1,2,3]
list_value=int(input("Enter the value to insert:"))
p=int(input("Enter the position"))
idx = p - 1
l.insert(idx,list_value)
print(l)

[7] Write a program to read elements of a list and do the following.
a) The program should ask for the position of the element to be deleted from the list and delete the element at the desired position in the list.
b) The program should ask for the value of the element to be deleted from the list and delete this value from the list.

a) Delete By element position

l = [1,2,3]
p=int(input("Enter the position to delete:"))
l.pop(p)
print(l)

b) Delete by value

l = [1,2,3]
value=int(input("Enter the position to delete:"))
l.remove(value)
print(l)

[8] Write a Python program to find the highest 2 values in a dictionary.

d = {11:22,33:46,45:76}
s = sorted(d.values())
print(s[-1],s[-2])

[9] Write a Python program to create a dictionary from a string ‘w3resource’ such that each individual character mates a key and its index value for fist occurrence males the corresponding value in the dictionary.
Expected output : {‘3’: 1, ‘s’: 4, ‘r’: 2, ‘u’: 6, ‘w’: 0, ‘c’: 8, ‘e’: 3, ‘o’: 5}

Click here to see the answer

Access all contents of Computer Science Class 11

Thanks for visit.

Share this article NCERT solutions for class 11 ip chapter 4 with your friends and classmates.

Give your suggestions and feedback in comment section.

Comment Your Views

%d bloggers like this: