Python Tutorial @ Home

# Python for loop

## Example:11Write 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 elements3. average of all -ve elementsSol:

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:12Write 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 elements3. average of all even elementsSol:

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:13Write 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 elements3. average of all odd elementsSol:

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:14Write 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
>>>

## 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:15Write 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
>>>

## 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
>>>

#### 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

Important Pages