0% found this document useful (0 votes)
10 views3 pages

List Programs 2024

The document provides a series of Python programming exercises focused on list manipulation. Each exercise includes a specific task, such as creating lists, finding maximum values, filtering elements, and modifying lists based on certain conditions. Sample code and expected outputs are provided for each task to illustrate the functionality.
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)
10 views3 pages

List Programs 2024

The document provides a series of Python programming exercises focused on list manipulation. Each exercise includes a specific task, such as creating lists, finding maximum values, filtering elements, and modifying lists based on certain conditions. Sample code and expected outputs are provided for each task to illustrate the functionality.
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/ 3

Chapter 7 List in Python

1.Write a program to create a list using eval() function and print content of the list on the screen.
lst=eval(input("Enter list of numbers:"))
print(lst)
output
Enter list: [10,20,30,40,50]
[10, 20, 30, 40, 50]

2.Write a program to create a list of n numbers entered by the user and display the content on the screen.
lst=[]
n=int(input("How many nos u want to create list with:"))
for i in range(0,n):
num=int(input("Enter a number:"))
lst.append(num)
print(lst)
Output
How many nos u want to create list with: 5
Enter a number: 10
Enter a number: 20
Enter a number: 30
Enter a number: 40
Enter a number: 50
[10, 20, 30, 40, 50]

3.Write a program to create a list of numbers and find the maximum element of the list.
lst=[]
size=int(input("Enter size of list:"))
for i in range(0,size):
num=int(input("Enter a no:"))
lst.append(num)
max=lst[0]
for i in range(1,size):
if lst[i]>max:
max=lst[i]
print(“Maximun no of the list is”, max)

4.Write a Python program to create a list of numbers and display only those numbers from the list that end
with the digit 4.
lst=eval(input("Enter list of numbers:"))
for ele in lst:
if ele%10==4:
print(ele)
output
Enter a list :[10,14,564,23,4,74]
14
564
4
74

5.Write a program to read a list of n integers (positiveas well as negative). Create two new lists, one having
all positive numbers and the other having all negative numbers from the given list. Print all three lists.
lst=eval(input("Enter list of numbers:"))
size=len(lst)
plst=nlst=[]
for i in range(0, size):
if lst[i]>0:
plst.append(lst[i])
else:
nlst.append(lst[i])
print("Main List:",lst)
print("Positive no List:",plst)
print("Negative no List:",nlst)

6. Write a program to read a list of elements. Modify this list so that it does not contain any duplicate
elements, i.e., all elements occurring multiple times in the list should appear only once.
lst=eval(input("Enter list of numbers:"))
nlst=[]
for ele in lst:
if ele not in nlst:
nlst.append(ele)
lst=nlst
print(lst)
output
Enter list of numbers:[10,20,30,10,20,40,10,50]
[10,20,30,40,50]

7.Write a program to read a list of elements and calculate the mean of a list of numbers.
lst=eval(input("Enter list of numbers:"))
size=len(lst)
sum=0
for i in range(0,size):
sum=sum+lst[i]
mean=sum/size
print("Mean=",mean)

8.Write a program to input a list of numbers and search for an number in a list. If a number is found, then
print its location otherwise print an appropriate message if the number is not present in the list.
lst=eval(input("Enter list of numbers:"))
num=int(input("Enter no to search:"))
size=len(lst)
found=False
loc=0
for i in range(0,size):
if lst[i]==num:
loc=i
found=True
break
if found==False:
print("No not found")
else:
print(num," is found at index ",loc)
output
Enter size of list:[10,20,30,40,50]
Enter no to search:40
40 is found at index 3
9.Write a Python program to create a list of integers and display only the prime numbers from that list.
lst=eval(input("Enter a list :"))
for ele in lst:
found=False
for i in range(2, ele//2+1):
if ele%i==0:
found=True
break
if found==False:
print(ele)
output
Enter a list :[11,4,17,73,12,9]
11
17
73
12

10.Write a program to input a list of numbers and swap elements at the even location with the elements at
the odd location
lst=eval(input("Enter a list of numbers:"))
size=len(lst)
for i in range(0,size-1,2):
lst[i],lst[i+1]=lst[i+1],lst[i]
print(lst)
output
Enter size of list:[10,20,30,40,50,60]
[20, 10, 40, 30, 60, 50]

11.Write a program input a list of numbers and reverse this list without creating new list.
lst=eval(input("Enter a list of numbers:"))
size=len(lst)
j=size-1
for i in range(0,size//2):
lst[i],lst[j]=lst[j],lst[i]
j=j-1
print(lst)
output
Enter size of list:[10,20,30,40,50]
[50, 40, 30, 20, 10]

12. Write a program that creates a list of integers. Modify the list so that all odd values are increased by 5,
while all even values are multiplied by 2.
lst=eval(input("Enter a list of numbers:"))
size=len(lst)
for i in range(0,size):
if lst[i]%2==0:
lst[i]=lst[i]*2
else:
lst[i]=lst[i]+5
print(lst)
output
Enter a list :[5,12,4,7,11]
[10, 24, 8, 12, 16]

You might also like