Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
119 views

Python - Sample Answersheet

The document discusses Python programs to perform various operations on lists: 1) Find the largest number in a list using sorting, max() function, and finding the max of user inputs. 2) Find the second largest number in a list using sorting and an O(n) optimal method. 3) Find the smallest number in a list using sorting and min() function. 4) Separate even and odd elements of a list into two lists. 5) Merge and sort two lists. 6) Create a list of tuples from a list where each tuple contains a number and its cube.

Uploaded by

Kusal Saraf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views

Python - Sample Answersheet

The document discusses Python programs to perform various operations on lists: 1) Find the largest number in a list using sorting, max() function, and finding the max of user inputs. 2) Find the second largest number in a list using sorting and an O(n) optimal method. 3) Find the smallest number in a list using sorting and min() function. 4) Separate even and odd elements of a list into two lists. 5) Merge and sort two lists. 6) Create a list of tuples from a list where each tuple contains a number and its cube.

Uploaded by

Kusal Saraf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

IT Workshop Lab (Python)

(PCC-CS393)
CSE – 3rd Sem. – 2nd Year

31.Write a Program in Python to find the Largest number in a list by three methods.
(a)sort the list in ascending order and print the last element in list.

Program

n=list(map(int,input("Enter three numbers ").split()))


n.sort()
print('The largst number is',n[-1])

OUTPUT
In [1]: runfile('D:/Python Programs/Prog2.py', wdir='D:/Python Programs')

Enter three numbers 10 20 30

The largst number is 30

(b) Using max() method.

Program

n=list(map(int,input("Enter three numbers ").split()))


print('The largst number is',max(n))

OUTPUT
In [1]: runfile('D:/Python Programs/Prog2.py', wdir='D:/Python Programs')
Enter three numbers 20 77 43
The largst number is 77

(c) Find max list element on inputs provided by user.


Program

n=list(map(int,input("Enter three numbers ").split()))


print('The largst number is',max(n))

OUTPUT
In [1]: runfile('D:/Python Programs/Prog2.py', wdir='D:/Python Programs')

Enter three numbers 78 99 65

The largest number is 99

Assignment No. – VII Page - 1 KUSAL SARAF, 18/CSE/124, </> CSE Dept, FIEM
IT Workshop Lab (Python)
(PCC-CS393)
CSE – 3rd Sem. – 2nd Year

32. Python Program to Find the Second Largest Number in a List using the following methods.
a) With inputs provided by the user and sort() function
Program

n=list(map(int,input("Enter two numbers ").split()))


print('The largst number is',max(n))

OUTPUT

In [1]: runfile('D:/Python Programs/Prog2.py', wdir='D:/Python Programs')


Enter two numbers 20 30
The largest number is 30

b) An optimal method having O(n) time complexity Program

list1=list(map(int,input("Enter two numbers ").split()))


max=max(list1[0],list1[1])
secondmax=min(list1[0],list1[1])

for i in range(2,len(list1)):
if list1[i]>max:
secondmax=max
max=list1[i]
else:
if list1[i]>secondmax:
secondmax=list1[i]
print("Second highest number is :",str(secondmax))

OUTPUT

In [1]: runfile('D:/Python Programs/Prog2.py', wdir='D:/Python Programs')

Enter two numbers 40 22

Second highest number is : 22

33. Python Program to Find the Smallest Number in a List using the following methods.
a) Using sort() method

Program

n=list(map(int,input().split()))
n.sort()
print(n[0])

Assignment No. – VII Page - 2 KUSAL SARAF, 18/CSE/124, </> CSE Dept, FIEM
IT Workshop Lab (Python)
(PCC-CS393)
CSE – 3rd Sem. – 2nd Year

OUTPUT

In [1]: runfile('D:/Python Programs/Prog2.py', wdir='D:/Python Programs')

Enter two number 35 55

3
b) Using min() method
Program

n=list(map(int,input('Enter two number').split()))


print(min(n))

OUTPUT

In [1]: runfile('D:/Python Programs/Prog2.py', wdir='D:/Python Programs')

Enter two number43 22

22
34. Python Program to Put Even and Odd elements of a List into Two Different Lists.
Program

n=list(map(int,input('Enter number').split()))
even=[]
odd=[]
for x in n:
if x%2==0:
even.append(x)
else:
odd.append(x)
print(even)
print(odd)

OUTPUT
In [1]: runfile('D:/Python Programs/Prog2.py', wdir='D:/Python Programs')
Enter number10 15 20 25 30 35 40 45 50
[10, 20, 30, 40, 50]
[15, 25, 35, 45]

35. Python Program to Merge Two Lists and Sort it.


Program

Assignment No. – VII Page - 3 KUSAL SARAF, 18/CSE/124, </> CSE Dept, FIEM
IT Workshop Lab (Python)
(PCC-CS393)
CSE – 3rd Sem. – 2nd Year

l1=[66,32,55,90,11,5]
l2=[78,89,23]
l3=[]
l3=l1+l2
l3.sort()
print(l3)
OUTPUT
In [1]: runfile('D:/Python Programs/Prog2.py', wdir='D:/Python Programs')
[5, 11, 23, 32, 55, 66, 78, 89, 90]

36. Python program to create a list of tuples from given list having number and its cube in each
tuple.
Program

l=list(map(int,input("Enter the numbers ").split()))


a=[(x,x**3) for x in l]
print(a)

OUTPUT
In [1]: runfile('D:/Python Programs/Prog2.py', wdir='D:/Python Programs')
Enter the numbers 1 2 3 4
[(1, 1), (2, 8), (3, 27), (4, 64)]

Assignment No. – VII Page - 4 KUSAL SARAF, 18/CSE/124, </> CSE Dept, FIEM

You might also like