IF STATEMENT IN PYTHON

GOCOURSE

 

If Statement

          It is a selection statement, and a block of code is executed if the given condition is true.

Syntax:

if (condition):

    Code to be executed

Program 1:


 if(9<10):
     print("true")


Output:


 true


Elif Statement

"Try this condition if the previous conditions were false" is the command that Python uses with the elif keyword.

Syntax:

if(condition):
  code to be executed
elif(condition):
  code to be executed

Program 2:


 a=10
 b=9
 if(a<b):
     print("true")
 elif(a>b):
     print("false")


Output:


 false

Else Statement

         A block of code to be executed can be specified with the else statement. when the condition is not true.

Syntax:

If(condition):
  Code to be executed
else:
  Code to be executed

Program:


 subject1=int(input("Enter subject1 mark:"))
 subject2=int(input("Enter subject2 mark:"))
 total=subject1+subject2
 percentage=total/2
 if(percentage >90):
     print("Good mark")
 else:
     print("better luck next time")


Output:


 Enter subject1 mark:80
 Enter subject2 mark:75
 better luck next time




More topic in Python

Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send