Wednesday, June 22, 2011

Difference between continue and pass in python

Pass is a "do-nothing" statement. Best used with a if-else or try-except condition

Continue means "jump to start of loop", ignoring rest of the statements in the loop

Code Output

for r in [1,2,3]:
   print(r)
   continue
   print(r,"\tagain")

1
2
3

for r in [1,2,3]:
   print(r)
   pass
   print(r,"again")



1
(1, 'again')
2
(2, 'again')
3
(3, 'again')



for r in [1,2,3]:
   if r%2!=0:
      print(r)
   else:
      pass


1
3


for r in [1,2,3]:
   if r%2==0:
      continue
   print(r)


Bad coding practice according to me but nevertheless this is possible.


1
3

No comments:

Post a Comment