11 listBasedQuestions

You might also like

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

#1.Program to create a list of n number of elements.

L=[] #L=list()
n=int(input('How many number of elements you want to insert:'))
for i in range(n): # 0 to n-1
x=int(input('Enter a number:'))
L.append(x)

print('List:',L)
'''
#------------------------------------------------
#2.Program to create a tuple of n numbers.
t=()
n=int(input('How many number of elements you want to insert:'))
for i in range(n):
x=int(input('Enter a number:'))
t=t+(x,) #(10,20,30)
print('Tuple:',t)

#-------------------------------------------------

#3.Program to create a dictionary with n-elements


d={} # d=dict()
n=int(input('How many number of elements you want to insert:'))
for i in range(n):
key=int(input('Enter the roll number of Student'))
value=input('Enter the name of student:')
d[key]=value
print(d)
'''
-----------------------------------------------------
'''
4.Find the smallest element from a list.
'''
L=[2,3,7,4,1]
min=L[0]
for i in range(len(L)):
if L[i]<min:
min=L[i]
print('List:',L)
print('Minimum element of List:',min)
-------------------------------------------------------
'''
5.Find the smallest element from a list.
'''
L=[2,3,7,4,1]
min=L[0]
for i in range(len(L)):
if L[i]<min:
min=L[i]
print('List:',L)
print('Minimum element of List:',min)
-------------------------------------------------------
'''
6. program to create a list of numbers and print the addition
of even elements and odd elements.
'''
L=[]
n=int(input('How many number of elements you want to insert in the list:'))
for i in range(n):
x=int(input('Enter a number:'))
L.append(x)
print('List:',L)
se=0
so=0
for i in range(len(L)):
if L[i]%2==0:
se=se+L[i]
else:
so=so+L[i]
print('Sum of even numbers:',se)
print('Sum of odd numbers:',so)
------------------------------------------------------
'''
7.Input a list of numbers and swap elements at the even location
with the elements at the odd location.
'''
L=[]
n=int(input('How many number of elements you want to insert into the list:'))
for i in range(n):
x=int(input('Enter a number:'))
L.append(x)
print('List:',L)
i=0
while i<len(L)-1:
L[i],L[i+1]=L[i+1],L[i]
i+=2
print('Processed list:',L)

-------------------------------------------------------------
'''
8. Program to search an element in the list
'''
n=int(input('How many number of elements you want to insert in the list:'))
arr=[]
for i in range(n):
arr.append(int(input('Enter an element in the list:')))

s=int(input('Enter an element to search in the list:'))


print('List:',arr)
for i in range(len(arr)):
if arr[i]==s:
print(s,'is found at',i+1,'position.')
break
else:
print(s,'is not found in the list')
------------------------------------------------------------------
'''
9. Create a dictionary with the roll number, name and marks of
n students in a class and display the name of students who
have scored marks above 75.
'''
n=int(input("Enter number of students: "))
result={}
for i in range(n):
print("Enter Details of student No.", i+1)
rno=int(input("Roll No: "))
name=input("Student Name: ")
marks=int(input("Marks: "))
result[rno] = [name, marks]

print(result)
#Display names of students who have got marks more than 75
print("Student's name who get more than 75 marks is/are")
for i in result:
if result[i][1] > 75:
print(result[i][0])
---------------------------------------------------------------------
#10. Program to sort a list by using Bubble sort.
aList=[15,6,13,22,3,52,2]
print("Original list is :",aList)
n=len(aList)

for i in range(n-1):
for j in range(n-1-i):
if aList[j]>aList[j+1]:
aList[j],aList[j+1]=aList[j+1],aList[j]
print('List after Sorting:',aList)

#aList.sort()
--------------------------------------------------------------------
#11. Program to sort a list by using sort.
L=[2,4,5,1,3]
print('Unsorted List:',L)
for i in range(1,len(L)):
key=L[i]
a=i-1
while a>=0 and key<L[a]:
L[a+1]=L[a]
a-=1
else:
L[a+1]=key
print('Sorted List:',L)
------------------------------------------------------------------
'''
12. Program to count the number of strings where the string length
is 2 or more and the first and last characters are same
from a given list of strings.

Sample List: ['abc','xyz','cbc','121']


Expected Result: 2
'''
L=['abc','xyz','cbc','121']
count=0
for i in L:
if len(i)>=2 and i[0]==i[len(i)-1]:
count=count+1
print(i)
print('No. of strings having length 2 or more:',count)
-----------------------------------------------------------------
'''
13.Write a program which accepts a number from the user and
prints the frequency of the number in the list given as under,
if number is not in the list, it should print 'Number not
available'.
L=[3,21,5,6,3,8,21,6]
'''
L=[3,21,5,6,3,8,21,6]
print(L)
n=int(input('Enter a number to check its frequency in List:'))
c=0
for i in range(len(L)):
if L[i]==n:
c=c+1
print('Frequency of',n,'in List is',c)
if c==0:
print('Number is not available in the list.')
-------------------------------------------------------------
#14.
'''
Write a program that takes a list, adds 5 in all the odd values and 10 in all
the even values of the list.
'''
L=[10,20,3,100,65,87,1]
for i in range(len(L)):
if L[i]%2==0:
L[i]+=10
else:
L[i]+=5
print(L)
-------------------------------------------------------------------
#15.
'''
Program to swap an element of list with the next element if
it the element is divisible by 5.
'''
L=[2,3,5,7,10,1,15,11,50,7]
i=0
print('Actual List:',L)
while i<len(L):
if L[i]%5==0:
L[i],L[i+1]=L[i+1],L[i]
i+=2
else:
i+=1
print('New List:',L)
-------------------------------------------------------------
#16.
'''
Program to create a list of 7 positive integers and create another
list from previously defined list with only those elements that
are divisible by 5.
'''

L=[]
i=0
while i<7:
x=int(input('Enter a number:'))
if x<0:
print('Please enter a positive integer.')
continue
else:
L.append(x)
i+=1
print(L)
L_new=[]
for i in range(len(L)):
if L[i]%5==0:
L_new.append(L[i])
print('Original list:',L)
print('List of those numbers that are divisible by 5:',L_new)
---------------------------------------------------------------
#17.

You might also like