Python if, if-else statements

Author: Al-mamun Sarkar Date: 2020-03-24 20:43:50

Python If statement is used to perform some task based on a specified condition. Python if, if-else statements. Applying conditions using if-else statements.

 

Use of if-else:

a = 6
if a < 10:
    print("hello")
    print(a)


if a < 10:
    print('a is less than 10.')
else:
    print('a is greater than 10.')

Output:

hello
6
a is less than 10.

 

Short Hand if-else (Ternary Operators):

x = 10
y = 30
print("X") if x > y else print("Y")

 

Nested if statement:

if a < 10:
    if a == 5:
        print("Welcome")
    else:
        print("Go home")