Example:11
Write a python script to display all the elements stored in the list and also print the following:
1. count of all -ve elements.
2. sum of all -ve elements
3. average of all -ve elements
Sol:
n=[1,-2,3,-4,5,-6,7,-8,-9,10] p=0 s=0 for i in n: print(i) if(i<0): s=s+i p=p+1 print("Total -ve elements in the list = ",p) print("sum of all -ve elements in the list = ",s) av=s/p print("average of all -ve elements in the list = ",av)
Output:
1
-2
3
-4
5
-6
7
-8
-9
10
Total -ve elements in the list = 5
sum of all -ve elements in the list = -29
average of all -ve elements in the list = -5.8
>>>
Example:12
Write a python script to display all the elements stored in the list and also print the following:
1. count of all even elements.
2. sum of all even elements
3. average of all even elements
Sol:
n=[1,2,3,4,5,6,7,8,9,10] p=0 s=0 for i in n: print(i) if(i%2==0): s=s+i p=p+1 print("Total even elements in the list = ",p) print("sum of all even elements in the list = ",s) av=s/p print("average of all even elements in the list = ",av)
Output:
1
2
3
4
5
6
7
8
9
10
Total even elements in the list = 5
sum of all even elements in the list = 30
average of all even elements in the list = 6.0
>>>
Example:13
Write a python script to display all the elements stored in the list and also print the following:
1. count of all odd elements.
2. sum of all odd elements
3. average of all odd elements
Sol:
n=[1,2,3,4,5,6,7,8,9,10] p=0 s=0 for i in n: print(i) if(i%2==1): s=s+i p=p+1 print("Total odd elements in the list = ",p) print("sum of all odd elements in the list = ",s) av=s/p print("average of all odd elements in the list = ",av)
Output:
1
2
3
4
5
6
7
8
9
10
Total odd elements in the list = 5
sum of all odd elements in the list = 25
average of all odd elements in the list = 5.0
>>>
Example:14
Write a python script to display all the elements stored in the list and also print the largest element?
Sol:
n=[23,4,56,7,68,4] c=0 m=0 for i in n: c=c+1 print(i) if(i>m): m=i print("Total elements in the list = ",c) print("Max element = ",m)
Ouput:
23
4
56
7
68
4
Total elements in the list = 6
Max element = 68
>>>
Note: If all the elements in the above program are -ve then we will get invalid output, the correct program can be modified as given below:
Modified programs:
n=[-23,-4,-56,-7,-68,-4] c=0 m=0 for i in n: c=c+1 if(c==1): m=i print(i) if(i>m): m=i print("Total elements in the list = ",c) print("Max element = ",m)
Output:
-23
-4
-56
-7
-68
-4
Total elements in the list = 6
Max element = -4
>>>
Example:15
Write a python script to display all the elements stored in the list and also print the lowest element?
Sol:
n=[23,4,56,7,68,4] c=0 m=0 for i in n: c=c+1 if(c==1): m=i print(i) if(i<m): m=i print("Total elements in the list = ",c) print("Min element = ",m)
Output:
23
4
56
7
68
4
Total elements in the list = 6
Min element = 4
>>>
Example:16
Write a python script to display all the elements stored in the list and also print the following:
* largest element
* lowest element
* difference between largest and lowest element
Sol:
n=[10,24,33,4,15,68,10,82,19,10] ma=0 mi=0 c=0 for i in n: print(i) if c==0: ma=i mi=i c=c+1 #max no if i>ma: ma=i #min no if i<mi: mi=i print("max no = ",ma) print("min no = ",mi) d=ma-mi print("diff = ",d)
Output:
10
24
33
4
15
68
10
82
19
10
max no = 82
min no = 4
diff = 78
>>>