Leela Soft 1000 MCQ Programs Madhu
break
print(a, b, c)
m = m + 1
Program Explanation
1. User must enter the upper limit and store it in a variable.
2. The while and for loop is used to find the value of the Pythagorean triplets using the
formula.
3. If the value of a side is greater than the upper limit or if any of the side is 0, the loop is
broken out of.
4. Then the triplets are printed.
Runtime Test Cases
Case 1:
Enter upper limit:20
345
8 6 10
5 12 13
15 8 17
12 16 20
Case 2:
Enter upper limit:35
345
8 6 10
5 12 13
15 8 17
12 16 20
7 24 25
24 10 26
21 20 29
16 30 34
Python Program to Search the Number of Times a Particular Number Occurs in a List
Problem Description
The program takes a number and searches the number of times the particular number occurs in
a list.
Problem Solution
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
1. Take in the number of elements for the first list and store it in a variable.
2. Take in the elements of the list one by one.
3. Then take in the number to be searched in the list.
4. Use a for loop to traverse through the elements in the list and increment the count
variable.
5. Display the value of the count variable which contains the number of times a particular
number occurs in a list.
6. Exit.
Program/Source Code
Here is source code of the Python Program to search the number of times a particular number
occurs in a list. The program output is also shown below.
a = []
n = int(input("Enter number of elements:"))
for i in range(1, n + 1):
b = int(input("Enter element:"))
a.append(b)
k = 0
num = int(input("Enter the number to be counted:"))
for j in a:
if(j == num):
k = k + 1
print("Number of times", num, "appears is", k)
Program Explanation
1. User must enter the number of elements for the first list and store it in a variable.
2. User must then enter the elements of the list one by one using a for loop and store it in a
list.
3. User must also enter the number to be search the list for.
4. A for loop is used to traverse through the elements in the list and an if statement is used
to count a particular number.
5. The total count of a particular number in the list is printed.
Runtime Test Cases
Case 1:
Enter number of elements:4
Enter element:23
Enter element:45
Enter element:23
Enter element:67
Enter the number to be counted:23
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Number of times 23 appears is 2
Case 2:
Enter number of elements:7
Enter element:12
Enter element:45
Enter element:67
Enter element:45
Enter element:67
Enter element:67
Enter element:67
Enter the number to be counted:67
Number of times 67 appears is 4
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
3. Python Programming Examples on Lists
Python Program to Find the Largest Number in a List
Problem Description
The program takes a list and prints the largest number in the list.
Problem Solution
1. Take in the number of elements and store it in a variable.
2. Take in the elements of the list one by one.
3. Sort the list in ascending order.
4. Print the last element of the list.
5. Exit.
Program/Source Code
Here is source code of the Python Program to find the largest number in a list. The program
output is also shown below.
a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=int(input("Enter element:"))
a.append(b)
a.sort()
print("Largest element is:",a[n-1])
Program Explanation
1. User must enter the number of elements and store it in a variable.
2. User must then enter the elements of the list one by one using a for loop and store it in a
list.
3. The list should then be sorted.
4. Then the last element of the list is printed which is also the largest element of the list.
Runtime Test Cases
Case 1:
Enter number of elements:3
Enter element:23
Enter element:567
Enter element:3
Largest element is: 567
Case 2:
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Enter number of elements:4
Enter element:34
Enter element:56
Enter element:24
Enter element:54
Largest element is: 56
Python Program to Find the Second Largest Number in a List
Problem Description
The program takes a list and prints the second largest number in the list.
Problem Solution
1. Take in the number of elements and store it in a variable.
2. Take in the elements of the list one by one.
3. Sort the list in ascending order.
4. Print the second last element of the list.
5. Exit.
Program/Source Code
Here is source code of the Python Program to find the second largest number in a list. The
program output is also shown below.
a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=int(input("Enter element:"))
a.append(b)
a.sort()
print("Second largest element is:",a[n-2])
Program Explanation
1. User must enter the number of elements and store it in a variable.
2. User must then enter the elements of the list one by one using a for loop and store it in a
list.
3. The list should then be sorted.
4. Then the last element of the list is printed which is also the largest element of the list.
Runtime Test Cases
Case 1:
Enter number of elements:4
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Enter element:23
Enter element:56
Enter element:39
Enter element:11
Second largest element is: 39
Case 2:
Enter number of elements:3
Enter element:23
Enter element:4
Enter element:67
Second largest element is: 23
Python Program to Put Even and Odd elements in a List into Two Different Lists
Problem Description
The program takes a list and puts the even and odd elements in it into two separate lists.
Problem Solution
1. Take in the number of elements and store it in a variable.
2. Take in the elements of the list one by one.
3. Use a for loop to traverse through the elements of the list and an if statement to check if
the element is even or odd.
4. If the element is even, append it to a separate list and if it is odd, append it to a different
one.
5. Display the elements in both the lists.
6. Exit.
Program/Source Code
Here is source code of the Python Program to put the even and odd elements in a list into two
different lists. The program output is also shown below.
a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=int(input("Enter element:"))
a.append(b)
even=[]
odd=[]
for j in a:
if(j%2==0):
even.append(j)
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
else:
odd.append(j)
print("The even list",even)
print("The odd list",odd)
Program Explanation
1. User must enter the number of elements and store it in a variable.
2. User must then enter the elements of the list one by one using a for loop and store it in a
list.
3. Another for loop is used to traverse through the elements of the list.
4. The if statement checks if the element is even or odd and appends them to separate lists.
5. Both the lists are printed.
Runtime Test Cases
Case 1:
Enter number of elements:5
Enter element:67
Enter element:43
Enter element:44
Enter element:22
Enter element:455
The even list [44, 22]
The odd list [67, 43, 455]
Case 2:
Enter number of elements:3
Enter element:23
Enter element:44
Enter element:99
The even list [44]
The odd list [23, 99]
Python Program to Merge Two Lists and Sort it
Problem Description
The program takes two lists, merges them and sorts the merged list.
Problem Solution
1. Take in the number of elements for the first list and store it in a variable.
2. Take in the elements of the list one by one.
3. Similarly, take in the elements for the second list also.
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
4. Merge both the lists using the ‘+’ operator and then sort the list.
5. Display the elements in the sorted list.
6. Exit.
Program/Source Code
Here is source code of the Python Program to merge two lists and sort it. The program output is
also shown below.
a=[]
c=[]
n1=int(input("Enter number of elements:"))
for i in range(1,n1+1):
b=int(input("Enter element:"))
a.append(b)
n2=int(input("Enter number of elements:"))
for i in range(1,n2+1):
d=int(input("Enter element:"))
c.append(d)
new=a+c
new.sort()
print("Sorted list is:",new)
Program Explanation
1. User must enter the number of elements for the first list and store it in a variable.
2. User must then enter the elements of the list one by one using a for loop and store it in a
list.
3. User must similarly enter the elements of the second list one by one.
4. The ‘+’ operator is then used to merge both the lists.
5. The sort function then sorts the list in ascending order.
6. The sorted list is then printed.
Runtime Test Cases
Case 1:
Enter number of elements:4
Enter element:56
Enter element:43
Enter element:78
Enter element:12
('Second largest number is:', 56)
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
Case 2:
Enter the number of elements in list 1 : 0
Enter the number of elements in list 2 : 3
Enter element 1 : 12
Enter element 2 : 12
Enter element 3 : 12
The union is :
Python Program to Sort the List According to the Second Element in Sublist
Problem Description
The program takes a list of lists and sorts the list according to the second element in the sublist.
Problem Solution
1. Take in a list containing sublists.
2. Using two for loops, use bubble sort to sort the sublists based on the second value of the
sublist.
3. If the second element of the first sublist is greater than the second element of the second
sublist, exchange the entire sublist.
4. Print the sorted list.
5. Exit.
Program/Source Code
Here is source code of the Python Program to sort the list according to the second element in the
sublist. The program output is also shown below.
a=[['A',34],['B',21],['C',26]]
for i in range(0,len(a)):
for j in range(0,len(a)-i-1):
if(a[j][1]>a[j+1][1]):
temp=a[j]
a[j]=a[j+1]
a[j+1]=temp
print(a)
Program Explanation
1. User must enter a list containing several sublists.
2. Then bubble sort is implemented to sort the list according to the second element in the
sublist.
3. If the second element of the first sublist is greater than the second element of the second
sublist, then the entire sublist is switched.
www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu
4. This process continues till the entire list has been sorted.
5. The sorted list is then printed.
Runtime Test Cases
Case 1:
[['B', 21], ['C', 26], ['A', 34]]
Python Program to Find the Second Largest Number in a List Using Bubble Sort
Problem Description
The program takes a list and finds the second largest number in the list using bubble sort.
Problem Solution
1. Take in the number of elements for the list and store it in a variable.
2. Take in the elements of the list one by one.
3. Then sort the list using bubble sort.
4. Print the second last element of the sorted list which is the second largest number.
5. Exit.
Program/Source Code
Here is source code of the Python Program to find the second largest number in a list using bubble
sort. The program output is also shown below.
a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):
b=int(input("Enter element:"))
a.append(b)
for i in range(0,len(a)):
for j in range(0,len(a)-i-1):
if(a[j]>a[j+1]):
temp=a[j]
a[j]=a[j+1]
a[j+1]=temp
print('Second largest number is:',a[n-2])
Program Explanation
1. User must enter the number of elements for the list and store it in a variable.
2. User must then enter the elements of the list one by one using a for loop and store it in a
list.
3. Then bubble sort algorithm may be used to sort the elements of the list.
www.leelasoft.com Cell: 78 42 66 47 66