Tuesday 4 May 2021

Top 5000 python programs examples with solutions - Part 2

Program 11:

How to insert a value to a list of an index?
list1=[10,2,3,4,5,6,7,8]
list1.insert(3,30)
print(list1)


Program 12:

How to a check value in a list?
list1=[10,2,3,4,5,6,7,8]
if 5 in list1:
    print('list is having {}'.format(5))
else:
    print('list is not having the value {}'.format(5))


Program 13:

How to convert a string to a list in python?

list1=[10,2,3,4,5,6,7,8]
if 5 in list1:
    print('list is having {}'.format(5))
else:
    print('list is not having the value {}'.format(5))


Program 14:

How to append list to another list in python?
list1=[1,2,3,4,5,6]
list2=['A','B','C','D']
list1=[1,2,3,4,*list2,5,6] 
print(list1)


Program 15:

How to delete a part of list in python?
list1=[1,2,3,4,5,6]
del(list1[2:4])
print(list1)


Program 16:

Create odd list and even list and make it ordered and sorted?
list1=[1,3,5,7]
list2=[2,4,6,8]
list1.extend(list2)
list1.sort() 
print(list1)


Program 17:

Create a list random numbers and input new number find out its availability in the list?
import random
lst=[]
for i in range(20):
    int=random.randint(0,50)
    lst.append(int)

num=input('enter the input number')

for i in range(len(lst)):
    if lst[i] == num: 
    print('input number position:',i)


Program 18:

Create a list 15 elements,remove duplicates?
import random
lst=[]
for i in range(15):
    list=random.randint(0,50)
    lst.append(list)
print('Duplicated List:',lst)
non_dup=[]
for i in lst:
    if i not in non_dup:
        non_dup.append(i)
non_dup.sort()
print('Non Duplicated List:',non_dup)


Program 19:

How to print positive and negative list from a single list? 
list=[1,2,-4,5,-6,7,-8,-9,10]
positive=[]
negative=[]
for i in list:
    if i>0:
        positive.append(i)
    elif i<=-1:
        negative.append(i)
        
print('Positive:',positive)
print('Negative:',negative)


Program 20:

How to write program to uppercase a string?
list=['hello','divyapriya','welcome']
uppercase=[]
for i,items in enumerate(list):
    g=items.upper()
    uppercase.append(g) 
print(uppercase)

Labels:

0 Comments:

Post a Comment

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

<< Home