Conditional Statement in Python Language
Conditional Statement: The Python if statement is a statement which is used to test a specified condition.
We can use if statement to perform conditional operations in our python application. The if statement executes only when the specified condition is true. we can pass any valid expression into the if parentheses.
Conditional Statement in Python Laguage |
Types of Conditional statement in Python
If Statement in Python
If statement: 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.
if Syntax of Python
if(condition) { statements; } |
Example if Statement in Python
age=int(input("Enter Your Age:")) if (age>=18): { print("You are eligible to Vote for India") } **********OUTPUT********** Enter any number:18 You are eligible to Vote for India |
If Else Statement in Python
If else statement: 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 you will execute otherwise body of else execute.
if Else Syntax of Python
if condition : body _of_if else: body_of_else |
Example if Else statement in Python
age=int(input("Enter Your Age:")) if (age>=18): { print("You are eligible to Vote for India") } else: { print("You are eligible to Vote for India"); } **********OUTPUT********** Enter any number:18 You are eligible to Vote for India |
Example if Else Statement Even or Odd in Python
n=int(input("Enter Your Age:")) if (n>%2==0): { print("Number is Even:",n) } else: { print("Number is Odd:",n); } **********OUTPUT********** Enter any number:16 Number is Even:16 |
If Else if Ladder Statement in Python
If else if ladder statement: It is used to test the condition.
If executes only one condition at a time. The condition which is true first from the top will execute.it executes only one condition which becomes true first from the top. else part will execute if all the conditions are false.
if Else Syntax of Python
if condition : statement1 elif condition: statement2 elif condition: statement3 else: statement4 |
Example if elif Else statement in Python
per=float(input("Enter your percentage:")) if per>=60: print("First division") elif per>=45: print("Second division") elif per>=33: print("Third division") else: print("Sorry!!! You are fail.") **********OUTPUT********** Enter your percentage:56 Second division |
Nested If Statement in Python
Nested if statement: It is used to test the condition.One if inside another if is called nested if.
Nested if Syntax of Python
if condition : #statements if condition: #statements if condition: #statements |
Example if elif Else statement in Python
a=int(input("Enter First Number:")) b=int(input("Enter Second Number:")) c=int(input("Enter Third Number:")) if a>b: if a>c: print(a," a is greatest") if b>a: if b>c: print(b," b is greatest") if c>a: if c>b: print(c," c is greatest") **********OUTPUT********** Enter First Number:33 Enter Second Number:45 Enter Third Number:55 55 c is greatest |
0 Comments