IF ELSE, FOR ELSE, TRY ELSE......
IF ELSE :
We will see the different ways of using else keyword..
As all we know else can be used with If straight forward if flag is True the “Its True” will be printed else "Its false" will be printed.
print "Its True”
print "Its false"
FOR ELSE :
The second form of else usage will be with for loop
When else will be executed?
Else in for loop will be executed after the for, but only if the for loop terminates normally , ie with out any break.
In the below example when i=3 for loop will break so else will not be executed.
for i in a:
print "i = ",i
if i == 3:
print "inside else"
TRY ELSE :
The third form of using else is with try statement.
Else can be used with try when you need to catch specific Exceptions and to execute some code when other type exceptions are raised.
In example else will be executed only if exception is not raised.
If exception is raised else will not be executed..
When to use this else facility, its up to your implementation
print "In try"
something = a
print "In exception"
print "In else"
print "normal code flow"
Happy python :)