You are on page 1of 4

8-10-2023

Sunday, October 8, 2023 7:05 PM

Program:

Filter even numbers and odd numbers if number is even, append to even list if a number is
odd append to odd list.

list=[1,5,9,3,6,11,63,16,18,24,23,27,33,49,67,85]
evenList=[]
oddList=[]
for x in list:
if x%2==0:
evenList.append(x)
else:
oddList.append(x)
print("EvenList is:",evenList)
print("OddList is:",oddList)

To read list values dynamically in the above program:

int-->int(input())
float-->float(input())
string-->input()
.....
....

To read collection related data type values dynamically

list, tuple, set-->eval(input())

list=eval(input("Enter list elements:"))


evenList=[]
oddList=[]
for x in list:
if x%2==0:
evenList.append(x)
else:
oddList.append(x)
print("EvenList is:",evenList)
print("OddList is:",oddList)

output:
D:\>py program1.py
Python 22 batch 7 pm Page 1
D:\>py program1.py
Enter list elements:[22,11,33,55,44,62]
EvenList is: [22, 44, 62]
OddList is: [11, 33, 55]

Write a program to check whether the element exists in a list or not

[22,33,44,55,66]
Enter a element to search:33
33 is found in the list

Enter element to search:20


20 is not found in the list.

n=int(input("Enter number of elements:"))


list=[] 5
for x in range(n): range(5)
value=int(input("Enter values in the list:")) 0,1,2,3,4
list.append(value)
search=int(input("Enter element to search:")) 22
found=False [22,33,44,55,66]
for value in list: found search=33
if value==search:
found=True
if found==True:
print("{} element found".format(search))
else:
print("Element not found")

output:
D:\>py program1.py
Enter number of elements:5
Enter values in the list:44
Enter values in the list:55
Enter values in the list:66
Enter values in the list:22
Enter values in the list:33
Enter element to search:22
22 element found

D:\>py program1.py
Enter number of elements:4
Enter values in the list:22
Enter values in the list:21
Enter values in the list:77
Python 22 batch 7 pm Page 2
Enter values in the list:77
Enter values in the list:88
Enter element to search:10
Element not found

remove():

remove() is used to remove a element from the list. remove() only removes the first
occurrence from the list.

l=[10,20,30,10,30,20]
l.remove(10)
print(l)

output:
[20,30,10,30,20]

l=[10,20,30,10,30,20]
l.remove(99)
print(l)

output:
ValueError: list.remove(x): x not in list

l=[10,20,30,10,30,20]
x=int(input("Enter an element to remove:"))
if x in l:
l.remove(x)
else:
print("Specified element not found")
print(l)

output:
D:\>py program1.py
Enter an element to remove:10
[20, 30, 10, 30, 20]

D:\>py program1.py
Enter an element to remove:77
Specified element not found
[10, 20, 30, 10, 30, 20]

l=[10,20,30,10,20]
x=int(input("Enter an element to remove:"))
Python 22 batch 7 pm Page 3
l=[10,20,30,10,20]
x=int(input("Enter an element to remove:"))
while x in l:
l.remove(x)
print(l)

output:
D:\>py program1.py
Enter an element to remove:10
[20, 30, 20]

Challenge:4

Implement a program that takes two inputs from the user a and b, print 'True' if one of them is 10
or if their sum is 10.

a=
b=

Python 22 batch 7 pm Page 4

You might also like