ASSIGMENT3
ASSIGMENT3
>>> x=[100,110,120,130]
>>> print(sum(x))
460
def y(i):
result=1
for item in i:
result *=item
return result
x=[1,2,3,4,5,6,7,8,9,10]
result=y(x)
print(result)
Output
3628800
>>> x=[100,110,120,130]
>>> print(max(x))
130
>>> x=[100,110,120,130]
>>> print(min(x))
100
5. Write a Python program to count the number of strings from a given
list of strings. The string length is 2 or more and the first and last
characters are the same.
Sample List : ['abc', 'xyz', 'aba', '1221']
Expected Result : 2
def y(i):
z=sorted(i, key=lambda x: x[-1])
return z
x=[(2,5),(1,2),(4,4),(2,3),(2,1)]
result=y(x)
print(result)
x=[1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9]
print(x)
i=0
while(i<len(x)):
j=0
while(j<len(x)):
y=x[i]
z=x[j]
if(i==j):
j=j+1
elif(y==z):
x.remove(z)
j=j+1
else:
j=j+1
i=i+1
x.sort()
print(x)
Output
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
x=[100,110,120,130]
if(len(x)==0):
print("List is empty")
else:
print("List is not empty")
print(x)
Output:
List is not empty
[100, 110, 120, 130]
>>> x=[100,110,120,130]
>>> y=x
>>> y
[100, 110, 120, 130]
>>> x
[100, 110, 120, 130]
>>>
10. Write a Python program to find the list of words that are longer than n
from a given list of words.
x=["Apple","a","Banana","Cherry","Mango","Pinepple","sapota","jackfruit"]
print("Enter the min length number")
u=input()
n=int(u)
i=0
while(i<len(x)):
y=len(x[i])
if(y>n):
print(x[i])
i=i+1
else:
i=i+1
Output
Enter the min length number
4
Apple
Banana
Cherry
Mango
Pinepple
sapota
jackfruit
11. Write a Python function that takes two lists and returns True if they
have at least one common member.
x=["Apple","a","Banana","Cherry","Mango"]
y=["Pinepple","sapota","jackfruit","Cherry"]
i=0
while(i<len(x)):
j=0
while(j<len(y)):
if(x[i]==y[j]):
print("True")
j=j+1
else:
j=j+1
i=i+1
Output
True
12. Write a Python program to print a specified list after removing the
Oth, 4th and 5th elements.
Sample List: ['Red', 'Green', 'White', 'Black', 'Pink', 'Yellow']
Expected Output : ['Green', 'White', ‘Black’]
x=['Red','Green','White','Black','Pink','Yellow']
i=0
j=4
k=-1
x.remove(x[i])
x.remove(x[j])
x.remove(x[k])
print(x)
[[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]]]
print(array)
14. Write a Python program to print the numbers of a specified list after
removing even numbers from it.
x=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
i=0
while(i<len(x)):
if((x[i]%2)==0):
del x[i]
else:
print(x[i])
i=i+1
print(x)
Output
[1, 3, 5, 7, 9, 11, 13, 15]
15. Write a Python program to shuffle and print a specified list.
import random
def shuffle(y):
random.shuffle(y)
print(y)
x=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
shuffle(x)
Output
[8, 14, 9, 13, 11, 16, 2, 0, 7, 15, 4, 1, 6, 12, 5, 3, 10]