You are on page 1of 10

Assignment

#3
1.Write a program to input a list of integers and replace each even element of  the
list with the sum of its digits and each odd element with the product of its digits. 
For example, If the list is entered as: 
[100, 43, 20, 56, 32, 91, 80, 40, 45, 21] 
After executing the program, the list content should be changed as follows: 
[1, 12, 2, 11, 5, 9, 8, 4, 20, 2]
Code:
user = eval(input("Enter a list of integers: "))
print("Original list :",user)
for i in range (len(user)):
s=0
p=1
num = user[i]
while num>0:
r = num%10
s = s+r
p*=r
num//=10
if user[i]%2==0:
user[i]=s
else:
user[i]=p
print("Updated list:",user)

Output:
Enter a list of integers: [100, 43, 20, 56, 32, 91, 80, 40, 45, 21]

Original list : [100, 43, 20, 56, 32, 91, 80, 40, 45, 21]
Updated list: [1, 12, 2, 11, 5, 9, 8, 4, 20, 2]
>>>

2.Write a program to input a list from the user and modify its content in such a
way that the elements, which are multiples of 10 are swapped with the value
present in the very next position in the list. 
For example If the content of list P is:
[91, 50, 54, 22, 30, 54] 
Then content of list P should become:
[91, 54, 50, 22, 54, 30]
Code:
user = eval(input("Enter a list of integers: "))
print("Original list :",user)
i=0
while i<len(user)-1:
if user[i]%10==0:
user[i],user[i+1]=user[i+1],user[i]
i=i+2
else:
i=i+1
print("Updated list:",user)

Output:
Enter a list of integers: [91, 50, 54, 22, 30, 54]

Original list : [91, 50, 54, 22, 30, 54]


Updated list: [91, 54, 50, 22, 54, 30]
>>>

3.Write a program to swap even and odd positions of the values in list Val.
         For example, If the list is entered as: 
               [25,17,19,13,12,15]
 After swapping the list content should be displayed as: 
          [17,25,13,19,15,12]

Code:
user = eval(input("Enter a list of integers: "))
print("Original list :",user)
i=0
while i<len(user)-1:
user[i],user[i+1]=user[i+1],user[i]
i=i+2
print("Updated list:",user)

Output:
Enter a list of integers: [25,17,19,13,12,15]

Original list : [25, 17, 19, 13, 12, 15]


Updated list: [17, 25, 13, 19, 15, 12]
>>>

4.Write a program to input two different lists and then create a list that all the
common elements from the two lists.
For example : 
If list1=[11, 22, 44, 55, 99, 66] and list2=[44, 22, 55, 12, 56] 
then list3 is to be created as list3 = [22, 44, 55]
Code:
l1 = eval(input("Enter a list of numbers: "))
l2 = eval(input("Enter another list of numbers: "))
l3 = []
for i in l1:
for j in l2:
if i==j:
l3.append(i)
print("List of common elements :",l3)

Output:
Enter a list of numbers: [11, 22, 44, 55, 99, 66]

Enter another list of numbers: [44, 22, 55, 12, 56]


List of common elements : [22, 44, 55]
>>>

5.Write a program to enter a list and then push all the zeros in the list to the end
of the list. 
For example: If the list contains [0, 2, 3, 4, 6, 7, 0, 1]
Then the program should re-arrange the elements as [2, 3, 4, 6, 7,1, 0, 0]
Code:
l1 = eval(input("Enter a list of integers: "))
print("Original list:",l1)
for i in l1:
if i==0:
l1.remove(i)
l1.append(i)
print("Updated list:",l1)

Output:
Enter a list of integers: [0, 2, 3, 4, 6, 7, 0, 1]

Original list: [0, 2, 3, 4, 6, 7, 0, 1]


Updated list: [2, 3, 4, 6, 7, 1, 0, 0]
>>>

6.Write a program to input a list and then double the even elements of the list and
triple the odd elements of the list.

Code:
l1 = eval(input("Enter a list of integers: "))
print("Original list:",l1)
for i in range(len(l1)):
if l1[i]%2==0:
l1[i]*=2
else:
l1[i]*=3
print("Updated list:",l1)

Output:
Enter a list of integers: [22,13,14,17,9,84]

Original list: [22, 13, 14, 17, 9, 84]


Updated list: [44, 39, 28, 51, 27, 168]
>>>

7.Write a program to input a list containing names of cities and then display the
names of all those cities that start with the alphabet ‘A’. 
For example:
If the list contains [“JAMMU”, “DELHI”, “AMRITSAR”,"CHENNAI”,“AGRA”]
Then the program should print AMRITSAR and AGRA

Code:
l1 = eval(input("Enter a list: "))
print("Original list:",l1)
for i in range(len(l1)):
if l1[i][0] in "aA":
print(l1[i])

Output:
Enter a list: ["JAMMU", "DELHI", "AMRITSAR","CHENNAI","AGRA"]

Original list: ['JAMMU', 'DELHI', 'AMRITSAR', 'CHENNAI', 'AGRA']


AMRITSAR
AGRA
>>>

8.Write a menu driven program which creates an empty list and gives the following
options to the user to perform various operations on a list: 
(i)   Append an element to the list 
(ii)  Input an element from the user and remove it from the list 
(iii) Remove all elements from the list 
(iv) Count the number of occurrences of an element in the list 
(v)  Sort the list 
(vi) Reverse the list 
(vii)Display the list 

Code:
L=[]
n=int(input("Choose a task that you would like to perform,by entering the
appropriate number \n1- Append an element to the list \n2- Input an element
from the user and remove it from the list \n3- Remove all elements from the
list \n4- Count the number of occurrences of an element in the list \n5- Sort the
list \n6- Reverse the list \n7- Display the list \nYour Choice: "))
L=list(eval(input("Enter a List of Integers: ")))
if n==1:
c=int(input("Enter element to append: "))
L.append(c)
elif n==2:
c=int(input("Enter element to remove: "))
L.remove(c)
elif n==3:
L.clear()
elif n==4:
c=int(input("Enter element to count: "))
print("The count of",c,"in the given list is",L.count(c))
elif n==5:
L.sort()
elif n==6:
L.reverse()
elif n==7:
print("The list is:",L)
else:
print("Invalid Choice")

print("List now is: ",L)

Output:
Choose a task that you would like to perform,by entering the appropriate
number
1- Append an element to the list
2- Input an element from the user and remove it from the list
3- Remove all elements from the list
4- Count the number of occurrences of an element in the list
5- Sort the list
6- Reverse the list
7- Display the list

Your Choice: 1
Enter a List of Integers: [1,2,5,8,9]
Enter element to append: 8
List now is: [1, 2, 5, 8, 9, 8]
Your Choice: 2
Enter a List of Integers: [1,2,5,8,9]
Enter element to remove: 5
List now is: [1, 2, 8, 9]

Your Choice: 3
Enter a List of Integers: [1,2,5,8,9]
List now is: []

Your Choice: 4
Enter a List of Integers: [1,2,4,4,4,5,6,7,8]
Enter element to count: 4
The count of 4 in the given list is 3
List now is: [1, 2, 4, 4, 4, 5, 6, 7, 8]

Your Choice: 5
Enter a List of Integers: [0,25,67,93,45,27]
List now is: [0, 25, 27, 45, 67, 93]

Your Choice: 6
Enter a List of Integers: [25,17,19,13,12,15]
List now is: [15, 12, 13, 19, 17, 25]

Your Choice: 7
Enter a List of Integers: [1,2,3,4,5,6,7]
The list is: [1, 2, 3, 4, 5, 6, 7]
List now is: [1, 2, 3, 4, 5, 6, 7]

You might also like