Computer Science File
Program List 4
Aishwarya Thakur
Q1) Write a program to print the elements of a list [‘p’,’y’,’t’,’h’,’o’,’n’]
in separate lines along with elements of both indexes. (positive and negative)
Input:
L=['p','y','t','h','o','n ']
print("Positive Index:")
for i in range(0,len(L)):
print(i)
print("Negative Index:")
for i in range(-len(L),0):
print(i)
Output:
Q2) Extract two list-slices out of a given list of numbers.
Display and print the sum of elements of list-slice which contains every
element of the list between indexes 5 to 15.
The program should also display the average of elements in the second list that
contains every fourth element of the list.
Input:
l=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
l1=(l[5:16])
l2=(l1[0:len(l1):4])
print("List 1 is:",l1)
print("List 2 is:",l2)
Output:
Q3) Write a program to print odd numbers and sum of odd numbers from the list.
Input:
a=[]
s=0
l=eval(input("Enter a list: "))
for i in range(0,len(l)):
if(l[i]%2!=0):
a=a+[l[i]]
s=s+l[i]
print("Odd numbers in the list are:",a)
print("Sum of odd numbers in the list is:",s)
Output:
Q4) Write a program to print even numbers and sum of even numbers from the
list.
Input:
a=[]
s=0
l=eval(input("Enter a list: "))
for i in range(0,len(l)):
if(l[i]%2==0):
a=a+[l[i]]
s=s+l[i]
print("Even numbers in the list are:",a)
print("Sum of even numbers in the list is:",s)
Output:
Q5) Write a program to print odd numbers and product of odd numbers from the
list.
Input:
a=[]
p=1
l=eval(input("Enter the list: "))
for i in range(0,len(l)):
if(l[i]%2!=0):
p=p*l[i]
a=a+[l[i]]
print("Odd numbers of the list are: ",a)
print("Product of odd numbers from the list is:", p)
Output:
Q6) Write a program to print even numbers and product of even numbers from the
list.
Input:
a=[]
p=1
l=eval(input("Enter the list: "))
for i in range(0,len(l)):
if(l[i]%2==0):
if(l[i]!=0):
p=p*l[i]
a=a+[l[i]]
print("Even numbers of the list are: ",a)
print("Product of even numbers from the list is:", p)
Output:
Q7) Write a program to print positive numbers and sum of positive numbers from
the list.
Input:
a=[]
s=0
l=eval(input("Enter a list: "))
for i in range(0,len(l)):
if(l[i]>0):
a=a+[l[i]]
s=s+l[i]
print("positive numbers in the list are:",a)
print("Sum of positive numbers in the list is:",s)
Output:
Q8) Write a program to print negative numbers and sum of negative numbers
from the list.
Input:
a=[]
s=0
l=eval(input("Enter a list: "))
for i in range(0,len(l)):
if(l[i]<0):
a=a+[l[i]]
s=s+l[i]
print("negative numbers in the list are:",a)
print("Sum of negative numbers in the list is:",s)
Output:
Q9) Write a program to display the values for the nested list.
Input:
l=eval(input("Enter the list:"))
for i in range(0,len(l)):
if type(l[i])==list:
print(l[i])
Output:
Q10) Write a program to calculate the mean of a given list of numbers.
Input:
=0
L=eval(input("Enter a list:"))
for i in L:
s=s+i
mean=s/len(L)
print("Mean is:",mean)
Output:
Q11) A school stores attendance percentages of students in a list. Write
a program to find and display names of students with attendance below
75%.
Input:
n=eval(input("Enter the list of names:"))
a=eval(input("Enter the attendance percentage:"))
print("Students with low attendance are")
for i in range(0,len(a)):
if(a[i]<75):
print(n[i], a[i])
Output:
Q12) Write a program to search for an element in a given list of
numbers.
Input:
a=int(input("Enter the element:"))
L=eval(input("Enter the list:"))
for i in range(0,len(L)):
if(L[i]==a):
print("Element found at index", i)
Output:
Q13) Write a program to count the frequency of a given element in a list
of numbers.
Input:
L=eval(input("Enter the list:"))
a=int(input("Enter the element to find frequency:"))
f=0
for i in L:
if (i == a):
f+= 1
print("Frequency:", f)
Output:
Q14) Write a program to find the maximum element from the list.
Input:
L=eval(input("Enter the list:"))
m=L[0]
for i in L:
if(i>m):
m=i
print("The maximum element is", m)
Output:
Q15) Write a program to find the minimum element from the list.
Input:
L=eval(input("Enter the list:"))
m=L[0]
for i in L:
if(i<m):
m=i
print("The minimum element is", m)
Output:
Q16)Write a program to print the original list, print the list of unique
elements in the list and duplicate elements in the given list.
Input:
L=eval(input("Enter the list:"))
L1=[]
L2=[]
for i in L:
if i not in L1 and i not in L2:
[Link](i)
elif i in L1:
[Link](i)
[Link](i)
else:
[Link](i)
print("Original List:",L)
print("List of unique elements:",L1)
print("List of duplicate elements:",L2)
Output:
Q17) Write a program to print the reverse of a list.
Input:
L=eval(input("Enter the list:"))
print(L[-1::-1])
Output:
Q18) Write a program that takes any two list L and M of the same size
and adds their elements together to form a new list N whose elements are
sums of the corresponding elements in L and M.
Input:
L1=eval(input("Enter the list:"))
L2=eval(input("Enter the second list:"))
L3=[]
for i in range(0,len(L1)):
[Link](L1[i]+L2[i])
print("The resultant list is:",L3)
Output:
Q19) Write a program that takes a list ‘L’ ,adds 5 in all the odd values
and 10 in all the even values of the list L. Also display the list.
Input:
L=eval(input("Enter the list:"))
L2=[]
for i in range(0,len(L)):
if i%2==0:
[Link](L[i]+10)
elif i%2!=0:
[Link](L[i]+5)
print("The modified list is:",L2)
Output:
Q20)Write a program to display the square of an element if it is integer
and change the case if the element is a string. List is received as an
argument which contains both numbers and strings.
Input:
L=eval(input("Enter the list:"))
for i in range(len(L)):
if (type(L[i])==int):
L[i]=L[i]**2
elif (type(L[i])==str):
L[i]=L[i].swapcase()
print("Modified list:",L)
Output:
Q21) Write a program to display all the names from a list of names which
are starting from the alphabet ‘p’.
Input:
names=eval(input("Enter the list of names:"))
p_names=[]
for i in names:
if i[0]=='P':
p_names.append(i)
print("Names starting with 'P':",p_names)
Output:
Q22) Write a program to multiply an element by 2 if it is odd index for
a given list containing both number and strings.
Input:
L=eval(input("Enter the list:"))
for i in range(len(L)):
if i%2!=0:
L[i]=L[i]*2
print("Modified list:",L)
Output:
Q23) From given list gadgets = [“Mobile”, “Laptop”, 100, “Camera”,
310.28, “Speakers”, 27.00, “Television”, 1000, “Laptop Case”, “Camera
Lens”]
a)create separate lists of strings and numbers.
b)Sort the strings list in ascending order.
c)Sort the strings list in descending order.
d)Sort the number list from lowest to highest.
e)Sort the number list from highest to lowest.
Input:
gadgets=["Mobile", "Laptop", 100, "Camera", 310.28, "Speakers", 27.00, "Television",
1000, "Laptop Case", "Camera Lens"]
strings=[]
numbers=[]
for i in gadgets:
if type(i)==str:
[Link](i)
elif type(i)==int or type(i)==float:
[Link](i)
[Link]()
print("Sorted list of strings in ascending order:",strings)
[Link](reverse=True)
print("Sorted list of strings in descending order:",strings)
[Link]()
print("Sorted list of numbers in ascending order:",numbers)
[Link](reverse=True)
print("Sorted list of numbers in descending order:",numbers)
Q24) Write a program to display all the names from a list of names which
are starting from the alphabet ‘p’.
Input:
L=eval(input("Enter a list:"))
N=[]
for i in L:
if i[0]=='P':
[Link](i)
print("List of strings starting with 'p':",N)
Output:
Q25) Write a program to display all the names from a list of names whose
length is of four characters.
Input:
L=eval(input("Enter a list: "))
N=[]
for i in L:
if len(i)==4:
[Link](i)
print("List of names with exactly 4 characters:",N)
Output:
Q26) Write a program which adds all the numbers in the list.
Input:
L=eval(input("Enter a list:"))
a=0
for i in L:
if type(i)==int or type(i)==float:
a+=i
print("Sum of numbers in the list:",a)
Output:
Q27) A hospital maintains a patient queue. Write a program to remove the
first patient after treatment and add a new one at the end.
Input:
L=eval(input("Enter the list of patients:"))
[Link](L[0])
a=eval(input("Enter the patient to be added:"))
L=L+a
print ("The modified list is:",L)
Output:
Q28) A manufacturing unit records product results as “P” (Pass) or “F”
(Fail). Write a program to calculate the percentage of defective
products.
Input:
L=['P','P','F','F','P','F','P','F','F','P','P','F','P','F','F','P','F','P'
,'P','F']
a=[Link]('F')
print("Percentage of defective products in the list is: ",(a/len(L))*100)
Output:
Q29) . A retail shop keeps product prices in a list. Write a program to
apply a 10% discount on all items priced above ₹1000.
Input:
L=eval(input("Enter product prices:"))
N=[]
for i in L:
if i>1000:
i=i-(i*(10/100))
[Link](i)
else:
[Link](i)
print("Discounted prices:",N)
Output:
Q30) Write a Python program to find the common elements between two
lists.
Input:
L=eval(input("Enter first list:"))
M=eval(input("Enter second list:"))
N=[]
for i in L:
if i in M:
[Link](i)
print("Common elements are:",N)
Output:
Q31) Write a program to check whether a list is a palindrome or not
Input:
L=eval(input("Enter the list:"))
if (L==L[::-1]):
print("The list is a palindrome")
else:
print("The list is not a palindrome")
Output: