Python Decision Making

In decision making statement the control will flow only in one direction. Based upon the condition it will execute either True block or False block, here condition means relation between two operators. Decision statement “control” the flow of execution, they are also known as control statement.

There are two block’s if block and else block, if part is a true block and else part is a false block. The condition and expression will be written in if block.

Some logical conditions which are commonly used in Decision making statements are,

Operator NameOperatorExample
Equals==a == b
Not Equals!=a != b
Less than<a < b
Less than or equal to<=a <= b
Greater than>a > b
Greater than or equal to=a >= b

Python provides following decision making statements.

StatementDescription
if statementIf the condition is true the block of code will be executed.
If else StatementIf the condition is true the true block will execute, if the condition becomes false the else block code will execute.
nested if statementsWhen an if statement contains another if statement then it is a nested if statement.

if statements

If is an conditional statement, if the condition becomes true then the block of code will be executed. In if statement we use if as keyword.

Syntax

if expression:
    statement  

Example:

a=30
b=20
if a>b:
 print("A is greater than B")

Output:

A is greater than B

if-else statement

If the condition becomes true the true block will execute, if the condition becomes false the else block will execute.

Syntax:

if condition:
    True block statements
else:
    False block statements 

Example:

a = 10
b = 20
if a > b:
 print("A is greater than B")
else:
 print("B is greater than A")

Output:

B is greater than A

nested if statements

When an if statement contains another if statement then it is a nested if statement. The nested if statement allows us to check multiple conditions and will execute based upon the condition.

Syntax:

if expression_1:
   statement
   if expression_2:
      statement
   elif expression_3:
      statement
else:
   statement

Example:

age = 21
if age < 18:
    print(" You are Minor and you are not eligible to work")
else:
    if age >= 18 and age <= 60:
        print(" You are Eligible to Work ")
    else:
        print(" You are too old to work as per the Government rules")

Output:

You are Eligible to Work

More Reading

Post navigation

2 Comments

  • That is very fascinating, You’re an overly skilled blogger. I have joined your feed and look forward to seeking extra of your fantastic post. Also, I’ve shared your site in my social networks!

  • Hey very nice website!! Man .. Beautiful .. Amazing .. I’ll bookmark your blog and take the feeds also厈I am happy to find a lot of useful information here in the post, we need work out more techniques in this regard, thanks for sharing. . . . . .

Leave a Reply

Your email address will not be published. Required fields are marked *