Monday, November 30, 2015

Python Conditional Statements

1. Basic IF Statement :
if condition:
    Block 
    statements 

Example:
1
2
3
4
5
6
if True:
    print "its True Block"
if 10:
    print "its True Block"
if 1<2:
    print "Yes its True 1 is lesser then 2"
2. IF.. Else... Statement
if condition:
    True Block
else:
    False Block
Example:
a=1
b=2
if a<b:
    print "its b is big"
else:
    print "its a is big"

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
if True:
    print "its True Block"
 
if not 23:
    print "Yes i am not 23"
else:
    print "Yes i am 23"

if None:
    print "Not none"
else:
    print "I am NOne"

3. IF.. Elif...elif... Statement

if condition1:
    Cond1 Block 
elif Condition2:
    Cond2 Block
elif Condition3:
    Cond3 Block 
 .
 .
4. IF.. Elif...elif...else... Statement

if condition1:
    Cond1 Block 
elif Condition2:
    Cond2 Block
elif Condition3:
    Cond3 Block 
 .
 .
else:
    Else Block
Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
a=int(raw_input("Enter a value"))
b=int(raw_input("Enter b value"))
c=int(raw_input("Enter c value"))

if a<b and b<c:
    print "C is big"
elif a>b and a>c:
    print "A is big"
else:
    print "B is big"
5. Nested IF.. Elese Statement
if Condition1:
    if Condition2:
     Condition2 True Block 
 else:
     Condition2 False Block 
else:
    if Condition3:
     Condition3 True Block 
 else:
     Condition3 False Block
Example:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
if a<b:
   if b<c:
       print "C is big"
   else:
       print "B is big"
else:
   if a>c:
       print "A is Big"
   else:
       print "C is big"

No comments:

Post a Comment