Ad Code

Responsive Advertisement

Decision making if-else statement in Python 3 | Learn Python for free.

Decision Making statements in Python 3

So in last topic we have learned what are the if-statements and today we are going to learn the if-else statement. What are if-else statement and also solve some programs on if-else statement.

What are decision making statements in Python 3?

The order of execution of the program is known as the flow of control and decision making statements are used to control the flow of execution, it will depend upon the conditions we apply.

If-else statement-:

'if' statement alone will execute the statement if 'if' condition is true but what if we want to print some statement if 'if' condition is false.

So here we have if-else condition in which else condition will execute the statement if 'if' condition is false.

Note-: If statement requires condition but else do not.

What is if-else statement?

If is a keyword followed by the condition and ended with Colon (:)

Same as else is a Keyword but it does not take any condition, it also ends up with colon(:).

For example-:

if condition :
     Statement (s)
else:
     Statement (s)


How if-else Conditions works?


  • If the 'if' condition is true then the indented system gets executed.
  • But if the 'if' condition is not true then the else condition will work.
  • There is no limit on the number of the statements and statement can appear under an if and else block.

How we use if-else statement in a program?

  • We can also use () brackets outside the condition but it is not mandatory.
  • If condition requires condition but else condition do not.
  • The statements are written inside the if and else statement after 4 spaces.
  • The statement would be printed if the if statement is true or else statement is true.
  • Otherwise it will skip the block of statement.

Let us see how if-else statement works through flowchart -:


Now look at the example to understand if-else statement in Python 3.

Program 1 .Write a program to check whether the input number is Even or Odd?

num=int(input("Enter the Number : "))
if num % 2 == 0 :  # here number is divided by 2 and if remainder is 0 than it will work
   print(num,"  is Even")
else  :
   print(num,"  is Odd")   


To learn the modulus function click here

Output of the program-:

If input is = 6


6 is Even

if input is =  9


9 is Odd

  • Statement #1 ask the user to input the number.
  • Statement #2 is the test expression where it check whether the condition output is 0 or not.
  • If Statement #2 is correct the it will be executed and it will print 'num' is Even.
  • If statement #2 is incorrect then it else statement work and it will print 'num' is Odd .

Program 2 .Write a program to determine whether the person is eligible to vote or not?

age  = int(input("Enter the age of a Person : ")) # stat-1
if age>=18 :    # stat-2
   print("You are  eligible to vote")
else:
   print("You are not eligible to Vote ")

Output of the program-:

If input is = 10


You are not eligible to Vote

if input is =  20


You are  eligible to vote


Let us see how this program work-:


  • Statement #1 ask the user to input the age of a person
  • Statement #2 is the test expression where it check whether the ages greater than equal to 18 or not.
  • If Statement #2 is correct the it will be executed and it will print that you are eligible to vote.
  • If statement #2 is incorrect then it else statement work and it will print You are not eligible to vote.

Learning Tip :

Statement or statements indented inside if statement are executed only if, if condition evaluates to true otherwise the control skips the block of statement inside if and control goes to the outside of the if block.

Some Questions for you based on the if-statement.


1. Write a Program to print fail in number of a student is less than 33 out of 100.

2. Write a program to print your name if your first letter is input ? for eg. [ my name is Piyush so it will print Piyush of i enter P ].


If you have any problem you can comment below.




Post a Comment

0 Comments