A decision in a program is made by testing a condition that is either True or False. The result decides which block of code runs.

The if Statement

The simplest form is the if statement. If the condition after if is True, the indented block under it is executed, and otherwise it is skipped. A colon follows the condition, and the block is marked by indentation. The condition is a comparison or a logical expression, such as marks >= 40 or age > 18 and age < 60.

Definition: Conditional statement. A statement that runs a block of code only when a stated condition is True.

if-else and elif

The if-else statement chooses between two blocks. When there are more than two choices, elif, short for else if, is used. The conditions are tested from the top, and the first one that is True runs, after which the rest are skipped. An optional final else handles all remaining cases. In Python any value can be used as a condition: zero, an empty string, an empty list and None count as False, and everything else counts as True.

Nested Conditions and Conditional Expression

An if statement can be placed inside another, which is called nesting. Each level is indented further. Python also has a one-line conditional expression, written value_if_true if condition else value_if_false, which is convenient for simple choices such as status = "Pass" if m >= 40 else "Fail".

Conditions are often built by joining comparisons with the logical operators and, or and not. Python also allows a chained comparison such as 0 < x < 10, which is true when x lies between the two limits. The operators is and is not test whether two names refer to the same object, and they are different from == which compares values. When a program must choose among many fixed values, an if-elif chain is the usual method, although newer versions of Python also provide the match statement for this purpose. Writing the conditions clearly, with brackets where needed, prevents many errors.

Table 2.1: Forms of the conditional statement

Form Use Blocks
if One optional block 1
if-else Two alternatives 2
if-elif-else Many alternatives 3 or more
Nested if Decision inside a decision Levels

Example 2.1: Grade from marks

Problem: Write a program that reads marks and prints the grade: A for 75 and above, B for 60 to 74, C for 40 to 59 and F otherwise.

Solution:

  1. Test the highest range first, using elif for the next ranges.
  2. Use else for the remaining marks.

Result: For marks 68 the program prints grade B.

marks = int(input("Marks: "))
if marks >= 75:
    grade = "A"
elif marks >= 60:
    grade = "B"
elif marks >= 40:
    grade = "C"
else:
    grade = "F"
print("Grade:", grade)
print("Pass" if marks >= 40 else "Fail")

Input

68

Output

Marks: Grade: B
Pass

Common mistakes

  • Using = instead of == in a condition.
  • Forgetting the colon after the condition.
  • Testing the ranges in the wrong order so that an earlier condition catches every value.

Key takeaways

  • if, elif and else choose between blocks of code.
  • Only the first True condition in a chain runs.
  • Zero, an empty string and None count as False.
  • A conditional expression picks one of two values in one line.

Practice questions

  1. What is the purpose of elif?
  2. Which values are treated as False in a condition?
  3. What is nesting of if statements?

Answers to the practice questions

  1. It tests another condition when the earlier conditions are False.
  2. Zero, None and empty sequences such as "" and [].
  3. Placing one if statement inside another.

Sources

  • Official BCA syllabus Python Programming, course code 26BCA403SEC01, Unit II