Conditional Statements in Python

Subject: Data Structures and Algorithms · Language: Python · Level: beginner · 6 min read

Learn if, if else, elif ladder, nested if, match-case idea, and decision-making logic in Python.

Conditional statements allow a program to make decisions. Without conditions, every program would run the same lines in the same order every time. With `if`, `elif`, and `else`, Python can choose different paths based on values.

An `if` condition is checked first. If it is true, Python runs the indented block below it. If it is false, Python skips that block. This simple decision-making idea is used in almost every DSA problem.

An `elif` ladder is used when there are many possible cases. Python checks conditions from top to bottom and runs the first true block. After one block runs, the remaining conditions are skipped. That is why the order of conditions is very important.

For example, in a grading problem, check `marks >= 90` before `marks >= 75`. If you check a smaller condition first, a high mark may match the wrong grade. This teaches students that conditions are not only about writing comparisons, but also about arranging them correctly.

Nested conditions are useful when a second decision should happen only after a first decision is true. For example, first check whether a number is positive, then check whether it is even. Too much nesting can make code hard to read, so use it carefully.

In DSA, conditions handle edge cases, compare values, update answers, stop loops, validate indexes, and choose logic paths. A single wrong condition can turn a correct idea into a wrong answer, so students should dry-run conditions with sample values.

Conditional statements are used when a program needs to make a decision. In real life, we make decisions all the time: if marks are high, print a good grade; if a number is divisible by 2, call it even; if a password is correct, allow login.

In Python, conditions are written using expressions that produce a boolean result: `True` or `False`. Based on that result, the program decides which block of code should run.

Conditional statements are very important in DSA because they help control logic. Searching, sorting, counting, validation, boundary checks, and decision-based problems all use conditions.

Indentation in Python Conditions

Python uses indentation to define the body of `if`, `elif`, and `else` blocks. This is different from languages that use curly braces.

```python

if marks >= 40:

print("Pass")

```

Sample Output when `marks = 50`

```text

Pass

```

The indented line belongs to the `if` block. If indentation is wrong, Python will show an error or the logic may not work as expected.

if Statement

`if` is used when we want to run a block of code only when a condition is true.

```python

if marks >= 40:

print("Pass")

```

Sample Output when `marks = 50`

```text

Pass

```

Here, `marks >= 40` is the condition. If it is true, `Pass` is printed. If it is false, the block is skipped.

if else Statement

`if else` is used when there are exactly two possible paths. One path runs when the condition is true, and the other path runs when the condition is false.

```python

if marks >= 40:

print("Pass")

else:

print("Fail")

```

Sample Output when `marks = 30`

```text

Fail

```

This is useful for pass/fail, even/odd, eligible/not eligible, found/not found, and yes/no decisions.

elif Ladder

`elif` means else-if. It is used when there are multiple conditions to check one by one.

A grading system is a common example because marks may fall into Grade A, Grade B, Grade C, Pass, or Fail. Python checks from top to bottom and runs the first matching block.

```python

if marks >= 90:

print("Grade A")

elif marks >= 75:

print("Grade B")

else:

print("Needs Improvement")

```

Sample Output when `marks = 82`

```text

Grade B

```

The order of conditions matters. If a broad condition is written before a specific one, the specific condition may never get a chance to run.

Nested if

Nested `if` means writing one `if` statement inside another `if` statement. It is useful when a second condition should be checked only after the first condition is true.

```python

if username_correct:

if password_correct:

print("Login successful")

```

Sample Output when both conditions are `True`

```text

Login successful

```

Nested conditions should be used carefully. Too many nested blocks can make code harder to read.

match-case Idea

Modern Python also has `match-case`, which can be used for fixed-choice matching. It is somewhat similar to switch-style logic in other languages.

```python

choice = 1

match choice:

case 1:

print("Start")

case 2:

print("Stop")

case _:

print("Invalid")

```

Output

```text

Start

```

For beginner DSA, `if`, `elif`, and `else` are enough for most problems. Learn `match-case` later when you are comfortable with conditions.

Choosing Between if and elif

Use separate `if` statements when multiple independent checks may all run. Use `elif` when only one branch should run from many options.

For example, grading should use `elif` because one mark value should receive one grade. Counting multiple properties may use separate `if` statements.

Example Program

```python

marks = int(input("Enter marks: "))

if marks >= 90:

print("Grade A")

elif marks >= 75:

print("Grade B")

elif marks >= 50:

print("Grade C")

elif marks >= 35:

print("Pass")

else:

print("Fail")

```

Sample Input

```text

82

```

Output

```text

Enter marks: Grade B

```

This program uses an `elif` ladder because marks are checked in ranges. Python checks the highest grade first and then moves downward.

Conditional Statements in DSA

Conditional statements appear in almost every DSA problem. You use them to compare values, update maximum or minimum, check whether an element is found, handle edge cases, and control loop behavior.

For example, while finding the largest element in a list, an `if` condition checks whether the current value is greater than the current maximum.

The condition inside `if` must produce a truth value. For example, `marks >= 40` produces either `True` or `False`. If it is true, Python runs the indented block below the `if`. If it is false, Python skips that block.

Nested conditions are used when one decision depends on another decision. For example, check whether a user exists first, then check the password. Too much nesting can make code hard to read, so use it only when it makes the logic clearer.

In DSA, conditions are used for boundary checks, maximum and minimum updates, target matching, edge cases, and loop control. A small condition mistake can change the whole answer, so students should trace conditions carefully.

Practice Question

Take a number as input and check whether it is positive, negative, or zero. Then extend the program to check whether a positive number is even or odd.

More Python Lessons

Browse all PrepCampus study materials