Python List Programs

Write a program in Python, which accepts a list Arr of numbers and n is a numeric value by which all elements of the list are shifted to left.

​Sample Input Data of the list Arr = [ 10, 20, 30, 40, 12, 111 ], n = 2

​Output Arr = [ 30, 40, 12, 11, 10, 20 ]

L=eval(input("Enter a list:"))

'''

L= [10,20,30,40,12,11]
     0  1  2  3  4  5
#-ve -6 -5 -4 -3 -2 -1

11=>5-2=>3
12=>4-2=>2
40=>3-2=>1
30=>2-2=>0

20=>1-2=>-1
10=>0-2=>-2
'''
n=int(input("Enter the number of shifts"))
M=[0]*len(L) #it will create a list of 0 with len(L)
#number of elements
for i in range(0,len(L)):
    M[(i-n)%len(L)]=L[i]

print("New list is:",M)

Write a program to find the sum of prime numbers in a list.

L=[] #empty list
#----accepting the list values----#
n=int(input("Enter the number of element you want to enter:"))
for i in range(n):
    m=int(input("Enter a number:"))
    L.append(m)
print("The entered list is:",L)
s=0 #sum of the prime numbers
print("The prime numbers are:")
for i in L:
    v=i #each element of list
    c=0 #counter
    for j in range(1,v+1):
        if v%j==0:
            c=c+1
    if c==2:
        s=s+v #sum of the primes
        print(v)
print("The sum of the primes are:",s)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top