Sunday 18 April 2021

Top 5000 python programs examples with solutions - Part 1

Program 1:

Print the last character or element of an identifier?

var=list("print the sample sentence")
c=var[-1]
print(c)



Program 2:

Print the first 3 elements of the string?

var='print the sample sentence'
lst=list(var[:3])
print(lst)

Program 3:

How to get reverse of a list?

var=list('print the sample sentence')
lst=var[::-1]
print(lst)

Program 4:

How to Add one list to another list?

list1=[1,2,3,4]
list2=[5,6,7,8]
list1.extend(list2)
print(list1)

Program 5:

How to Remove part of an element in a list?

list1=[1,2,3,4,5,6,7,8]
list1[2:5]=[]
print(list1)

Program 6:

How to sort element of a list?

list=[10,2,3,4,5,6,7,8]
list.sort()
print(list)

Program 7:

How to sort element of a list?

list=[10,2,3,4,5,6,7,8]
list.sort()
print(list)

Program 8:

How to reverse sort an element of list?

list=[10,2,3,4,5,6,7,8]
list.sort(reverse=True)
print(list)

Program 9:

How to Compare Two Lists in Python?

list1=[10,2,3,4,5,6,7,8]
list2=[9,2,3,11,2,3,55,11,23]
set1=set(list1)
set2=set(list2)
if set1 == set2:
    print('Both Lists are same')
else:
    print('Both Lists Are not same')

Program 10:

How to update first element of a list?

list1=[10,2,3,4,5,6,7,8]
list1[0]='M'
print(list1)




Labels:

0 Comments:

Post a Comment

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

<< Home