PYTHON TUTORIAL | PYTHON LIST

Python List Questions

Question:1
Python program to create a list and display all the elements?
Sol:

n=[]
while True:
    a=int(input("Enter any element "))
    n.append(a)
    ch=input("Like to add another element ...(y/n) ")
    if(ch=="y" or ch=="Y"):
        continue
    else:
        break
print("list is ")
print(n)

        

Output:

Enter any element 2
Like to add another element …(y/n) y
Enter any element 6
Like to add another element …(y/n) y
Enter any element 8
Like to add another element …(y/n) y
Enter any element 78
Like to add another element …(y/n) y
Enter any element 25
Like to add another element …(y/n) y
Enter any element 45
Like to add another element …(y/n) n
list is
[2, 6, 8, 78, 25, 45]
>>>

Question:2
Python program to calculate and print sum of all the elements of a list?
Sol:

n=[1,2,3,4,5]
s=0
for i in n:
    print(i)
    s=s+i
print("Sum of all elements ",s)

Output:

1
2
3
4
5
Sum of all elements 15
>>>

Question:3
Python program to calculate and print product of all the elements of a list?
Sol:

n=[1,2,3,4,5]
p=1
for i in n:
    print(i)
    p=p*i
print("Product of all elements ",p)

Output:

1
2
3
4
5
Product of all elements 120
>>>

Question:4
Python program to print total +ve elements in the list?
Sol:

n=[1,2,-3,4,-5]
p=0
for i in n:
    print(i)
    if(i>0):
        p+=1
print("Total +ve elements ",p)

Output:

1
2
-3
4
-5
Total +ve elements 3
>>>

Question:5
Python program to print total -ve elements in the list?
Sol:

n=[1,2,-3,4,-5]
p=0
for i in n:
    print(i)
    if(i<0):
        p+=1
print("Total -ve elements ",p)

Output:

1
2
-3
4
-5
Total -ve elements 2
>>>

Visitor Counter

0051049