In: Computer Science
By using Python code:
1. list1 = ['smith', 'john', 'andy']
list2 = ['kim', 'mary', 'paul']
Write a python program which does the following:
a. add items in list2 to list1
b. Remove mary from list1
c. Insert sam at 5th position
d. Sort the elements of list1 in ascending order
2. Write a python program that asks the user to enter a sentence and creates a list of words in that sentence.
3. friends =
('sam','andy','mary','andy','john','andy','mary')
Write a python program which does the following:
a. for the above string calculate the number of times "andy" appears in the tuple
b. change sam with smith
c. print index of john
Q1
for i in list2:
list1.append(i)
list1.remove('mary')
list1.insert(5,'sam')
list1.sort()
print(list1)
The output is
Q2
string=input('Enter a sentence')
words=list(set(string.split()))#set is used to get unique words
from sentence
print(words)
Q3
friends =
('sam','andy','mary','andy','john','andy','mary')
count=0
for i in friends:
if i=='andy':
count+=1
print('Andy appears',count,'times')
temp=list(friends)
temp[0]='smith'
friends=tuple(temp)
print(friends)
print(friends.index('john'))
The output is -