Ncert Solution Class Xi
Ncert Solution Class Xi
Question 1:
tuple1 = (23,1,45,67,45,9,55,45)
tuple2 = (100,200)
i. print(tuple1.index(45))
ii. print(tuple1.count(45))
iii. print(tuple1 + tuple2)
iv. print(len(tuple2))
v. print(max(tuple1))
vi. print(min(tuple1))
vii. print(sum(tuple2))
viii. print(sorted (tuple1))
print(tuple1)
ANSWER:
i. The 'index()' function returns the index of the first occurrence of the element in a tuple.
OUTPUT:
2
ii. The 'count()' function returns the numbers of times the given element appears in the tuple.
OUTPUT:
3
iv. The 'len()' function returns the number of elements in the given tuple.
OUTPUT:
2
vii. The 'sum()' function returns the sum of all the elements of the tuple.
OUTPUT:
300
viii. The 'sorted()' function takes element in the tuple and return a new sorted list. It doesn’t make any
changes to the original tuple. Hence, print(tuple1) will print the original tuple1 i.e. (23, 1, 45,
67, 45, 9, 55, 45)
OUTPUT:
[1, 9, 23, 45, 45, 45, 55, 67]
(23, 1, 45, 67, 45, 9, 55, 45)
Page No 224:
Question 2:
stateCapital =
{"AndhraPradesh":"Hyderabad","Bihar":"Patna","Maharashtra":"Mumbai",
"Rajasthan":"Jaipur"}
Find the output of the following statements:
i. print(stateCapital.get("Bihar"))
ii. print(stateCapital.keys())
iii. print(stateCapital.values())
iv. print(stateCapital.items())
v. print(len(stateCapital))
vi. print("Maharashtra" in stateCapital)
vii. print(stateCapital.get("Assam"))
viii. del stateCapital["Andhra Pradesh"]
print(stateCapital)
ANSWER:
Page No 224:
Question 3:
In Lists and Tuples, the items are retained in the order in which they are inserted. The elements can
always be accessed based on their position. The element at the position or ‘index’ 0 will always be at
index 0. Therefore, the Lists and Tuples are said to be ordered collections.
Page No 224:
Question 4:
With the help of an example show how can you return more than one value from a function.
ANSWER:
In tuple, more than one value can be returned from a function by ‘packing’ the values. The tuple can be
then ‘unpacked’ into the variables by using the same number of variables on the left-hand side as there
are elements in a tuple.
Example:
Program:
#Function to compute area and perimeter of the square.
def square(r):
area = r * r
perimeter = 4 * r
#Returns a tuple having two elements area and perimeter, i.e. two
variables are packed in a tuple
return (area, perimeter)
#end of function
OUTPUT:
Enter the side of the square: 4
Area of the square is: 16
The perimeter of the square is: 16
Page No 224:
Question 5:
Page No 224:
Question 6:
When to use tuple or dictionary in Python. Give some examples of programming situations mentioning
their usefulness.
ANSWER:
Tuples are used to store the data which is not intended to change during the course of execution of the
program. For example, if the name of months is needed in a program, then the same can be stored in the
tuple as generally, the names will either be iterated for a loop or referenced sometimes during the
execution of the program.
Dictionary is used to store associative data like student's roll no. and the student's name. Here, the roll
no. will act as a key to find the corresponding student's name. The position of the data doesn’t matter as
the data can easily be searched by using the corresponding key.
Page No 224:
Question 7:
Prove with the help of an example that the variable is rebuilt in case of immutable data types.
ANSWER:
When a variable is assigned to the immutable data type, the value of the variable cannot be changed in
place. Therefore, if we assign any other value to the variable, the interpreter creates a new memory
location for that value and then points the variable to the new memory location. This is the same process
in which we create a new variable. Thus, it can be said that the variable is rebuilt in case of immutable
data types on every assignment.
OUTPUT:
Before: 10916064
After: 10916096
It can be seen that the memory location a variable is pointing after the assignment is different. The
variable is entirely new and it can be said that the variable is rebuilt.
Page No 224:
Question 8:
TypeError occurs while statement 2 is running. Give reason. How can it be corrected?
ANSWER:
The ‘statement 1’ is creating a variable, tuple1 which is of ‘int’ data type. The ‘statement 2’ is checking
for the length of the variable, but the argument passed is an ‘int’ data type. The 'len()' function can
return the length only when the object is a sequence or a collection. This is the reason for the type error.
The error can be corrected by adding one comma after ‘5’ in statement 1, as this will create a tuple and as
a tuple is a collection, 'len()' function will not return an error. The correct statement will be:
>>> tuple1 = (5,)
>>> len(tuple1)
Page No 224:
Question 1:
Write a program to read email IDs of n number of students and store them in a tuple. Create two new
tuples, one to store only the usernames from the email IDs and second to store domain names from the
email ids. Print all three tuples at the end of the program. [Hint: You may use the function split()]
ANSWER:
Program:
#Program to read email id of n number of students. Store these numbers in a
tuple.
#Create two new tuples, one to store only the usernames from the email IDs and
second to store domain names from the email ids.
emails = tuple()
username = tuple()
domainname = tuple()
#Create empty tuple 'emails', 'username' and domain-name
n = int(input("How many email ids you want to enter?: "))
for i in range(0,n):
emid = input("> ")
#It will assign emailid entered by user to tuple 'emails'
emails = emails +(emid,)
#This will split the email id into two parts, username and domain and
return a list
spl = emid.split("@")
#assigning returned list first part to username and second part to domain
name
username = username + (spl[0],)
domainname = domainname + (spl[1],)
OUTPUT:
How many email ids you want to enter?: 3
> [email protected]
> [email protected]
> [email protected]
Page No 224:
Question 2:
Write a program to input names of n students and store them in a tuple. Also, input name from the user
and find if this student is present in the tuple or not.
We can accomplish these by:
(a) writing a user defined function
(b) using the built-in function
ANSWER:
a) Program:
#Program to input names of n students and store them in a tuple.
#Input a name from the user and find if this student is present in the tuple
or not.
#Using a user-defined function
#Creating user defined function
def searchStudent(tuple1,search):
for a in tuple1:
if(a == search):
print("The name",search,"is present in the tuple")
return
print("The name",search,"is not found in the tuple")
name = tuple()
#Create empty tuple 'name' to store the values
n = int(input("How many names do you want to enter?: "))
for i in range(0,n):
num = input("> ")
#It will assign emailid entered by user to tuple 'name'
name = name + (num,)
OUTPUT:
How many names do you want to enter?: 3
> Amit
> Sarthak
> Rajesh
b) Program:
#Program to input names of n students and store them in a tuple.
#Input a name from the user and find if this student is present in the tuple
or not.
#Using a built-in function
name = tuple()
#Create empty tuple 'name' to store the values
n = int(input("How many names do you want to enter?: "))
for i in range(0, n):
num = input("> ")
#it will assign emailid entered by user to tuple 'name'
name = name + (num,)
OUTPUT:
How many names do you want to enter?: 3
> Amit
> Sarthak
> Rajesh
Page No 225:
Question 3:
Program:
#Write a Python program to find the highest 2 values in a dictionary
#Defining a dictionary
dic = {"A":12,"B":13,"C":9,"D":89,"E":34,"F":17,"G":65,"H":36,"I":25,"J":11}
#Sorting the list in ascending order, it will store the highest value at the
last index
lst.sort()
#Printing the highest and second highest value using negative indexing of the
list
print("Highest value:",lst[-1])
print("Second highest value:",lst[-2])
OUTPUT:
Highest value: 89
Second highest value: 65
Page No 225:
Question 4:
Program:
#Count the number of times a character appears in a given string
st = input("Enter a string: ")
dic = {}
#creates an empty dictionary
for ch in st:
if ch in dic:
#if next character is already in the dictionary
dic[ch] += 1
else:
#if ch appears for the first time
dic[ch] = 1
OUTPUT:
Enter a string: meritnation
{'m': 1, 'e': 1, 'r': 1, 'i': 2, 't': 2, 'n': 2, 'a': 1, 'o': 1}
Page No 225:
Question 5:
Write a program to input your friends’ names and their Phone Numbers and store them in the dictionary
as the key-value pair. Perform the following operations on the dictionary:
a) Display the name and phone number of all your friends
b) Add a new key-value pair in this dictionary and display the modified dictionary
c) Delete a particular friend from the dictionary
d) Modify the phone number of an existing friend
e) Check if a friend is present in the dictionary or not
f) Display the dictionary in sorted order of names
ANSWER:
Program:
dic = {}
#Creates an empty dictionary
#Adding a contact
if(inp == 1):
name = input("Enter your friend name: ")
phonenumber = input("Enter your friend's contact number: ")
dic[name] = phonenumber
print("Contact Added \n\n")
#Modifying a contact if the entered name is present in the dictionary
elif(inp == 2):
name = input("Enter the name of friend whose number is to be modified:
")
if(name in dic):
phonenumber = input("Enter the new contact number: ")
dic[name] = phonenumber
print("Contact Modified\n\n")
else:
print("This friend's name is not present in the contact list")
#Deleting a contact if the entered name is present in the dictionary
elif(inp == 3):
name = input("Enter the name of friend whose contact is to be deleted:
")
if(name in dic):
del dic[name]
print("Contact Deleted\n\n")
else:
print("This friend's name is not present in the contact list")
#Displaying all entries in the dictionary
elif(inp == 4):
print("All entries in the contact")
for a in dic:
print(a,"\t\t",dic[a])
print("\n\n\n")
#Searching a friend name in the dictionary
elif(inp == 5):
name = input("Enter the name of friend to search: ")
if(name in dic):
print("The friend",name,"is present in the list\n\n")
else:
print("The friend",name,"is not present in the list\n\n")
#Displaying the dictionary in the sorted order of the names
elif(inp == 6):
print("Name\t\t\tContact Number")
for i in sorted(dic.keys()):
print(i,"\t\t\t",dic[i])
print("\n\n")
#Exit the while loop if user enters 7
elif(inp == 7):
break
#Displaying the invalid choice when any other values are entered
else:
print("Invalid Choice. Please try again\n")
OUTPUT: