Else statement
The else statement allows us to do something if our test returns false.
if A==5:
print "equal"
else:
print "Not equal"
So now what we have is a decision. If the test A=5 is true, we display "equal". If the test A=5 is false, we will display "not equal". The else basically says that if the test is false then we must do something else.
Even more useful is the fact we can do further tests using the else statement.
if A==5:
print "equal to 5"
elif A ==4:
print "equal to 4"
else:
print "not equal to 5 or 4"
We can chain as many else if's together as we wish. The indentation of the code helps us to keep track of all these else if's
Custom Search