# Python while Loop

Python language is easy and broad programming language.The syntax of Python Code is simple as compared to many different languages like java, C++ etc .All we have to do is to take care of the indentation of the code .Here we give many basic examples for the beginners to illustrate python for loop ,python while loop and many other examples to understand how this language works in easy and efficient way.

## Example#1:

Find the sum of all elements of td.
td= [[5, 12, 60,80],[1,16,24,12],[10,25,32,39],[14,99,78,62]]

CODE:

acc = 0
i = 0
while i<len(td):
j = 0
while j<len(td[i]):
acc += td[i][j]
j+=1
i+=1
print acc

## Example#2

Find sum of each row of td.
td= [[5, 12, 60,80],[1,16,24,12],[10,25,32,39],[14,99,78,62]]

CODE:

i = 0
ac = [0]*(len(td))
while i<len(td):
ac[i] = 0
i+=1
i = 0
while i<len(td):
j = 0
while j<len(td[i]):
ac[i]+=td[i][j]
j+=1
i+=1

print ac

## Example#3

Find sum of each column of td.
td= [[5, 12, 60,80],[1,16,24,12],[10,25,32,39],[14,99,78,62]]

CODE:

i = 0
ac = [0]*(len(td))
while i<len(td):
ac[i] = 0
i+=1
i = 0
while i<len(td):
j = 0
while j<len(td[i]):
ac[i]+=td[j][i]
j+=1
i+=1
print ac

## Example#4

Find average of each rows.
td= [[5, 12, 60,80],[1,16,24,12],[10,25,32,39],[14,99,78,62]]

CODE:

i = 0
ac = [0]*(len(td))
while i<len(td):
ac[i] = 0
i+=1
i = 0
while i<len(td):
j = 0
while j<len(td[i]):
ac[i]+=td[i][j]
j+=1
ac[i] = ac[i]/4.0
i+=1
print ac

Output:

## Example#5

Find trace of the List td.
td= [[5, 12, 60,80],[1,16,24,12],[10,25,32,39],[14,99,78,62]]

CODE:

ac = 0
i = 0
while i<len(td):
j = 0
while j<len(td[i]):
if i == j:
ac +=td[i][j]
j+=1
i+=1
print ac

Output:

## Example#6

Find average of the diagonal elements of td.
td= [[5, 12, 60,80],[1,16,24,12],[10,25,32,39],[14,99,78,62]]

CODE:

ac = 0
i = 0
while i<len(td):
j = 0
while j<len(td[i]):
if i == j:
ac +=td[i][j]
j+=1
i+=1
ac = ac/4.0
print ac

Output:

## Example#7

Consider following 2D matrix of 3×3 integer values. Print the
# transpose of matrix td.
# Td= [[5, 12, 60],[1,16,24],[10,25,32]]

CODE:

z = [[5, 12, 60],[1,16,24],[10,25,32]]
i = 0
n = [0]*(len(z))
while i<len(z):
n[i] = [0,0,0]
i+=1
row,col= 0,0
while row<len(z):
col = 0
while col<len(z[row]):
n[col][row]=z[row][col]
col+=1
row+=1
print n

Output:

## Example#8

Initialize the name of your 5 friends in the list and print 3rd character
# of each name

CODE:

names = [“”]*(5)
i = 0
while i<len(names):
names[i] = “”
i+=1
i = 0
while i<len(names):
names[i]= raw_input(“Enter name: “)
i+=1
i = 0
while i<len(names):
print names[i][2],
i+=1

Output:

Comment below if you have any query or confusion !!!