Python for loop

Example: 6
Write a python script to display all the elements stored in the list and also print the count of all +ve elements.
Sol:

n=[23,-4,56,-7,68,-4]
p=0;
for i in n:
    print(i)
    if i>0:
        p=p+1
print("Total +ve elements in the list = ",p)

Output:

23
-4
56
-7
68
-4
Total +ve elements in the list = 3
>>>

Example: 7
Write a python script to display all the elements stored in the list and also print the count of all -ve elements.
Sol:

n=[23,-4,56,-7,68,4]
p=0
for i in n:
    print(i)
    if i<0:
        p=p+1
print("Total -ve elements in the list = ",p)

Output:

23
-4
56
-7
68
4
Total -ve elements in the list = 2
>>>

Example: 8
Write a python script to display all the elements stored in the list and also print the count of all even elements.
Sol:

n=[23,4,57,8,69,4]
p=0
for i in n:
    print(i)
    if(i%2==0):
        p=p+1
print("Total even elements in the list = ",p)

Output:

23
4
57
8
69
4
Total even elements in the list = 3
>>>

Example: 9
Write a python script to display all the elements stored in the list and also print the count of all odd elements.
Sol:

n=[23,4,57,8,69,41]
p=0
for i in n:
    print(i)
    if i%2==1:
        p=p+1
print("Total even elements in the list = ",p)

Output:

23
4
57
8
69
41
Total even elements in the list = 4
>>>

Example:10
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 even elements in the list = 5
sum of all +ve elements in the list = 26
average of all +ve elements in the list = 5.2
>>>

Python Basic Programming Tutorial

Python Introduction     Getting started in Python Programming      Python propgramming fundamentals     Python Operators    Python If Condition     Python for loop    Python range construct      Python While loop    break and continue statements     Different looping techniques     Python List     Python String     Python Functions    Python Inbuilt Functions     Python Recursion     Using Python Library     Python Tuples     Python Dictionary     Python Sets     Python Strings     Python Exception Handling     Python Data File Handling

Visitor Counter

0051100