Loop in Python
We can categories the Python loops in mainly three types
Loops
Python
provides three ways to write loops:
- For
- While
- Nested Loop
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
- Break
- Continue
- 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:
No comments: