# Loops in Python

Python has two kinds of loops

The ‘for loop’

Using for loop we can iterate through the items present in any iterable sequence.

Example

``````  nums = [1,2,3,4]
for num in nums:
print(num)

``````

output:

``````1
2
3
4
``````

To iterate through a sequence of numbers we can use range and xrange functions

Example

``````# Prints out the numbers 0,1,2,3,4
for x in range(5):
print(x)

# Prints out 3,4,5
for x in range(3, 6):
print(x)

# Prints out 3,5,7
for x in range(3, 8, 2):
print(x)
``````

output:

``````0
1
2
3
4
3
4
5
3
5
7
``````

The ‘while loop’

While loop keeps on repeatedly executing the block of code till the condition in the while loop is rendered false.

``````# Prints out 0,1,2,3,4
count = 0
while count < 5:
print(count)
count += 1  # This is the same as count = count + 1
``````

output:

``````0
1
2
3
4
``````
``` ''' in physics, contact between objects during a collision leads to loss of force as no medium is perfect. likely we are having n medium in a plane each of equal length followed by qouitient of reduction, In can be well explained using the following example person throws a ball on plane from certain height h of n medium your job is to find distance traveled in plane and the medium is repetted in cyclic order and height is only considered in integers input: 3(number of medium) 4(length of each) 5(height from which ball is thrown) 2(distance travelled for each step) 0.1 0.3 0.5 output: first step 5*0.9= 4 dist = 2 second step 4*0.9= 3 dist = 4 third step 3*0.7= 2 dist = 6 fouth step 2*0.7= 1 dist = 8 fifth step 1*0.5= 0 dist = 10 so,the result is 10(ten) steps Input enter total number of mediums: 3 enter length of medium: 3 enter height from which ball is thrown: 10 enter distance travelled in each step: 4 now enter qoutients of loss for 3 mediums: 1 medium :0.2 2 medium :0.3 3 medium :0.4 Output distance traveled by ball is 24 ''' msg = 'enter total number of mediums: ' print(msg) k = int(raw_input().strip()) msg = 'enter length of medium: ' print(msg) l = int(raw_input().strip()) msg = 'enter height from which ball is thrown: ' print(msg) h = int(raw_input().strip()) msg = 'enter distance travelled in each step:' print(msg) d = int(raw_input().strip()) print('now enter qoutients of loss for %d mediums:'%k) kq =[] for i in range(0,k): #appending into list, the converted float from input kq.append(float(input('%d medium :'%(i+1))))#input can print messages given as parameters h1 = h d1 = 0 while h1!=0: fl = k*l for i in range(0,k): #for medium check if (h1!=0): #height not equal to zero tt = d1%fl #medium repeatition check d1=d1+d #distance increament per step h1 = int(h1*(1-kq[int(tt/l)])) print('distance traveled by ball is %d'%d1) ```

There are statements that help us to have more control over the way loops work.

The break statement

When python comes across break statement when executing a loop, it stops the execution of both the iteration and the loop

``````# Prints out 0,1,2,3,4

count = 0
while True:
print(count)
count += 1
if count >= 5:
break

# Prints out only odd numbers - 1,3,5,7,9
for x in range(10):
# Check if x is even
if x % 2 == 0:
continue
print(x)
``````

output:

``````0
1
2
3
4
1
3
5
7
9
``````

The continue statement

When python comes across the continue statement when executing a loop, it stops the ongoing iteration and continues executing the loop.

## can we use "else" clause in loops?

unlike languages like C,CPP.. we can use else for loops. When the loop condition of "for" or "while" statement fails then code part in "else" is executed. If break statement is executed inside for loop then the "else" part is skipped. Note that "else" part is executed even if there is a continue statement.

``````# Prints out 0,1,2,3,4 and then it prints "count value reached 5"

count=0
while(count<5):
print(count)
count +=1
else:
print("count value reached %d" %(count))

# Prints out 1,2,3,4
for i in range(1, 10):
if(i%5==0):
break
print(i)
else:
print("this is not printed because for loop is terminated because of break but not due to fail in condition")
``````

output:

``````0
1
2
3
4
count value reached 5
1
2
3
4

``````