Python Decision Making | If else in Python

Sometimes we want to play pubg on our phone if the day is Sunday.

Sometimes we order Ice-cream online if the day is sunny.

Sometimes we go hiking if our parents allow.

All these are decisions that depend on the condition being met.

In python programming too, we must be able to execute instructions on a condition(s) being met. This is what conditions are for!

Decisions in a program are used when the program has conditional choices to execute a code block. Let’s take an example of traffic lights, where different colors of lights lit up in different situations based on the conditions of the road or any specific rule.

Python provides various types of conditional statements:

StatementDescription
if StatementsIt consists of a Boolean expression which results are either TRUE or FALSE, followed by one or more statements.
if else StatementsIt also contains a Boolean expression. The if the statement is followed by an optional else statement & if the expression results in FALSE, then else statement gets executed. It is also called alternative execution in which there are two possibilities of the condition determined in which any one of them will get executed.
Nested StatementsWe can implement if statement and or if-else statement inside another if or if – else statement. Here more than one if conditions are applied & there can be more than one if within elif.

The decision-making structures can be recognized and understood using flowcharts.

Python If Statement

Syntax:

if expression:
   #execute your code

Example:

a = 15

if a > 10:
   print("a is greater")

Output:

a is greater

Python if else Statement

Syntax:

if expression:
   #execute your code
else:
   #execute your code

Example:

a = 15
b = 20

if a > b:
   print("a is greater")
else:
   print("b is greater")

Output:

b is greater

Python elif Statements

elif – is a keyword used in Python replacement of else if to place another condition in the program. This is called chained conditional.

Syntax:

if expression:
   #execute your code
elif expression:
   #execute your code
else:
   #execute your code

Example:

a = 15
b = 15

if a > b:
   print("a is greater")
elif a == b:
   print("both are equal")
else:
   print("b is greater")

Output:

both are equal

If else and elif in Python

If else and elif statements are a multiway decision taken by our program due to certain conditions in our code.

Syntax:

'''
if (condition1):		// if condition 1 is true
	print(“yes”)
elif (condition2):		// if condition 2 is true
	print(“No”)
else:				// otherwise
	print(“May be”)
'''

Code example:

a = 22
if (a>9):
    print(“Greater”)
else:
    print(“lesser”)

Quick Quiz: Write a program to print yes when the age entered by the user is greater than or equal to 18.

Single Statement Condition

If the block of an executable statement of if – clause contains only a single line, programmers can write it on the same line as a header statement.Example:

a = 15

if (a == 15): print("The value of a is 15")

Relational Operators

Relational operators are used to evaluate conditions inside if statements. Some examples of relational operators are:

= = -> equals

>=  -> greater than/equal to

<=, etc.

Logical Operators

In python, logical operators operate on conditional statements. Example:

and -> true if both operands are true else false

or -> true if at least one operand is true else false

not -> inverts true to false and false to true

elif in python means [else if]. If statement can be chained together with a lot of these elif statements followed by an else statement.

Python elif clause

'''
if (condition1):
    #code
elif (condition 2):
    #code
elif (condition 2):
    #code
….
else:
    #code    '''

  • The above ladder will stop once a condition in an if or elif is met.

Important Notes:

  • There can be any number of elif statements.
  • Last else is executed only if all the conditions inside elifs fail.

Exercise On If Else

  1. Write a program to find the greatest of four numbers entered by the user.
  2. Write a program to find out whether a student is pass or fail if it requires a total of 40% and at least 33% in each subject to pass. Assume 3 subjects and take marks as an input from the user.
  3. A spam comment is defined as a text containing the following keywords:

“make a lot of money”, “buy now”, “subscribe this”, “click this”. Write a program to detect these spams.

  1. Write a program to find whether a given username contains less than 10 characters or not.
  2. Write a program that finds out whether a given name is present in a list or not.
  3. Write a program to calculate the grade of a student from his marks from the following scheme:
90-100Ex
80-90A
70-80B
60-70C
50-60D
<50F
Was this article helpful?
YesNo

Leave a Comment

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