Assignment of Python,by codexritik.

CodeXRitik 💥


Q.1 python program to interchange first and last elements in a list.

Solution:

a=list(map(int,input('enter the element=').split( )))
b=a[0]
a[0]=a[4]
a[4]=b

print(a)

Q.2 python program to swap two elements in a list.

Solution:

a=list(map(int,input('enter the element=').split( )))
c=list(map(int,input('enter the element=').split( )))
b=a[3]
a[3]=c[2]
c[2]=b
print(a)
print(c)

Q.3 python ways to find length of  list.

Solution:

a=list(map(int,input().split()))
b=len(a)
print(b)

Q.4 python ways to check if element exists in list.

Solution:

a=list(map(int,input().split()))
b=int(input())
if(b in a):
    print('exist')
else:
    print('not exist')

Q.5 python reversing a List

Solution:

a=list(map(int,input().split()))
for i in reversed(a):
    print(i,end=' ')
    
 Q.6 python cloning or copying a list

Solution:

a=list(map(int,input().split()))
b=a.copy()
print(b)

            OR

a=list(map(int,input().split()))
for b in a:
    print(b,end=' ')

Q.7 python count occurrences of an element in a list

Solution:

a=list(map(int,input().split()))
b=0
for i in a:
    b=b+1
print(b)

Q.8 python program to find sum of elements in list

Solution;

a=list(map(int,input().split()))
b=0
for i in a:
    b=b+i
print(b)

     
Q.9 python multiply all numbers in the list

Solution:

a=list(map(int,input().split()))
b=1
c=0
for i in a:
    b=b*i
print(b)
    

    
Q.10 python program to find smallest number in a list
                             And
Q.11 python program to find largest number in a list

Question:

a=list(map(int,input().split()))
b=a[0]
c=a[0]
for i in a:
    if(i<b):
        b=i
    if(i>c):
        c=i
print('smallest=',b,'maximum=',c)
    
Q.12 python program to find second largest no in python

Solution:

a=list(map(int,input().split()))
b=a[0]
for i in a:
    if(i>b):
        c=b
        b=i
print(c)

Q.13 python program to print even no in a list.
                                 AND
Q.14 python program to print odd no in a list .

Solution:

a=list(map(int,input().split()))
b=[];c=[]
for i in a:
    if(i%2==0):
        b.append(i)
    if(i%2!=0):
        c.append(i)
print('even=',b,'odd=',c)
    
Q.15 python program to count even and odd numbers in a list.

Solution:

a=list(map(int,input().split()))
b=[];c=[];d=0;e=0
for i in a:
    if(i%2==0):
        b.append(i)
for j in b:
    d=d+1
print('even=',d)
for i in a:
    if(i%2!=0):
        c.append(i)
for j in c:
        e=e+1
print('odd=',e)

Q.16 python program to print positive no. in list
                              AND
Q.17 python program to print negative no. in list.

Solution:

a=list(map(int,input().split()))
b=[];c=[]
for i in a:
    if(i>0):
        b.append(i)
    if(i<0):
        c.append(i)
print('negative=',c,'positive=',b)

Q.18 python program to print all positive numbers in range.
                                       AND
Q.19 python program to print all negative numbers in range.

Solution:

z=int(input('small value='))
x=int(input('greater value='))
num=0;num1=0
larger=0;smaller=0
for i in range(z,x+1):
    if(i>0):
        larger=i
        num=larger
        print('positive=',num)
    if(i<0):
        smaller=i
        num1=smaller
        print('negative=',num1)

Q.20 python program to count positive and negative numbers in a list

Solution:

a=list(map(int,input().split()))
b=[];c=[];e=0;f=0
for i in a:
    if(i>0):
        b.append(i)
for j in b:
    e=e+1
print('positive=',e)
for i in a:
    if(i<0):
        c.append(i)
for j in c:
    f=f+1
print('negative=',f)

Q.21 Remove multiple elements from a list in python.

Solution:

a=list(map(int,input().split()))
print(set(a))
        



        
        

        

        
     



Assignment of Python,by codexritik. Assignment of Python,by codexritik. Reviewed by CodexRitik on February 16, 2020 Rating: 5

No comments:

Powered by Blogger.