December 21, 2014

Making decision



The most basic thing you have to know when you start to learn a new language is making decisions. Through making decisions, it would help you to create a good code.
In Python, we would like to use "if", "elsif", to describe the situations, the following are the example that for your reference. 

l  Making decision


(1)    If……
var = 100

if ( var  == 100 ) : 
print "Value of expression is 100"

print "Good bye!"
(2)   if…else
var1 = 100
if (var1 == 100):
   print " true expression value"
   print var1
else:
   print " false expression value"
   print var1

var2 = 0
if (var2 == 1):
   print " true expression value"
   print var2
else:
   print " false expression value"
   print var2

print "Good bye!"
(3)   if....else if …….else
Core Python does not provide switch or case statements as in other languages, but we can use if..elif...statements to simulate switch case as follows:
var = 100
if (var == 200) :
   print " true expression value"
   print var
elif (var == 150) :
   print " true expression value"
   print var
elif (var == 100) :
   print "true expression value"
   print var
else:
   print " false expression value"
   print var

print "Good bye!"


No comments :

Post a Comment