Tuesday, 13 July 2021

Top 5000 python programs examples with solutions - Part 5

 Program 51:

Write a program to generate integer coordinate (2,2) to (6,6) using list comprehension?

lst=[(x,y) for x in range(2,7) for y in range(2,7)]
print(lst)


Program 52:

Write a program to generate a list 10 elements and multiply it with 10?

lst=[ i*10 for i in range(1,11)]

print(lst)


Program 53:

Write a program to generate Fibonacci upto 20 using list comprehension?

lst=[0,1]
[lst.append(lst[k-1]+lst[k-2]) for k in range(2,20)]
print('fibonacci series',lst)


Program 54:

Write a program to generate 20 add and 20 even numbers using list comprehension?

even = [x for x in  range(0,40) if x%2==0]
add=[x for x in range(0,40) if x%2!=0]

print('First 20 Add numbers:')
print(add)
print('First 20 Even numbers:')
print(even)


Program 55:

Write a program to generate positive and negative list using list comprehension?

lst=[1,2,3,4,5,6,7,8,-4,-2,-11,-14,-17]

negative=[x for x in lst if x>0]
positive=[x for x in lst if x<0]

print('Negative Numbers:')
print(negative)
print('Positive Numbers:')
print(positive)


Program 56:

Write a program to convert list to uppercase using list comprehension?

lst=['one','two','three','four','five']
lst=[i.upper() for i in lst]
print(lst)


Program 57:

Write a program to generate a 2D matrix size of 4x5 range of 30 to 150?

import random

(rows,cols)=(6,5)

matrix=[[(4*random.randint(30,150))for i in range(cols)] for j in range(rows)]

print(matrix)


Program 58:

Write a program to convert uppercase in a list using list comprehension store in set?

lst=['one','two','three','four','five']
s=set([i.upper() for i in lst])
print(s)


Program 59:

Write a program to generate input values and assign it to set using list comprehension in python?

lst=set([int(x) for x in input('Enter the value:').split()])
print(lst)


Program 60:

Write a below program list comprehension  to normal loop in python?

 b=[n for n in range(5,20) if n%2 == 0]


a=[]
for i in range(5,20):
    if i%2 == 0:
        a.append(i) 
        print(a)

kaavannan blogspot

Labels:

0 Comments:

Post a Comment

Note: only a member of this blog may post a comment.

<< Home