Loop in Python


We can categories the Python loops in mainly three types

Loops
Python provides three ways to write loops:
  1. For
  2. While
  3. Nested Loop



##For Loops
for x in collection:
          print x


##
Nested
   for item_var in sequence:
   for itemm1_var in sequence:
      statements(inner loop)
   statements(outer loop)



##While

while expression:
   statement(s)

j=2
while(j <= 20):
          j=j+2
          print j

##
Nested
   while expression:
while expression:
   statement(s)
      statement(s)



Loop Control Statement
  1. Break
  2. Continue
  3. Pass


#Break
while(j <= 20):
          j=j+1
          if(j%2==0): break
                   print j
 # continue
 while(j <= 20):
          j=j+1
          if(j%2==0): continue
                   print j

#Pass
languages = ["C", "C++", "Perl", "Python"]
   for wd in languages:
   if wd == 'C++':
      pass
      print 'This is pass section'
   print 'Current Word :', wd

Recursion  
An recursion is a special type of function which call self content block auto  in particular action

# Simple

def factorial(n):
    if n == 1:
        return 1
    else:
        return n * factorial(n-1)

factorial(5)

Loop in Python Reviewed by Rupesh on 00:46 Rating: 5

No comments:

All Rights Reserved by Technology from Developers Eye © 2014 - 2015
Powered By Blogger, Designed by Aadics
Disclaimers:: The information provided within this blogsite is for general informational purposes only. While we try to keep the information up-to-date and correct, there are no representations or warranties, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the information, products, services, or related graphics contained in this blogsite for any purpose.The author does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause.

Contact Form

Name

Email *

Message *

Powered by Blogger.