0% found this document useful (0 votes)
15 views12 pages

Chapter 7 Advanced Python Programs

The document provides a series of Python programs that demonstrate various functionalities such as finding the second largest number in a list, sorting lists by element length, finding unions and intersections of two lists, and generating lists of tuples. It includes detailed problem descriptions, solutions, source code, explanations, and runtime test cases for each program. Each section outlines the steps taken to achieve the desired outcomes, showcasing practical applications of Python programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views12 pages

Chapter 7 Advanced Python Programs

The document provides a series of Python programs that demonstrate various functionalities such as finding the second largest number in a list, sorting lists by element length, finding unions and intersections of two lists, and generating lists of tuples. It includes detailed problem descriptions, solutions, source code, explanations, and runtime test cases for each program. Each section outlines the steps taken to achieve the desired outcomes, showcasing practical applications of Python programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Leela Soft 1000 MCQ Programs Madhu

4. The second last element of the sorted list is then printed which is basically the second
largest element in the list.

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)

Case 2:
Enter number of elements:6
Enter element:23
Enter element:45
Enter element:94
Enter element:10
Enter element:34
Enter element:95
('Second largest number is:', 94)

Python Program to Sort a List According to the Length of the Elements


Problem Description
The program takes a list and sorts the list according to the length of the elements.

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. Then sort the list giving the length of the elements as the key.
4. Display the sorted list.
5. Exit.

Program/Source Code
Here is source code of the Python Program to sort a list according to the length of the elements.
The program output is also shown below.

a=[]
n=int(input("Enter number of elements:"))
for i in range(1,n+1):

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

b=input("Enter element:")
a.append(b)
a.sort(key=len)
print(a)

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. Then the list is sorted using the length of the elements as the key.
4. The sorted list is then printed.

Runtime Test Cases

Case 1:
Enter number of elements:4
Enter element:"Apple"
Enter element:"Ball"
Enter element:"Cat"
Enter element:"Dog"
['Cat', 'Dog', 'Ball', 'Apple']

Case 2:
Enter number of elements:4
Enter element:"White"
Enter element:"Red"
Enter element:"Purple"
Enter element:"Orange"
['Red', 'White', 'Purple', 'Orange']

Python Program to Find the Union of two Lists


Problem Description
The program takes two lists and finds the unions of the two lists.

Problem Solution
1. Define a function which accepts two lists and returns the union of them.
2. Declare two empty lists and initialise to an empty list.
3. Consider a for loop to accept values for two lists.
4. Take the number of elements in the list and store it in a variable.
5. Accept the values into the list using another for loop and insert into the list.
6. Repeat 4 and 5 for the second list also.

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

7. Find the union of the two lists.


8. Print the union.
9. Exit.

Program/Source Code
Here is source code of the Python Program to find the union of two lists. The program output is
also shown below.

a=[]
n= int(input("Enter the number of elements in list:"))
for x in range(0,n):
element=int(input("Enter element" + str(x+1) + ":"))
a.append(element)
b=[sum(a[0:x+1]) for x in range(0,len(a))]
print("The original list is: ",a)
print("The new list is: ",b)

Program Explanation
1. User must enter the number of elements in the list and store it in a variable.
2. User must enter the values to the same number of elements into the list.
3. The append function obtains each element from the user and adds the same to the end
of the list as many times as the number of elements taken.
4. The same of 2 and 3 is done for the second list also.
5. The union function accepts two lists and returns the list which is the union of the two lists,
i.e, all the values from list 1 and 2 without redundancy.
6. The set function in the union function accepts a list and returns the list after elimination
of redundant values.
7. The lists are passed to the union function and the returned list is printed.

Runtime Test Cases

Case 1:
Enter the number of elements in list:5
Enter element1:10
Enter element2:10
Enter element3:20
Enter element4:20
Enter element5:20
Non-duplicate items:
[10, 20]

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Case 2:
Enter the number of elements in list:4
Enter element1:23
Enter element2:56
Enter element3:67
Enter element4:10
('The original list is: ', [23, 56, 67, 10])
('The new list is: ', [23, 79, 146, 156])

Python Program to Find the Intersection of Two Lists


Problem Description
The program takes two lists and finds the intersection of the two lists.

Problem Solution
1. Define a function which accepts two lists and returns the intersection of them.
2. Declare two empty lists and initialize them to an empty list.
3. Consider a for loop to accept values for the two lists.
4. Take the number of elements in the list and store it in a variable.
5. Accept the values into the list using another for loop and insert into the list.
6. Repeat 4 and 5 for the second list also.
7. Find the intersection of the two lists.
8. Print the intersection.
9. Exit.

Program/Source Code
Here is source code of the Python Program to find the intersection of two lists. The program
output is also shown below.

def intersection(a, b):


return list(set(a) & set(b))

def main():
alist=[]
blist=[]
n1=int(input("Enter number of elements for list1:"))
n2=int(input("Enter number of elements for list2:"))
print("For list1:")
for x in range(0,n1):
element=int(input("Enter element" + str(x+1) + ":"))
alist.append(element)
print("For list2:")

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

for x in range(0,n2):
element=int(input("Enter element" + str(x+1) + ":"))
blist.append(element)
print("The intersection is :")
print(intersection(alist, blist))
main()

Program Explanation
1. User must enter the number of elements in the list and store it in a variable.
2. User must enter the values to the same number of elements into the list.
3. The append function obtains each element from the user and adds the same to the end
of the list as many times as the number of elements taken.
4. The same of 2 and 3 is done for the second list also.
5. The intersection function accepts two lists and returns the list which is the intersection of
the two lists, i.e, all the common values from list 1 and 2.
6. The set function in the intersection function accepts a list and returns the list which
contains the common elements in both the lists.
7. The lists are passed to the intersection function and the returned list is printed.

Runtime Test Cases

Case 1:
Enter number of elements for list1:3
Enter number of elements for list2:4
For list1:
Enter element1:34
Enter element2:23
Enter element3:65
For list2:
Enter element1:33
Enter element2:65
Enter element3:23
Enter element4:86
The intersection is :
[65, 23]

Case 2:
Enter number of elements for list1:2
Enter number of elements for list2:4
For list1:
Enter element1:3

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Enter element2:4
For list2:
Enter element1:12
Enter element2:34
Enter element3:4
Enter element4:6
The intersection is :
[4]

Python Program to Create a List of Tuples with the First Element as the Number and Second
Element as the Square of the Number
Problem Description
The program takes a range and creates a list of tuples within that range with the first element as
the number and the second element as the square of the number.

Problem Solution
1. Take the upper and lower range for the numbers from the user.
2. A list of tuples must be created using list comprehension where the first element is the
number within the range and the second element is the square of the number.
3. This list of tuples is then printed.
4. Exit.

Program/Source Code
Here is source code of the Python Program to create a list of tuples with the first element as the
number and the second element as the square of the number. The program output is also shown
below.

l_range=int(input("Enter the lower range:"))


u_range=int(input("Enter the upper range:"))
a=[(x,x**2) for x in range(l_range,u_range+1)]
print(a)

Program Explanation
1. User must enter the upper and lower range for the numbers.
2. List comprehension must be used to create a list of tuples where the first number is the
number itself from the given range and the second element is a square of the first number.
3. The list of tuples which is created is printed.

Runtime Test Cases

Case 1:

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Enter the lower range:1


Enter the upper range:4
[(1, 1), (2, 4), (3, 9), (4, 16)]

Case 2:
Enter the lower range:45
Enter the upper range:49
[(45, 2025), (46, 2116), (47, 2209), (48, 2304), (49, 2401)]

Python Program to Find all Numbers in a Range which are Perfect Squares and Sum of all Digits
in the Number is Less than 10
Problem Description
The program takes a range and creates a list of all numbers in the range which are perfect squares
and the sum of the digits is less than 10.

Problem Solution
1. User must enter the upper and lower range for the numbers.
2. A list must be created using list comprehension where the element is a perfect square
within the range and the sum of the digits of the number is less than 10.
3. This list must then be printed.
4. Exit.

Program/Source Code
Here is source code of the Python Program to create a list of of all numbers in a range which are
perfect squares and the sum of the digits of the number is less than 10. The program output is
also shown below.

l=int(input("Enter lower range: "))


u=int(input("Enter upper range: "))
a=[]
a=[x for x in range(l,u+1) if (int(x**0.5))**2==x and sum(list(map(int,str(x))))<10]
print(a)

Program Explanation
1. User must enter the upper and lower range for the numbers.
2. List comprehension must be used to create a list of numbers which are perfect squares
and sum of the digits is less than 10.
3. The second part of the list comprehension first maps separate digits of the number into a
list and finds the sum of the elements in the list.
4. The list which is created is printed.

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Runtime Test Cases

Case 1:
Enter lower range: 1
Enter upper range: 40
[1, 4, 9, 16, 25, 36]

Case 2:
Enter lower range: 50
Enter upper range: 100
[81, 100]

Python Program to Find the Cumulative Sum of a List where the ith Element is the Sum of the
First i+1 Elements From The Original List
Problem Description
The program takes a list from the user and finds the cumulative sum of a list where the ith
element is the sum of the first i+1 elements from the original list.

Problem Solution
1. Declare an empty list and initialise to an empty list.
2. Consider a for loop to accept values for the list.
3. Take the number of elements in the list and store it in a variable.
4. Accept the values into the list using another for loop and insert into the list.
5. Using list comprehension and list slicing, find the cumulative sum of elements in the list.
6. Print the original list and the new list.
7. Exit.

Program/Source Code
Here is source code of the Python Program to find the cumulative sum of a list where the ith
element is the sum of the first i+1 elements from the original list. The program output is also
shown below.

a=[]
n= int(input("Enter the number of elements in list:"))
for x in range(0,n):
element=int(input("Enter element" + str(x+1) + ":"))
a.append(element)
b=[sum(a[0:x+1]) for x in range(0,len(a))]
print("The original list is: ",a)
print("The new list is: ",b)

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

Program Explanation
1. Use must declare a list and initialize it to an empty list.
2. Then a for loop is used to accept values for the list.
3. User must enter the number of elements in the list and store it in a variable.
4. Then the user must enter the values into the list using another for loop and insert into the
list.
5. List comprehension along with list slicing is used to find cumulative sum of elements in
the list
6. Then the original list and the new list is printed.

Runtime Test Cases

Case 1:
Enter the number of elements in list:5
Enter element1:1
Enter element2:2
Enter element3:3
Enter element4:4
Enter element5:5
('The original list is: ', [1, 2, 3, 4, 5])
('The new list is: ', [1, 3, 6, 10, 15])

Case 2:
Enter the number of elements in list:4
Enter element1:23
Enter element2:56
Enter element3:67
Enter element4:10
('The original list is: ', [23, 56, 67, 10])
('The new list is: ', [23, 79, 146, 156])

Python Program to Generate Random Numbers from 1 to 20 and Append Them to the List
Problem Description
The program takes in the number of elements and generates random numbers from 1 to 20 and
appends them to the list.

Problem Solution
1. Import the random module into the program.
2. Take the number of elements from the user.
3. Use a for loop, random.randint() is used to generate random numbers which are them
appending to a list.

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

4. Then print the randomised list.


5. Exit.

Program/Source Code
Here is source code of the Python Program to generate random numbers from 1 to 20 and
append them to the list. The program output is also shown below.

import random
a=[]
n=int(input("Enter number of elements:"))
for j in range(n):
a.append(random.randint(1,20))
print('Randomised list is: ',a)

Program Explanation
1. The random module is imported into the program.
2. User must enter the number of elements from the user.
3. Using a for loop, random.randint() is used to generate random numbers which are then
appended to a list.
4. The arguments given inside the random.randint() function are used to specify the range
to print the randomized numbers within.
5. Then the randomized list is printed.

Runtime Test Cases

Case 1:
Enter number of elements:3
('Randomised list is: ', [17, 4, 9])

Case 2:
Enter number of elements:7
('Randomised list is: ', [16, 5, 18, 19, 6, 7, 20])

Python program to Sort a List of Tuples in Increasing Order by the Last Element in Each Tuple
Problem Description
The program takes a list of tuples and sorts the list of tuples in increasing order by the last
element in each tuple.

Problem Solution
1. Take a list of tuples from the user.
2. Define a function which returns the last element of each tuple in the list of tuples.

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

3. Define another function with the previous function as the key and sort the list.
4. Print the sorted list.
5. Exit.

Program/Source Code
Here is source code of the Python Program to sort a list of tuples in increasing order by the last
element in each tuple. The program output is also shown below.

def last(n):
return n[-1]

def sort(tuples):
return sorted(tuples, key=last)

a=input("Enter a list of tuples:")


print("Sorted:")
print(sort(a))

Program Explanation
1. User must enter a list of tuples.
2. The function last returns the last element of each tuple in the list of tuples.
3. The function sort returns the sorted list with the last function as its key.
4. The sorted list is then printed.

Runtime Test Cases

Case 1:
Enter a list of tuples:[(2,5),(1,2),(4,4),(2,3)]
Sorted:
[(1, 2), (2, 3), (4, 4), (2, 5)]

Case 2:
Enter a list of tuples:[(23,45),(25,44),(89,40)]
Sorted:
[(89, 40), (25, 44), (23, 45)]

Python Program to Swap the First and Last Value of a List


Problem Description
The program takes a list and swaps the first and last value of the list.

Problem Solution

www.leelasoft.com Cell: 78 42 66 47 66
Leela Soft 1000 MCQ Programs Madhu

1. Take the number of elements in the list and store it in a variable.


2. Accept the values into the list using a for loop and insert them into the list.
3. Using a temporary variable, switch the first and last element in the list.
4. Print the newly formed list.
5. Exit.

Program/Source Code
Here is source code of the Python Program to swap the first and last value of a list. The program
output is also shown below.

a=[]
n= int(input("Enter the number of elements in list:"))
for x in range(0,n):
element=int(input("Enter element" + str(x+1) + ":"))
a.append(element)
temp=a[0]
a[0]=a[n-1]
a[n-1]=temp
print("New list is:")
print(a)

Program Explanation
1. User must enter the number of elements in the list and store it in a variable.
2. User must enter the values of elements into the list.
3. The append function obtains each element from the user and adds the same to the end
of the list as many times as the number of elements taken.
4. A temporary variable is used to swap the first and last element in the list.
5. The newly formed list is printed.

Runtime Test Cases

Case 1:
Enter the number of elements in list:4
Enter element1:23
Enter element2:45
Enter element3:67
Enter element4:89
New list is:
[89, 45, 67, 23]

Case 2:

www.leelasoft.com Cell: 78 42 66 47 66

You might also like