Python Exercise2

1. Write a program to
• Create a list of strings
• Sort it
• Print the last four strings

colors=[“Blue”,”Pink”,”Yellow”,”Red”,”White”,”Black”]

colors.sort()

print(colors)

print(colors[-4:])

1. Write a program to
• Create a list of numbers (integers and floats)
• remove the smallest one
• find out if 9 is there in the list

numb=[10,20.5,60.1,40,50.4,8,90]

numb.remove(min(numb))

print(numb)

print(“9 in numb: “,9 in numb)

1. Write a program to find the average of elements given in a list. Round of the result to 1.

numb=[10,20.5,60.1,40,50.4,8,90]

average=sum(numb)/len(numb)

print(“Average:{} “.format(round(average,1)))

1. Create a list colors and add some colors to the list. Find out the length of each element from the list.

colors=[“Pink”,”Yellow”,”Green”,”Orange”,”Black”,”Blue”]

for color in colors:

print(“Length of color {} is {}”.format(color,len(color)))

1. Write a program to sort and reverse the elements in the list

li=[4,5,6,10,20,40]

li.sort() #by default python sorts a list in Ascending order

print(“List after sorting {} “.format(li))

li.reverse()

print(“List after reversing {} “.format(li))

1. Write a program to add element to a list in different ways

li=[]

li.append(“test1″)

li.append([10,20,30])

li.insert(0,”test3”)

li.insert(1,[1,2])

li.extend([6,7,8])

print(“Final List : {} “.format(li))

1. Write a program to take input from the user for 5 times, and add those elements to the list.

s=[]

for x in range(5):

n=input(“Enter Value in to list : “)

s.append(n)

print(“Final List : {} “.format(s))

1. Write a program to concatenate two lists

l1 = [1,2,43]

l2 = [5,6,7]

l1 = l1 + l2

print(“After Concatenation {}”.format(l1))

1. Write a program to remove 3rd element from the list. Remove the range of elements (4:7).
s = [1,2,3,5,6,7,8,”black”,”blue”,”Green”,”Yellow”,10]

s = [1,2,3,5,6,7,8,”black”,”blue”,”Green”,”Yellow”,10]

s.remove(s[2])   #remove 3rd element from the list

print(“List after removing 3rd element : {} “.format(s))

s[4:7]=[]       #remove range of elements from the list

print(“List after removing range of  elements : {} “.format(s))

1. Write a program to print upper,lower and len of each word from the string ‘The quick brown fox jumps over the lazy dog’

[‘The’, ‘quick’, ‘brown’, ‘fox’, ‘jumps’, ‘over’, ‘the’, ‘lazy’, ‘dog’]

[‘THE’, ‘the’, 3]

[‘QUICK’, ‘quick’, 5]

[‘BROWN’, ‘brown’, 5]

[‘FOX’, ‘fox’, 3]

[‘JUMPS’, ‘jumps’, 5]

[‘OVER’, ‘over’, 4]

[‘THE’, ‘the’, 3]

[‘LAZY’, ‘lazy’, 4]

[‘DOG’, ‘dog’, 3]

words = ‘The quick brown fox jumps over the lazy dog’.split()

print(words)

for word in words:

s=[word.upper(),word.lower(),len(word)]

print(s)

1. Create a list of squares of all odd numbers below 50 and print the list.

a=[]

for x in range(1,50,2):

a.append(x*x)

print(a)

1. Create a list of integers, display the list in descending order and ascending order.

num=[200,50,60,10,80,90,300]

num.sort(reverse=True) #descending order

print(num)

1. Create a list with different data types.

li=[5,10,67.3,”Orange”,[45,67],True,{1:2,4:5}]. Replace 10,67.3, “Orange”,[45,67] with an empty list. Replace the last value (dictionary) with a tuple (5,6).

li=[5,10,67.3,”Orange”,[45,67],True,{1:2,4:5}]

li[1:5]=[]

print(“List after replacing : “,li)

li[-1]=(5,6)

print(“List after removing the last element:”,li)

1. Create a list names with [“hari”,”avinash”,”sreehari”,”arnav”,”amith”]. Print all the names which has “hari” as a substring

names=[“hari”,”avinash”,”sreehari”,”arnav”,”amith”,”harish”]

for name in names:

if “hari” in name:

print(name)

1. Create a list names with [“ria”,”hari”,”avinash”,”sreehari”,”arnav”,”amith”,”ram”]. Print all the names whose len is greater than or equal to 4

names=[“ria”,”hari”,”avinash”,”sreehari”,”arnav”,”amith”,”harish”,”ram”]

for name in names:

if len(name) >=4:

print(name)

Back to Main Page

Visit www.mongofactory.com to know more about the trainer.