Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Program 1 Python program to display the sum of input (n) numbers

using a list.
Solution print('Shreyas soni\n0827CS191224')
n=int(input("enter the valueof n:"))
list=[]
sum=0
for i in range(0,n):
e=int(input())
list.append(e)
for ele in range(0,len(list)):
sum+=list[ele]
print("sum of input no:",sum)

Output Shreyas soni


(Screen 0827CS191224
Shot) enter the valueof n:5
1
3
2
5
5
sum of input no: 16

Program 2 Python program to insert a number to a given position in a


list.
Solution print('Shreyas soni\n0827CS191224')
print("Enter the Size: ", end="")
try:
numsSize = int(input())
print("Enter " +str(numsSize)+ " Elements: ", end="")
nums = []
for i in range(numsSize):
nums.append(input())
print("\nEnter an Element to Insert at End: ")
elem = input()
nums.append(elem)
numsSize = numsSize+1
print("\nThe New List is: ")
for i in range(numsSize):
print(end=nums[i] + " ")
print()
except ValueError:
print("\nInvalid Input!")
Output Shreyas soni
(Screen 0827CS191224
Shot) Enter the Size: 5
Enter 5 Elements: 1
2
3
4
5

Enter an Element to Insert at End:

The New List is:


123456

Program 3 Write a program that prompts the user for a list of numbers
and prints out the maximum and minimum of the numbers
at the end when the user enters “done”. Write the program
to store the numbers the user enters in a list and use the
max() and min() functions to compute the maximum and
minimum numbers after the loop completes.

Solution print('Shreyas soni\n0827CS191224')


list=[]
while True:
num=input("enter a no.")
if num=='done':
break
list.append(num)
if list:
print("maximum:",max(list))
print("maximum:", min(list))

Output Shreyas soni


(Screen 0827CS191224
Shot) enter a no.1
enter a no.5
enter a no.6
enter a no.2
enter a no.4
enter a no.done
maximum: 6
maximum: 1

Process finished with exit code 0


Program 4 Write another program that prompts for a list of numbers as
above and at the end prints out both the maximum and
minimum of the numbers.
Solution print('Shreyas soni\n0827CS191224')
list=[]
while True:
num=input("enter a no.")
if num=='done':
break
list.append(num)
if list:
print("maximum:",max(list))
print("maximum:", min(list))

Output Shreyas soni


(Screen 0827CS191224
Shot) enter a no.2
enter a no.12
enter a no.45
enter a no.35
enter a no.55
enter a no.done
maximum: 55
maximum: 12

Program 5 Python program to delete an element from a list by index


which is given by the user.
Solution print('Shreyas soni\n0827CS191224')
print("Enter 5 Elements: ")
arr = []
for i in range(5):
arr.append(input())

print("\nEnter the Value to Delete: ")


val = input()

arr.remove(val)
print(arr)
Output Shreyas soni
(Screen 0827CS191224
Shot) Enter 5 Elements:
1
2
3
4
5

Enter the Value to Delete:


3
['1', '2', '4', '5']

Program 6 Write a Python program to print a specified list after


removing the 1st, 2nd and 5th elements.
Solution print('Shreyas soni\n0827CS191224')
list=[1,2,3,4,5]
print("before removal:")
print(list)
list=[x for (i,x) in enumerate(list) if i not in (1,2,5)]
print("after removal:")
print (list)

Output Shreyas soni


(Screen 0827CS191224
Shot) before removal:

[1, 2, 3, 4, 5]
after removal:
[1, 4, 5]

Program 7 Write a Python program to generate all sublists of a list.


Solution print('Shreyas soni\n0827CS191224')
def sub_lists(l):
lists = [[]]
for i in range(len(l) + 1):
for j in range(i):
lists.append(l[j: i])
return lists
l1 = [1, 2, 3]
print(sub_lists(l1))

Output Shreyas soni


(Screen 0827CS191224
Shot) [[], [1], [2], [1, 2], [3], [2, 3], [1, 2, 3]]

Program 8 Write a Python program to find all the values in a list are
greater than a given number
Solution print('Shreyas soni\n0827CS191224')
list = [200,300,400,500]
num=int(input("enter a no.:"))
for i in range(0,len(list)):
if(list[i]>num):
print(list[i])

Output Shreyas soni


(Screen 0827CS191224
Shot) enter a no.:250
300
400
500

Program 9 Python program to find the even numbers in a List.


Solution print('Shreyas soni\n0827CS191224')
list1 = [11,23,45,23,64,22,11,24]
for num in list1:
if num % 2 == 0:
print(num, end = " ")

Output Shreyas soni


(Screen 0827CS191224
Shot) 64 22 24

Program Python program to find the largest number in a list without


10 using built-in functions.
Solution print('Shreyas soni\n0827CS191224')
list = [11,23,45,23,64,22,11,24]

max=list[0]
for i in range(0,len(list)):
if(list[i]>max):
max=list[i]
print("max no is",max)

Output Shreyas soni


(Screen 0827CS191224
Shot) max no is 64

You might also like