Categories:Viewed: 6 - Published at: a few seconds ago

Write a python program to search a key in the text file

fname = 'sample.txt'
l='keyword' # Enter letter to be searched
k = 0

with open(fname, 'r') as f:
    for line in f:
        words = line.split()
        for i in words:
            if(i==l):
                k=k+1
print("Occurrences of the letter:",k)

Write a python program to expalin list comprehension and print alternative values

t = (1, 2, 4, 3, 8, 9)
print([t[i] for i in range(0, len(t), 2)])

Write a python program to sort tuple values

a=(2,3,1,5)
tuple_sorted = sorted(a)
print(tuple(tuple_sorted))

Write a python program to multiple two list values

l1=[1,2,3]
l2=[4,5,6]

print('multiply two list values:',[x*y for x in l1 for y in l2])

Write the list comprehension to pick out only negative integers from a given list ā€˜lā€™.

l1=[1,2,3,-4,-8]

print('negative integers:', [x for x in l1 if x<0])

Write a python program to convert all list elements to upper case

s=["pune", "mumbai", "delhi"]
print([(w.upper(), len(w)) for w in s])

Write a python program to expalin python zip method

l1=[2,4,6]
l2=[-2,-4,-6]
for i in zip(l1, l2):
    print(i)

Write a python program to add two list using python zip method

l1=[10, 20, 30]
l2=[-10, -20, -30]
l3=[x+y for x, y in zip(l1, l2)]
print('added two list:',l3)

Write a list comprehension for number and its cube

l=[1, 2, 3, 4, 5, 6, 7, 8, 9]
print([x**3 for x in l])

Write a list comprehension for printing rows into columns and vv

l=[[1 ,2, 3], [4, 5, 6], [7, 8, 9]]
print([[row[i] for row in l] for i in range(3)])

Write a list comprehension for printing rows into columns and vv

def unpack(a,b,c,d):
    print(a+d)
x = [1,2,3,4]
unpack(*x)

Write a python program to use python lambda function

lamb = lambda x: x ** 3
print(lamb(5))

Write a python program to multiply a string n times

a = 'python'
print(a*5)