If Else Statement in Python programming | Hands on Coding

If else statement

Syntax:

if  condition  :
    body _of_if
else:
    body_of_else
  • It is used to test the condition and gives the output in both situation either condition true or false.
  • If the condition is true body of if will execute otherwise body of else execute.

no=int(input("Enter any number:"))
if no>5:
    print("Number is greater than 5")
else:
    print("Number is less than 5")
/*
Output
Enter any number:7
Number is greater than 5
*/



/*Example 1*/
if 10:
    print("Hello")
else:
    print("Hi")
/*
 ### Output ###
Hello
because 10 is non-zero value
*/
/*Example 2*/
if 0:
    print("Hello")
else:
    print("Hi")
/*
 ### Output ###
Hi
*/
/*Example 3*/
if 'A':
    print("Hello")
else:
    print("Hi")
/*
 ### Output ###
 Hello
 because ASCII value of A is 65
 which is non-zero
*/