If Statement in Python

 

If statement

Syntax:

if  condition  :
    body 
  • It is used to test the condition and the result is based on the condition.
  • If the condition is true its body will execute otherwise does not execute.

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

Here in this program we will use single variable for number , After taking user input we will apply condition on it. if the given number is greater than 0 then it will definitely positive , if the given number is less than 0 we will print negative and at last if the number is equal to 0 then we will print Number is Zero.

no=int(input("Enter any number:"))
if no > 0:
    print("Number is positive")
if no < 0:
    print("Number is negative")
if no == 0:
    print("Number is Zero")
"""
###Output###
Run 1
Enter any number:5
Number is positive
Run 2
Enter any number:-9
Number is negative
"""