How To Use IF Else Statement in Python

How To Use IF Else Statement in Python
How To Use IF Else Statement in Python


How To Use IF Else Statement in Python

if statement: A if statement is used to test the condition and the result is based on the condition in python language.
When if the condition is true its body will execute otherwise does not execute.


Syntax of if Statement

if (condition) :


  statement



How To Use if Statement in Python?

age=int(input("Enter Your Age:"))


if(age>=18):


           print("You can Drive Bike")   

  

*****OUTPUT*****


Enter Your Age:21


You can Drive Bike



if-else statement: 
A if-else statement is used to test the condition and gives the output in both situations either condition true or false.

When if the condition is the true body of if will execute otherwise the body of else execute.

Important Same Others If Else Statement

👉C++ If-Else Statement 

👉Java If-Else Statement 

👉C If-Else Statement


Syntax of if-else Statement

if (condition) : 


      Statement 


else: 


   Statement



How To Use if-else Statement in Python?

age=int(input("Enter Your Age:"))


if(age>18):


    print("You can Drive Bike")


else:


    print("You can not Drive Bike")    

 

*****OUTPUT*****


Enter Your Age:15


You can not Drive Bike



if-else-if-Ladder Statement: 
A 
if-else-if statement is used to test the condition.

If executes only one condition at a time.
When condition which is true first from the top will execute, otherwise else executed.


Syntax of if-else Statement

if (condition):


   statement1


elif(condition):


   statement2


elif(condition):


   statement3


else:


   statement4



How To Use if-else-if Statement in Python?

age=int(input("Enter Your Age:"))


if(age<=15):


    print("You are kid")


elif(age<=17):


    print("You are no Young")


elif(age>=21):


    print("You can Drive Bike")


else:


    print("You can not Drive Bike")     


*****OUTPUT*****


Enter Your Age:21


You can Drive Bike



Nested if statement: 
Nested if statement means one inside another so one if inside another if is called nested if statement.

it is used to test the condition one if inside another if is called nested if.


Syntax of Nested if Statement

if (condition):


      #statements


if(condition):


      #statements


if(condition):


       #statements



How To Use Nested if Statement in Python?

print("Enter one Number")


x=int(input())


print("Enter second Number")


y=int(input())


print("Enter third Number")


z=int(input())


if(x>y):


    print("x number is grater:",x)


if(y>x):


    if(y>z):


        print("y number is grater:",y)


if(z>x):


    if(z>y):


        print("z number is grater:",z)   


*****OUTPUT*****


Enter one Number


23


Enter the second Number


54


Enter the third Number


67

z number is greater: 67


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.