Python if, if-else statements

Python if, if-else statements

Instructor-svg Al-Mamun Sarkar
Mar 24 , 2020

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")

 

 

  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram