Saturday, July 11, 2015

Python on the go - Revision of Conditional Execution

There are times in a program when there is a need to execute certain instructions after a certain condition is met  and this process may be carried out by using   "if"  statements as shown:-


num = raw_input('Input a number? ')
num2=float(num)
if num2>0:
   print "The number is positive"
elif num2==0:
     print "The number is zero"
else:
     print "The number is negative"


elif is an abbreviation for  "else if"  and it can be used more than once. else is used for the possible remaining conditions and normally used at the end of the "if" statements.  However, there does not need to be one.


No comments:

Post a Comment