0% found this document useful (0 votes)
33 views6 pages

ASSIGMENT3

Uploaded by

Rahul Tr
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)
33 views6 pages

ASSIGMENT3

Uploaded by

Rahul Tr
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/ 6

1. Write a Python program to sum all the items in a list.

>>> x=[100,110,120,130]
>>> print(sum(x))
460

2. Write a Python program to multiply all the items in a list.

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

3. Write a Python program to get the largest number from a list.

>>> x=[100,110,120,130]
>>> print(max(x))
130

4. Write a Python program to get the smallest number from a list.

>>> 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

6. Write a Python program to get a list, sorted in increasing order by the


last element in each tuple from a given list of non-empty tuples
Sample List: [(2, 5), (1, 2), (4, 4), (2, 3), (2, 1)]
Expected Result: [(2, 1), (1, 2), (2, 3), (4, 4), (2, 5)]

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)

7. Write a Python program to remove duplicates from a list.

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]

8. Write a Python program to check if a list is empty or not.

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]

9. Write a Python program to clone or copy a list.

>>> 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)

13. Write a Python program to generate a 3*4*6 3D array whose each


element is*.

array= [[[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]],

[[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]

You might also like