# Python if else Statement

If else statement is used when both the true and false parts of a given condition are specified to be executed. When the condition is true, the statement inside the if block is executed; if the condition is false, the statement outside the if block is executed.

##### Syntex of python if else statement

**if:**condition:

statement 1

statement 2

statement..n

else:

statement 1

statement 2

statement..n

**The block of statement ( after : colon ) is executed if condition is TRUE else statement will be executed**

**Note:**"To overcome the drawbacks of simple if statement/one way selection we use two way selection statement known as if-else statement."

##### Q.) W.A.P to print the greater number using simple if-else statement.

a=int(input("Enter first Number")) b=int(input("Enter Second Number")) if a>b: print("A is gratest no") else: print("B is Gratest no")

##### Output

4

B is greater no

##### Program Explanation

**Step 1:-**

We declared variables called a and b.

**Step 2:-**

A Message will give for entering their own values for a and b:- Enter First number

Enter Second Number
**Step 3:-**

User Enter two integer No Let`s We Enter :- 6 3

**Step 4:-**

next If condition checks whether a is greater than b or not.
If this condition is True, then it will return a is greater than b.
if the condition is not true then it will return a is not greater than b

hear we used the Conditional Operator(i.e > greater than) to check whether a is greater than b or not. If this condition is True, then it will be executed if part, which is variable an (a is greater than b). If the first condition fails, then it will be executed else part, which will give output as b is greater than a

**Step 5:-**

in this program, the condition is true because a is greater than b os if a part will be executed because we have enter 6 and 3 i.e 6 is greater than 3

if we enter 3 and 6 then 3 is less than 6 so else part will be executed

**Step 6:-**

So Output will be :- a is grater than b

##### Q.) W.A.P to Enter Two no and Check both are Equals or not.

a=int(input("Enter first Number")) b=int(input("Enter Second Number")) if a==b: print("Both are equals") else: print("both are not equals")

##### Output

Both are not Equals

##### Program Explanation

**Step 1:-**

We declared two variables called a and b.

**Step 2:-**

Message will give for enter their own values for a and b:- Enter First number.

Enter Second number
**Step 3:-**

User Enter two integer No Let`s We Enter :- 4 5

**Step 4:-**

First C Programming If condition checks whether a is equal to b or not. If this condition is True, then it will return both are Equal.
if the condition is false then it will be executed else part

hear we used the Conditional Operator(i.e equals to ==) to check whether a is equals to b or not. If this condition is True, then it will be executed if part, which is variable a (a is equal to b). If the first condition fails, then it will execute else part, which will give output as a is not equal to b

**Step 5:-**

Hear,else part will be executed because we have enter 4 and 5 i.e both are not equals

if we enter 4 and 4 than both numbers are equals so it will give output as both numbers are equals
**Step 6:-**

So Output will be :- both are not equls

##### Q.) W.A.P to Enter a no and check Even or Odd

n=int(input("Enter a Number")) if n%2==0: print(n,"is even number") else: print(n,"is odd number")

##### Output

Even

##### Program Explanation

**Step 1:-**

We declared a variables called n that will store number .

**Step 2:-**

Message will give for enter their own values for n:- Enter a No.

**Step 3:-**

User Enter a integer No Let`s We Enter :- 4

**Step 4:-**

as we know even numbers are divisible by 2 remaining of 0 is called an even number.

and we also know that for storing remainder we use % (modulus operator)
next If condition checks whether n is divisible by 2 or not.
If this condition is True, then it will return even else odd.

hear we used the Conditional Operator(==) and arithmetic operator (%) for store remainder to check whether the remainder is zero or not. If this condition is True, then it will be executed if part, which is even. If the first condition fails, then it will execute else part, which will give output as odd

**Step 5:-**

in this program, condition is true because 4 is even of if a part will be executed because we have enter 4 i.e 4 is even

if we enter 5 then output will be odd so else part will be executed

**Step 6:-**

So Output will be :- even

##### Q.) W.A.P to Enter a no and check divisible by 3 or not.

n=int(input("Enter a Number")) if n%3==0: print(n,"is divisible by 3") else: print(n,"is not divisible by 3")

##### Output

divisible by 3

##### Program Explanation

**Step 1:-**

We declared a variables called n that will store number .

**Step 2:-**

Message will give for enter their own values for n:- Enter a No.

**Step 3:-**

User Enter a integer No Let`s We Enter :- 15

**Step 4:-**

we know that for storing remainder we use % (modules operator)
next If condition checks whether n is divisible by 3 or not.
If this condition is True, then it will return divisible by 3 else not divisible by 3.

hear we used the Conditional Operator(==) and arithmetic operator (%) for store remainder to check whether the remainder is zero or not. If this condition is True, then it will be executed if part, which is divisible by 3. If the first condition fails, then it will execute else part, which will give output as not divisible by 3

**Step 5:-**

in this program, condition is true because 15 is divisible by 3 if part will be executed because we have entered 15 i.e 15 is divisible by 3

if we enter 16 then output will be not divisible by 3 so else part will be executed

**Step 6:-**

So Output will be :- divisible by 3

Copyright © 2022 Shineskill Software Pvt. Ltd., All rights reserved.