article

Tuesday, May 29, 2018

Python IF and Else Condition Example code

Python IF and Else Condition Example code
 
#!/usr/bin/python
var = 100
if ( var  == 100 ) : print "Value of expression is 100"
print "Good bye!"

if 2 + 2 == 4:
 print "2 and 2 is 4"
 print "Arithmetic works."
else:
 print "2 and 2 is not 4"
 print "Big Brother wins."
 #--------------------------------------------------------------
num = 15
if num < 10:
 print "number is less than 10"
elif num > 10:
 print "number is greater than 10"
else:
 print "number is equal to 10" 
 #-------------------------------------------------------------- 
if 1 < 2:
 print "1 is less than 2"
elif 3 < 4:
 print "3 is less than 4"
else:
 print "Who moved my cheese?" 
 #-------------------------------------------------------------- 
want_cake = "yes"
have_cake = "no"
if want_cake == "yes":
  print "We want cake..."
  if have_cake =="no":
      print "But we don't have any cake"
elif have_cake == "yes":
 print "And it's our lucky day"
else:
 print "The cake is a lie."
#------------------------------------------------------------
phrase = "it marks the spot"
for letter in phrase:
 if letter == "X":
  break
else:
 print("There was no 'X' in the phrase")
#---------------------------------------------------------------
tries = 0
while tries < 3:
 password = input("Password: ")
 if password == "I<3Bieber":
  break
 else:
  tries = tries + 1
else:
 print("Suspicious activity. The authorities have been alerted.")
#---------------------------------------------------------------
if age > 18:
    print "adult person"

for i in range(5):
    print i 
#---------------------------------------------------------------
age = 17
if age > 18:
   print "Driving licence issued"
else:
   print "Driving licence not permitted"
#---------------------------------------------------------------
name = "Luke"
if name == "Jack":
   print "Hello Jack!"
elif name == "John":
   print "Hello John!"
elif name == "Luke":
   print "Hello Luke!" # Hello Luke!
else:
   print "Hello there!"   
#---------------------------------------------------------------
grades = ["A", "B", "C", "D", "E", "F"]
grade = "L"
if grade not in grades:
   print "unknown grade" # unknown grade
#---------------------------------------------------------------
sex = "M"
age = 26
if age < 55 and sex == "M":
   print "a young male" # a young male
#---------------------------------------------------------------
name = "Jack"
if ( name == "Robert" or name == "Frank" or name == "Jack" 
      or name == "George" or name == "Luke"):
   print "This is a male"   
#---------------------------------------------------------------
x = 10
y = 0
if (y != 0 and x/y < 100):
   print "a small value"   
#---------------------------------------------------------------
x,y =10,40
if(x < y):
   st= "x is less than y"
else:
   st= "x is greater than y"
print (st) 
#---------------------------------------------------------------
total = 500
#country = "US"
country = "AU"
if country == "US":
    if total <= 50:
        print("Shipping Cost is  $50")
elif total <= 100:
        print("Shipping Cost is $25")
elif total <= 150:
     print("Shipping Costs $5")
else:
        print("FREE")
if country == "AU": 
   if total <= 50:
     print("Shipping Cost is  $100")
else:
     print("FREE")
#---------------------------------------------------------------
a = 100
b = 100
if b > a:
  print("b is greater than a")
elif a == b:
  print("a and b are equal")
else:
  print("a is greater than b")
#---------------------------------------------------------------
a = 200
b = 33
c = 500
if a > b and c > a:
  print("Both conditions are True") 
#---------------------------------------------------------------
a = 6
b = 5
 
# Basic comparisons
if a < b:
    print("a is less than b")
 
if a > b:
    print("a is greater than b")
 
print("Done")

Related Post