Friday, 18 June 2021

Top 5000 python programs examples with solutions - Part 3

Program 21:

How to write  a program to calculate negative numbers in a list?

list=[1,2,3,4,5,-1,-2,-3,-4,-8]
if 0 not in list:
    list.append(0)
list=sorted(list)
pos=list.index(0)
print(pos)


Program 22:

How to write first character of  words from a list to new list?

list=['Hello','every','one']
list2=[]
for i in list:
    list2.append(i[0]) 
print(list2)


Program 23:

Write a program to remove empty tupes in python?
tuples=((1,123),(),(2,213),(),(3,313))
lst2=[]
for i in tuples:
    if len(i) != 0:
        lst2.append(i)
print(lst2)


Program 24:

How to convert list to tuples in python?
rollno=[1,2,3,4,5]
age=[12,23,45,123]
name=['senthil','ravi','mani','ramp']
rollno_1=tuple(rollno)
age_no=tuple(age)
name_no=tuple(name)
print(rollno_1,age_no,name_no)


Program 25:

How to create sets from a list startswith 'A' and 'B'  in python?
setv={'Apple','Ball','Cat','Dog','Elephant','Frog','Attur','Basket','Ariyalur','Belur'}
A=set()
B=set()
for i in setv:
    if i.startswith('A'):
        A.add(i)
    if i.startswith('B'): 
        B.add(i)


Program 26:

Generate the random numbers between 10 to 25 find out values greater 20 using set?
import random
A=set()
while True:
    A.add(random.randint(10,25))
    if len(A) == 10:
        break
    
print('Original List:',A)
B=set()
count=0
for item in A:
    if item > 20:
        B.add(item)
        count+=1
print('Values Greater 20:',B)
print('Count Values Greater 20 :',count)


Program 27:

Two variable  Swapping in python using tuple?
x=10
y=20
print('Before Swap X={},Y={}'.format(x,y))
(x,y)=(y,x) 
print('After Swap X={},Y={}'.format(x,y))


Program 28:

Write a program between 1000 and 2000  find all numbers divisible by 7 but not  multiple of 4?
list=[]
for i in range(1000,2000):
    if (i%7==0) and (i%4!=0):
        list.append(str(i))
         print (','.join(list))


Program 29:

How will you remove duplicates in list,string,tuple?
s='helloworld'
lst=['h','e','l','l','o','w','o','r','l','d']
tpl=('h','e','l','l','o','w','o','r','l','d')

s=''.join(sorted(set(s),key=s.index))
lst=list(sorted(set(lst),key=lst.index))
tpl=tuple(sorted(set(tpl),key=tpl.index))

print(s)
print(lst) 
print(tpl)


Program 30:

How to perform a set is a subset of another set?
s={1,2,3,4,5,6,7}
t={3,4,1}
print(t.issubset(s)) #True
print(t<s) #True
print(t>s) #False

Labels:

0 Comments:

Post a Comment

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

<< Home