Conditional Decision making in python

Decision making in python


 

If Condition


## if condition
user = int(input("enter a number: "))
if user % 2 == 0:
print('number is even')
else:
print("Number is odd")
print("program ended")



 

 

or

 
    ## or
 
    user = int(input("enter a number: "))
if user%2==0 or user > 5:
print('if code')
else:
print("else code")
print("program ended")



 
    ## or
    print("Program started")
user = int(input("enter a number: "))
if not(user%2==0 or user > 5 or user -20 > 0):
print('if code')
else:
print("else code")
print("program ended")






and

 
    ## and
user = int(input("enter a number: "))
if user%2==0 and user > 5:
print('if code')
else:
print("else code")
print("program ended")


 
 
 
 

not

 
    ## not
user = int(input("enter a number: "))
if not(user%2==0 and user > 5):
print('if code')
else:
print("else code")
print("program ended")





If condition is False then else part will be run

 
    ## if condition is False then else part will run.
 
    print("Program started")
user = int(input("enter a number: "))
if False:
print('if code')
else:
print("else code")
print("program ended")




If condition is True then if part will be run

 
    
    ## if condition is True then if part will run.
 
    print("Program started")
user = int(input("enter a number: "))
if True:
print('if code')
else:
print("else code")
print("program ended")






 

 

Nested if-else 

Find the biggest number.

 
   ## Nested if-else

a = 5
b = 15
c = 10

if a > b:
if a > c:
print("a is biggest")
else:
print("c is biggest")
else: # b > a
if b > c:
print("b is the biggest")
else:
print("c is the biggest")
 
    output: b is the biggest


 

another example:

 
    a = int(input("enter a number: "))
b = int(input("enter b number: "))
c = int(input("enter c number: "))

if a > b:
if a > c:
print("a is the biggest number.")
else:
print("c is the biggest number")
else:
if b > c:
print("b is the biggest number.")
else:
print('c is the biggest number.')

 output:
    enter a number: 5
    enter b number: 10
    enter c number: 15

    c is the biggest number.
 
 
 


 

 

 

 

 

 

 

 

 

 

 

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
إرسال تعليق (0)
أحدث أقدم