Python: Decision Making

Every day we make decisions. When I get up in the morning, I need my shake. Now, for the shake, I need 500 ml Milk, 100 gms of Oats, 1 Banana, 1 Kiwi, and 1 Apple. In case, I don’t have any one of these, it can spell trouble. What’s the trouble ? I cannot make my shake.

In programming, we have a similar structure to situations such as these. Decision making is done with a set of statements called if, else-if, and else.

If: If a certain condition is met, we perform a block of code.
Else-if: If the IF condition is not met, we perform the code written in else-if
Else: If the IF condition, and the ELSE-IF condition are both not met, we perform this block of code.

Sr.No.Statement & Description
1An if statement consists of a boolean expression followed by one or more statements.
2An elif statement is python’s way of saying “if the previous conditions were not true, then try this condition”.
3The else keyword catches anything which isn’t caught by the preceding conditions.

A more picturesque, and descriptive version can be seen in this image below: An image representing if-else statements. The order of preference is 1, 2, and then 3 .

IF statement


x = 33
y = 200
if y > x:
  print(“y is greater than x”)


y is greater than x

Please note the indentation. In Python, we use indentations as a way of saying that the code which follows the statement is enclosed. In other programming languages, we tend to use braces, or a parenthesis for the same. In python, things are slightly simpler, and we use indentations.

ELIF statement


x = 56
y = 56
if x > y:
  print(“x is greater than y”)
elif x == y:
  print(“x and y are equal”)


x and y are equal

From the above example, we can see that because a and b both have equal values, the first block was not satisfied. Instead, the second block of code under elif, has been executed.

ELSE statement


x = 100
y = 50
if x > y:
  print(“x is greater than y”)
elif x == y:
  print(“x and y are equal”)
  print(“x is greater than y”)


x is greater than y

Python Technology Tutorials

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: