12 Loop statements in python

 1. Using loops we can execute a block of code repeatedly


2. Python support 3 types of loops

    1. For loop    ==> for loop Iterate based on range

    2. While loop  ==> while loop Iterate based on condition

    3. nested loop ==> If a loop contains another loop that is called nested loop


3. Loop statements 

    1. break     ==> break used to terminates the loop

    2. continue  ==> continue used to skip the current iteration 

    3. pass      ==> 

        1. pass will do nothing 

        2. we use pass when statement is require to aviod syntax issues.

1) for loop with range

'''

1. Range() function used to craete a range of values

'''


#for n in range(8):        # 0 to n-1

 #  print(n)


#for n in range(3, 8):     # n to n-1

 #   print(n)


#for n in range(8, 15, 3):  # n to n-1 , step

#    print(n)


for n in range(5, 8):

    print(n) 


#for i in range(4, 8):

#    print(i)

#    if i > 4 : break



#for i in range(4):

    # if i : continue

    # print(i)


#for i in range(6):

    # if i >= 4 : continue

    # print(i)

    # if i == 2 : break


#for i in range(5):

    # pass


if 2 == 2: break

2) for loop with data types

'''

1. We can use all data types in for loop except number.(ex--25)

'''


## for loop with string

#s = 'umamahesh'

#for c in s:

 #   print(c)


## for loop with list

names = ['uma','mahesh','kumar','nagesh', 'sri']

for i in names:

    print(i)

    if (i == names[4]):

        print(i)

    #print(name)



# For loop with dictionary

#emp_det = {'name':'uma', 'eid':35, 'dname':'IT', 'name':'ram'}

#for k in emp_det:

    # print(k, emp_det[k])


#for k, v in emp_det.items():

    # print(k, v)


## For loop with number

#NO =  345

#for n in NO: print('OK')

3) while statement
'''
1. While loop Iterate based on condition
'''
n = 6
#while n > 3:
    # n -= 2
    # print(n)

#while n:
    # print(n)
    # n -= 2

sal = 500
while sal >= 300:
     print(sal)
     sal -= 50

3) nested statements
#for x in [1,2,3,4]:
     #print('----', x)
     #for c in 'SRC':
         #print('++++++++++++', c)


for x in [1,2,3]:
    print('----', x)
    for y in 'AB':
        print('=======>', y)
        for z in {'n':'uma',88:99}:
             print('++++++++++', z)
4) breaking and continue
The syntax of the break is given below.

#loop statements  
break; 

list =[1,2,3,4]  
  
for i in list:  
    if i == 3:  
        break
    print(i) 
 
# Using range function
for i in range(0,10):
if i==5:
break
print(i)


# Program based on string
str = "python"  
for i in str:  
    if i == 'o':  
        break  
    print(i);  

#Apply continue staement
list =[1,2,3,4]  
  
for i in list:  
    if i == 3:  
        continue 
    print(i)

# Program 2
i = 0                     
while(i < 10):                
   i = i+1  
   if(i == 5):  
      continue  
   print(i)  


#Python Pass
In Python, the pass keyword is used to execute nothing; 
it means, when we don't want to execute code, the pass can be used to execute empty. 
It is the same as the name refers to. 
It just makes the control to pass by without executing any code. 
If we want to bypass any code pass statement can be used.

# pass is just a placeholder for  
# we will adde functionality later.  
values = {'P', 'y', 't', 'h','o','n'}  
for val in values:  
    pass  

#Example usintg Pass statem

for i in [1,2,3,4,5]:   
    if(i==4):  
        pass  
        print("This is pass block",i)  
    print(i)  
 
5) loop questions
1. Use of loops ?
2. Types of loops in python ?
3. what is for loop ?
4. What is while loop ?
5. Difference between for loop and while loop ?
6. What is nested loop ?
7. What are the loop statements ?
8. Use of break ?
9. Use of continue ?
10. Use of pass ?
11. Use of range() function ?

Comments

Popular posts from this blog

1 PYTHON PROGRAMMING

16 file handling in python

4 Tuple data types