6. Python If Conditions

Decision making is needed when you want to execute a block of code on particular condition, only if that condition is satisfied.

For decision making we're going to use Python if..elif..else statement.

6.1 Python if Statement


if condition:
    statement1
    statement2
    ...............


The program evaluates the condition. When the condition is satisfied or True the statement1, statement2, and etc will be executed, but when the expression is False then the statement will be not executed. Example
n = -3

if n % 2 != 0:
    print(n,"is odd number.")

print("After if statement")

if n > 0:
    print(n,"is postive.") 
print("After if statement")
Remember that condition with non zero value Python interpereter will treat it as True, while None and 0 are treated as False. Example
if None:
    print("This block will not executed.")
n = 0
if n:
    print("This block will not executed.")
print("This is the only one will be printed.")

6.2 Python if..else Statement

if condition:
    if_statement
    ..............
else:
    else_statement

    ..............
In Python if..else statement, if the condition evaluate to True then the block if statement will be executed otherwise the else statement is executed. Example
n = 3
if n % 2 == 0:
    print(n, "is even number")
else:
    print(n, "is odd number")

a, b = 5, 7
if a > b:
    print(a, "is greater than", b) 
else:
    print(b, "is greater than", a)     
You can make the program above with one line using Python ternary operator. Example
n = 3
if n % 2 == 0:
    print(n, "it's even")
else:
    print(n, "it's odd")

a, b = 5, 7
if a > b:
    print("a is greater") 
else:
    print("b is greater")  

print("Using Python ternary operator:")
print("it's even") if n % 2 == 0 else print("it's odd")
print("a is greater") if a > b else print("b is greater")

6.3 Python if..elif..else Statement

if condition:
    block_statement
elif condition:
    block_statement

......
......
elif condition:
    block_statement
else:
    block_statement


In Python elif is short of else if. It allows the program to evaluate conditions.

When condition if is False then the elif block_statement will be executed, but if all elif condition turn to be False then the block else statement will be executed. Example
# Try these options
# ch = 'a'
# ch = 'b'
# ch = 'c'
ch = 'd'

if ch == 'a':
    print("it's 'a'")
elif ch == 'b':
    print("it's 'b'")
elif ch == 'c':
    print("it's 'c'")
else:
    print("it's 'd'")

6.4 Python Nested if Statements

When there are some if_elif_else statements inside if_elif_else statement, it is called nested if Statement. Example
n = -35
if n > 0:
    if n % 2 == 0:
        print("n is positive and even number.")
    else:
        print("n is positive and odd number.")
else:
    if n % 2 == 0:
        print("n is negative and even number.")
    else:
        print("n is negative and odd number.")

Subscribe to receive free email updates:

1 Response to "6. Python If Conditions"

  1. JAMMY casino bonus code - jtmhub.com
    JAMMY casino bonus 보령 출장안마 code, Promo Code, Cashback Offer, No Deposit Bonus 시흥 출장마사지 and 익산 출장안마 Free Spins 영주 출장안마 No Deposit Bonus 2021. 안양 출장마사지

    ReplyDelete