Computer Science with Python Practical Book Solutions class 11 Term -2 as per newest CBSE syllabus. These are the programs mentioned in the syllabus of term- 2 and for Practical file and annual Exam.

**Practical Notebook** : Computer Science (Python) Solutions Class 11 Term -2

**INDEX**

**1. **Find the largest/smallest number in a list/tuple**2.** Input a list of numbers and swap elements at the even location with the elements at the odd location.**3.** Input a list/tuple of elements, search for a given element in the list/tuple.**4.** Input a list of numbers and find the smallest and largest number from the list.**5**.Create a dictionary with the roll number, name and marks of n students in a class and display the names of students who have scored marks above 75.**6.** Repeatedly ask the user to enter a team name and how many games the team has won and how many they lost. Store this information in a dictionary where the keys are the team names and the values are a list of the form [wins, losses].**(i)** using the dictionary created above, allow the user to enter a team name and print out the team’s winning percentage.**(ii) **using dictionary create a list whose entries are the number of wins for each team.**(iii)** using the dictionary, create a list of all those teams that have winning records

**7.** Write a program that repeatedly asks the user to enter product names and prices. Store all of these in a dictionary whose keys are the product names and whose values are the price.**8.**When the user is done entering products and price, allow them to repeatedly enter a product name and print the corresponding price or a message if the product is not in dictionary. . **a**. A list consisting of the integers 0 through 49.**b**. A list consisting the square of the integer 1 through 50**c**. The list [“a”, “bb”, “ccc”, “dddd” ….] that ends with 26 copies of the letter z. **9.** Write a program as per following specifications:**(a)**

”’print the length of the longest

String in the list of a strings str_list.

Precondition: the list will contain

At least one element .”’**(b)**”’L is a list of numbers. return a new list where each element is the corresponding element of list L summed with number num.”’ **10.**Write a program as per following specification :

“‘ Return the length of the shortest string in the tuple of a strings str_tuple.

Precondition : the tuple will contain at least one element .’”

** Q.1. Find the largest/smallest number in a list/tuple **

**Code-**

lst = [] num = int(input('How many numbers: ')) for n in range(num): numbers = int(input('Enter number ')) lst.append(numbers) print("Maximum element in the list is :", max(lst), "\nMinimum element in the list is :", min(lst))

**output-**

**Q.2. Input a list of numbers and swap elements at the even location with the elements at the odd location. **

**Code-**

val=eval(input("Enter a list ")) print("Original List is:",val) s=len(val) if s%2!=0: s=s-1 for i in range(0,s,2): val[i],val[i+1]=val[i+1],val[i] print("List after swapping :",val)

**Output****–**

Enter a list [3,4,5,6]

Original List is: [3, 4, 5, 6]

List after swapping : [4, 3, 6, 5]

**Q.3. Input a list/tuple of elements, search for a given element in the list/tuple. **

**Code-**

# Python code to find the tuples containing # the given element from a list of tuples # List of tuples Input = [(14, 3),(23, 41),(33, 62),(1, 3),(3, 3)] # Find an element in list of tuples. Output = [item for item in Input if item[0] == 3 or item[1] == 3] # printing output print(Output)

**Output-**

[(14, 3), (1, 3), (3, 3)]

**Q.4.Input a list of numbers and find the smallest and** **largest number from the list**

**Code-**

# Python Program to find Largest and Smallest Number in a List NumList = [] Number = int(input("Please enter the Total Number of List Elements: ")) for i in range(1, Number + 1): value = int(input("Please enter the Value of %d Element : " %i)) NumList.append(value) print("The Smallest Element in this List is : ", min(NumList)) print("The Largest Element in this List is : ", max(NumList))

**Output-**

Please enter the Total Number of List Elements: 5 Please enter the Value of 1 Element : 50 Please enter the Value of 2 Element : 45 Please enter the Value of 3 Element : 33 Please enter the Value of 4 Element : 78 Please enter the Value of 5 Element : 66 The Smallest Element in this List is : 33 The Largest Element in this List is : 78

**Q.5 Create a dictionary with the roll number, name and marks of n students in a class and display the names of students who have scored marks above 75.**

**Code- **

n=int(int(input("Enter n: "))) d={} for i in range(n): roll_no=int(input("Enter roll no: ")) name=input("Enter name: ") marks=int(input("Enter marks: ")) d[roll_no]=[name,marks] for k in d: if(d[k][1]>75): print(d[k][0])

Output:Enter n: 2 Enter roll no: 1 Enter name: a Enter marks: 85 Enter roll no: 2 Enter name: b Enter marks: 55 a

**Q.6**. **Repeatedly ask the user to enter a team name and how many games the team has won and how many they lost. Store this information in a dictionary where the keys are the team names and the values are a list of the form [wins, losses].(i) using the dictionary created above, allow the user to enter a team name and print out the team’s winning percentage.(ii) using dictionary create a list whose entries are the number of wins for each team.(iii) using the dictionary, create a list of all those teams that have winning records.**

**Code-**

dic ={} lstwin = [] lstrec = [] while True : name = input ("Enter the name of team (enter q for quit)=") if name == "Q" or name == "q" : break else : win = int (input("Enter the no.of win match =")) loss = int(input("Enter the no.of loss match =")) dic [ name ] = [ win , loss ] lstwin += [ win ] if win > 0 : lstrec += [ name ] nam = input ("Enter the name of team those you are entered =") print ("Winning percentage =",dic [ nam ][0] *100 / (dic [nam ][0] + dic[nam ][1] )) print("wining list of all team = ",lstwin) print("team who has winning records are ",lstrec)

**Output-**

**Q.7.** **Write a program that repeatedly asks the user to enter product names and prices. Store all of these in a dictionary whose keys are the product names and whose values are the price.When the user is done entering products and price, allow them to repeatedly enter a product name and print the corresponding price or a message if the product is not in dictionary.**

**Code-**

dic = { } while True : product = input("enter the name of product (enter q for quit )= ") if product == "q" or product == "Q" : break else : price = int(input("enter the price of product = ")) dic [ product ] = price while True : name = input("enter the name of product those you are entered (enter q for quit )= ") if name == "q" or name == "Q" : break else : if name not in dic : print("name of product is invaild") else : print("price of product = ",dic[name])

**Output** –

**Q.8. Create the following lists using a for loop:a. A list consisting of the integers 0 through 49.b. A list consisting the square of the integer 1 through 50**

**c. The list [“a”, “bb”, “ccc”, “dddd” ….] that ends with 26 copies of the letter z.**

**Code-**

#(a) lst = [ ] for i in range(50): lst = lst + [ i ] print(lst) #(b): lst = [ ] for i in range(51): lst = lst + [ i**2 ] print(lst) #(c): lst = [ ] for i in range(1,27): lst = lst + [ chr(96+i) * i ] print(lst)

**Output-**

**Q.9. Write a program as per following specifications:****(a)”’print the length of the longestString in the list of a strings str_list.Precondition: the list will containAt least one element .”’(b)”’L is a list of numbers. return a new list where each element is the corresponding element of list L summed with number num.”’**

**Code-**

lst = eval(input("enter a list of string = ")) if lst == [] : print("list will contain at least one element ") else : long = "" for i in range(len(lst)) : if len(long ) < len(lst[i]) : long = lst[i] print("longest string in list = ",long) #(b) lst = eval(input("enter a list = ")) num = int(input("enter a number = ")) for i in range(len(lst)): lst[i] = lst[i] + num print("new list = ",lst)

**Output-**

**Q.10.Write a program as per following specification :**

“‘ Return the length of the shortest string in the tuple of a strings str_tuple.

Precondition : the tuple will contain at least one element .’”

**Code-**

tup = eval(input("enter the string tuple = ")) short = tup [ 0 ] for i in tup : if len(i) < len( short ) : short = i print("smllest string in tuple = ",short)

**Output –**

Thanks for reading Computer Science with Python Practical Book Solutions class 11 Term -2. 10 questions has been written according to 2021-2022 with syllabus of CBSE computer science with python.

Below are some useful link.

Computer Science Solution – Class 11 Archives – CS Study