Python - Branching Statements ✍️

Decision-making is the most important topic of all programming languages. It allows us to run a particular block of code for a particular decision. Python has a particular condition in the decision-making process. Condition checking is the backbone of a python project. Now, we discuss this topic very briefly. Now, I will teach you various examples and different methods of Decision-making.

Let's start,

The below diagram clearly understanding Control Structures or Decision-making processes. Now we will see the branching Statements types.

Python - Branching Statements

if condition

  • This statement is used to test the condition only.

Syntax

Python - Branching Statements

Flow Chart

Python - Branching Statements

A simple method of if statement

Input:

a=int(input("enter a number : "))
if a>0:
    print('postive values')

Output:
enter a number : 2
postive values
Input:

num=int(input("enter the number : "))
if (num>=0):
    print('whole number')

Output:
enter the number : 5
whole number

Input:

x=int(input("enter your age : "))
if (x>=18):
    print("you are eligible for voting")

Output:
enter your age : 21
you are eligible for voting

If statement using logical operators (and, or),

Input:

a = 200
b = 33
c = 500
if a > b and c > a: #AND
  print("Both conditions are True")
if a > b or a > c: #OR
  print("At least one of the conditions is True")

Output:
Both conditions are True
At least one of the conditions is True

If else condition,

  • This statement is used to test the condition.
  • When the condition is true, then a true statement is executed.
  • When the condition is false, then a false statement is executed.

Syntax

Python - Branching Statements

Flow Chart

Python - Branching Statements

Simple if-else statements

Input:

num=int(input("enter the age : "))
if (num>18):
    print('you can vote')
else:
    print('you are not eligible')

Output: 

#1st run
enter the age 25
you can vote

#2nd run
enter the age 12
you are not eligible

The direct condition using if-else 

Input:

a= int(input("Enter Id no :"))
if ( a==101) :
    print("Id is correct, please come")
else:
    print("Good bye!")

Output:

1st Run:
Enter Id no :101
Id is correct, please come

2nd Run:
Enter Id no.:206
Good bye!

String Matching using if statement

Input:

word1 = input("Enter any Word : ")
word2 = input("Enter another Word : ")

if sorted(word1) == sorted(word2):
    print("Both are same")
else:
    print("NOT same words")

Output:

1st Run
Enter any Word : hello
Enter another Word : hi
NOT same words

2nd Run
Enter any Word : magic
Enter another Word : magic
Both are same

Three marks comparison using logical operator (AND)

Input:

mark1 = int(input("\nEnter First mark : "))
mark2 = int(input("Enter Second mark : "))
mark3 = int(input("Enter Third mark : "))

if (mark1 == mark2) and (mark2 == mark3):
    print("All marks are same")
else:
    print("NOT all Three are same")


Output:

1st Run
Enter First mark : 56
Enter Second mark : 68
Enter Third mark : 78
NOT all Three are same

2nd Run
Enter First mark : 75
Enter Second mark : 75
Enter Third mark : 75
All marks are same

Finding Odd or Even

Input:

a = int(input("Enter the number : "))
if (a%2 == 0):
    print("The given number is EVEN")
else:
    print("The given number is ODD")

Output:

1st Run
Enter the number : 215634
The given number is EVEN

2nd Run
Enter the number : 54821
The given number is ODD

If elif else condition

  • Three statement - if
  • Checking another time - elif
  • False - else

Syntax

Python - Branching Statements

Simple program using elif

Input:

age=int(input('enter the age is : '))
if age <= 5:
    print("free ticket")
elif age <=18:
    print("ticket Rs: 10")
elif age <=30:
    print("ticket Rs: 20")
else:
    print("ticket Rs:30")

Output:

1st run:
enter the age is 5
free ticket

2nd run:
enter the age is 12
ticket Rs: 10

3rd run:
enter the age is 55
ticket Rs:30

if – elif - else using logical operator

Input:

age=int(input("Enter your age : "))

if age < 6:
    print('Hello little one')
elif age >= 6 and age < 10:
    print('Are you enjoying school?')
elif age >= 10 and age < 13:
    print('You are a Tween now')
elif age >= 13 and age < 20:
    print('Now you are officially a teenager')
else:
    print('Welcome to the real world')

Output:

1st Run:
Enter your age : 56
Welcome to the real world

2nd Run:
Enter your age : 5
Hello little one

3rd Run:
Enter your age : 10
You are a Tween now

Find the largest number using if-else statements

Input:

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
 
if (num1 > num2) and (num1 > num3):
   largest = num1
elif (num2 > num1) and (num2 > num3):
   largest = num2
else:
   largest = num3 
print("The largest number is", largest)

Output:

1st Run:
Enter 1st number: 7
Enter 2nd number: 68
Enter 3rd number: 48
The largest number is 68

2nd Run:
Enter 1st number: 99
Enter 2nd number: 47
Enter 3rd number: 83
The largest number is 99

we are discussing if - elif- else statements very clearly. The next topic is nested if-else statements.

Nested If statements

Input:

x = 12
if x > 10:
    print("Above ten,")
    if x > 20:
        print("and also above 20!")
    else:
        print("but not above 20")
else:
    print("bye")

Output:
Above ten,
but not above 20.

Eligibility for Aadhar Card - Nested if Statement

age = int(input("Enter your age : "))

if age > 5:
       nationality = input("Enter your Nationality : ")
       if nationality.upper() == 'INDIAN' :
              print("You are eligible for Aadhar card")
       else:
              print("Not for other nationalities")
else:
       print("Age criteria not met")

Output:

1st Run
Enter your age : 23
Enter your Nationality : INDIAN
You are eligible for Aadhar card

2nd Run
Enter your age : 4
Age criteria not met

3rd Run
Enter your age : 15
Enter your Nationality : GERMAN
Not for other nationalities

I think now, you clearly understand if-else (branching) statements. I gave many examples, if you have any doubt ask me anything. next session we will discuss looping statements.


Similar Articles