# Day 14 – If-Else Conditionals in Python

![](https://cdn.hashnode.com/uploads/covers/664f77938fc1f806b829b90b/0e5e99aa-7676-4cbe-be77-7f3c3ac3745f.jpg align="center")

Welcome to **Day 14 of 100 Days of Python!**

Today, we are learning one of the most important concepts in programming: **conditional statements**.

Programs often need to make decisions based on certain conditions.

For example:

*   If your age is greater than 18 → you can drive.
    
*   If the apple price is within your budget → buy apples.
    
*   If a number is negative → print that it is negative.
    
*   If a number is zero → print that it is zero.
    
*   If none of the conditions match → execute another block of code.
    

Python provides `if`, `elif`, and `else` statements to implement this decision-making logic.

* * *

## What We Will Learn

In this tutorial, we will learn:

*   What conditional statements are
    
*   `if` statements
    
*   `if-else` statements
    
*   `if-elif-else` statements
    
*   Comparison operators
    
*   How Python evaluates conditions
    
*   Nested `if` statements
    
*   Using `and` with conditions
    
*   Taking user input for conditions
    
*   How indentation controls conditional blocks
    
*   Practical examples using Python
    

* * *

# 1\. What Are Conditional Statements?

A **conditional statement** allows a program to make decisions.

The program evaluates a condition, which produces either:

```text
True
```

or

```text
False
```

Based on the result, Python decides which block of code should execute.

For example:

```python
age = 20

if age > 18:
    print("You can drive.")
```

Here, Python checks:

```python
age > 18
```

Since `20 > 18` is `True`, the `print()` statement executes.

Output:

```text
You can drive.
```

If the condition were `False`, Python would skip the `if` block.

* * *

# 2\. The `if` Statement

The simplest conditional statement in Python is the `if` statement.

### Syntax

```python
if condition:
    # code to execute when condition is True
```

Example:

```python
age = 20

if age > 18:
    print("You can drive.")
```

### How It Works

Python evaluates:

```python
age > 18
```

The result is:

```text
True
```

Therefore, Python executes:

```python
print("You can drive.")
```

* * *

# 3\. Comparison Operators

Conditional statements commonly use **comparison operators**.

These operators compare two values and return either `True` or `False`.

| Operator | Meaning | Example |
| --- | --- | --- |
| `>` | Greater than | `10 > 5` → `True` |
| `<` | Less than | `5 < 10` → `True` |
| `>=` | Greater than or equal to | `10 >= 10` → `True` |
| `<=` | Less than or equal to | `10 <= 10` → `True` |
| `==` | Equal to | `10 == 10` → `True` |
| `!=` | Not equal to | `10 != 5` → `True` |

### Important

Do not confuse:

```python
=
```

with:

```python
==
```

`=` is used for **assignment**:

```python
age = 20
```

`==` is used for **comparison**:

```python
age == 20
```

* * *

# 4\. `if-else` Statement

Sometimes we want one block of code to execute when a condition is `True` and another block when it is `False`.

For this, we use `if-else`.

### Syntax

```python
if condition:
    # executes when condition is True
else:
    # executes when condition is False
```

Example:

```python
applePrice = 210
budget = 200

if applePrice <= budget:
    print("Alexa, add 1 kg Apples to the cart.")
else:
    print("Alexa, do not add Apples to the cart.")
```

Here:

```python
210 <= 200
```

is `False`.

Therefore, the `else` block executes.

Output:

```text
Alexa, do not add Apples to the cart.
```

* * *

# 5\. Understanding `if-else` Flow

The execution flow can be visualized like this:

```text
             Start
               |
               v
       Evaluate condition
               |
        +------+------+
        |             |
      True          False
        |             |
        v             v
    if block       else block
        |             |
        +------+------+
               |
               v
             Continue
```

Only one of the two blocks executes.

* * *

# 6\. Taking User Input in Conditions

Conditional statements become more useful when we take input from the user.

Example:

```python
a = int(input("Enter your age: "))

if a > 18:
    print("You can Drive.")
else:
    print("You cannot Drive.")
```

The `input()` function returns a string, so we use:

```python
int()
```

to convert the entered value into an integer.

For example, if the user enters:

```text
20
```

then:

```python
a
```

contains the integer:

```text
20
```

Python checks:

```python
a > 18
```

Since the condition is `True`, it prints:

```text
You can Drive.
```

* * *

# 7\. Understanding `01_myif.py`

Our first program is:

```python
a = int(input("Enter your age: "))

print("Your age is: ", a)

# Conditional operators
# >, <, >=, <=, ==, !=

# print(a > 18)
# print(a <= 18)
# print(a == 18)
# print(a != 18)

if a > 18:
    print("You can Drive.")
    print("Yes")
else:
    print("You cannot Drive.")
    print("No")

print("End of the program.")
```

Let's understand it step by step.

### Step 1: Take Input

```python
a = int(input("Enter your age: "))
```

The user enters their age.

### Step 2: Display the Age

```python
print("Your age is: ", a)
```

The entered age is displayed.

### Step 3: Check the Condition

```python
if a > 18:
```

Python checks whether the age is greater than `18`.

### Step 4: If True

If the user enters:

```text
25
```

then:

```python
25 > 18
```

is `True`.

Python executes:

```python
print("You can Drive.")
print("Yes")
```

### Step 5: If False

If the user enters:

```text
16
```

then:

```python
16 > 18
```

is `False`.

Python executes:

```python
print("You cannot Drive.")
print("No")
```

### Step 6: Continue the Program

Finally:

```python
print("End of the program.")
```

executes regardless of whether the `if` or `else` block was selected.

* * *

# 8\. The `elif` Statement

What if we have more than two possible conditions?

For example, a number can be:

*   Negative
    
*   Zero
    
*   Positive
    

We could use multiple `if` statements, but Python provides a cleaner solution:

```python
elif
```

`elif` means **"else if"**.

### Syntax

```python
if condition1:
    # code
elif condition2:
    # code
else:
    # code
```

Python checks the conditions from top to bottom.

* * *

# 9\. How `if-elif-else` Works

Consider:

```python
num = 0

if num < 0:
    print("Number is negative.")
elif num == 0:
    print("Number is Zero.")
else:
    print("Number is positive.")
```

Python first checks:

```python
num < 0
```

For `num = 0`:

```text
0 < 0
```

is `False`.

Python then checks:

```python
num == 0
```

This is `True`.

Therefore:

```text
Number is Zero.
```

is printed.

The `else` block is not executed.

* * *

# 10\. Understanding `03_elif.py`

Our program is:

```python
num = int(input("Enter a number: "))

if num < 0:
    print("Number is negative.")
elif num == 0:
    print("Number is Zero.")
elif num == 999:
    print("Number is Special.")
else:
    print("Number is positive.")

print("I am happy now.")
```

This program demonstrates multiple conditions.

### Condition 1

```python
if num < 0:
```

If the number is negative:

```text
Number is negative.
```

### Condition 2

```python
elif num == 0:
```

If the number is exactly zero:

```text
Number is Zero.
```

### Condition 3

```python
elif num == 999:
```

If the number is exactly `999`:

```text
Number is Special.
```

### Final Case

```python
else:
    print("Number is positive.")
```

If none of the previous conditions is `True`, the `else` block executes.

For example, if:

```text
num = 25
```

then:

*   `25 < 0` → False
    
*   `25 == 0` → False
    
*   `25 == 999` → False
    
*   `else` → executes
    

Output:

```text
Number is positive.
```

* * *

# 11\. Important Point About `elif`

Python checks an `if-elif-else` chain from top to bottom.

Once Python finds a condition that evaluates to `True`, it executes that block and skips the remaining conditions in that chain.

For example:

```python
num = 999

if num > 0:
    print("Positive")
elif num == 999:
    print("Special")
```

The first condition:

```python
num > 0
```

is already `True`.

Therefore, Python prints:

```text
Positive
```

and does not continue to the `elif`.

This is why **the order of conditions matters**.

* * *

# 12\. The `else` Statement

The `else` block is executed when none of the preceding conditions is `True`.

Example:

```python
age = 15

if age >= 18:
    print("Adult")
else:
    print("Minor")
```

Since:

```python
15 >= 18
```

is `False`, Python executes:

```python
else
```

Output:

```text
Minor
```

The `else` block is optional.

You can have:

```python
if
```

by itself,

or:

```python
if + else
```

or:

```python
if + elif + else
```

* * *

# 13\. Nested `if` Statements

An `if` statement can also be placed inside another `if`, `elif`, or `else` block.

This is called a **nested conditional statement**.

Example:

```python
num = 18

if num < 0:
    print("Number is negative.")

elif num > 0:

    if num <= 10:
        print("Number is between 1-10")

    elif num > 10 and num <= 20:
        print("Number is between 11-20")

    else:
        print("Number is greater than 20")

else:
    print("Number is zero")
```

Here, the second `if` is inside the `elif num > 0` block.

Therefore, it is a nested `if`.

* * *

# 14\. Understanding `04_nested.py`

Let's break the program down.

First:

```python
num = 18
```

Python checks:

```python
if num < 0:
```

Since:

```text
18 < 0
```

is `False`, Python moves to:

```python
elif num > 0:
```

This is:

```text
18 > 0
```

which is `True`.

Therefore, Python enters this block:

```python
if num <= 10:
```

Now:

```text
18 <= 10
```

is `False`.

Python moves to:

```python
elif num > 10 and num <= 20:
```

Both conditions are true:

```text
18 > 10      → True
18 <= 20     → True
```

The `and` operator requires both conditions to be `True`.

Therefore, the result is:

```text
True
```

and Python prints:

```text
Number is between 11-20
```

* * *

# 15\. Using `and` in Conditions

In our nested program, we use:

```python
num > 10 and num <= 20
```

The `and` operator combines two conditions.

Both conditions must be `True`.

| Condition 1 | Condition 2 | `and` Result |
| --- | --- | --- |
| True | True | True |
| True | False | False |
| False | True | False |
| False | False | False |

For:

```python
num = 18
```

we get:

```python
18 > 10
```

→ `True`

and:

```python
18 <= 20
```

→ `True`

Therefore:

```python
True and True
```

results in:

```text
True
```

* * *

# 16\. Python Indentation Is Important

Python uses **indentation** to identify which statements belong to a conditional block.

Correct:

```python
if age > 18:
    print("You can drive.")
```

The `print()` statement is indented, so it belongs to the `if` block.

Another example:

```python
if age > 18:
    print("You can drive.")
    print("Yes")
else:
    print("You cannot drive.")
```

Both statements under the `if` are indented.

Incorrect indentation can result in an error or change the program's logic.

* * *

# 17\. Parentheses Around Conditions

You may see conditions written like:

```python
if (applePrice <= budget):
```

This is valid Python.

You can also write:

```python
if applePrice <= budget:
```

Both work.

For example:

```python
if (num == 0):
    print("Zero")
```

and:

```python
if num == 0:
    print("Zero")
```

have the same meaning.

Parentheses are not required around a simple condition.

* * *

# 18\. Complete Programs From Day 14

## Program 1 – Age Checking

```python
a = int(input("Enter your age: "))

print("Your age is: ", a)

if a > 18:
    print("You can Drive.")
    print("Yes")
else:
    print("You cannot Drive.")
    print("No")

print("End of the program.")
```

Example output:

```text
Enter your age: 21
Your age is:  21
You can Drive.
Yes
End of the program.
```

* * *

## Program 2 – Apple Budget

```python
applePrice = 210
budget = 200

if applePrice <= budget:
    print("Alexa, add 1 kg Apples to the cart.")
else:
    print("Alexa, do not add Apples to the cart.")
```

Output:

```text
Alexa, do not add Apples to the cart.
```

* * *

## Program 3 – Multiple Conditions

```python
num = int(input("Enter a number: "))

if num < 0:
    print("Number is negative.")
elif num == 0:
    print("Number is Zero.")
elif num == 999:
    print("Number is Special.")
else:
    print("Number is positive.")

print("I am happy now.")
```

* * *

## Program 4 – Nested Conditions

```python
num = 18

if num < 0:
    print("Number is negative.")

elif num > 0:

    if num <= 10:
        print("Number is between 1-10")

    elif num > 10 and num <= 20:
        print("Number is between 11-20")

    else:
        print("Number is greater than 20")

else:
    print("Number is zero")
```

Output:

```text
Number is between 11-20
```

* * *

# 19\. Types of Conditional Structures

The concepts covered today can be summarized as follows:

### 1\. `if`

Used when we want to execute code only when a condition is true.

```python
if age > 18:
    print("Adult")
```

### 2\. `if-else`

Used when there are two possible paths.

```python
if age >= 18:
    print("Adult")
else:
    print("Minor")
```

### 3\. `if-elif-else`

Used when there are multiple possible conditions.

```python
if marks >= 90:
    print("A")
elif marks >= 75:
    print("B")
else:
    print("C")
```

### 4\. Nested Conditionals

Used when one condition needs to be checked inside another conditional block.

```python
if age >= 18:
    if has_license:
        print("Can drive")
```

* * *

# 20\. Important Concepts at a Glance

| Concept | Purpose |
| --- | --- |
| `if` | Executes code when a condition is `True` |
| `else` | Executes when previous condition(s) are `False` |
| `elif` | Checks another condition |
| `>` | Greater than |
| `<` | Less than |
| `>=` | Greater than or equal to |
| `<=` | Less than or equal to |
| `==` | Checks equality |
| `!=` | Checks inequality |
| `and` | Both conditions must be true |
| `int()` | Converts input into an integer |
| Indentation | Defines conditional blocks |
| Nested `if` | Conditional statement inside another conditional |

* * *

# 21\. Quick Revision

### What is a conditional statement?

A conditional statement allows a program to make decisions based on whether a condition evaluates to `True` or `False`.

### What does `if` do?

It executes a block of code when its condition is `True`.

### What does `else` do?

It executes when the preceding `if`/`elif` conditions are all `False`.

### What does `elif` mean?

`elif` means **else if** and allows us to check additional conditions.

### Can we have multiple `elif` statements?

Yes.

```python
if condition1:
    ...
elif condition2:
    ...
elif condition3:
    ...
else:
    ...
```

### Can we put an `if` inside another `if`?

Yes. This is called a **nested** `if` **statement**.

### What does `==` mean?

It checks whether two values are equal.

```python
a == b
```

### What does `=` mean?

It assigns a value.

```python
a = 10
```

* * *

# 22\. Key Takeaways

*   Conditional statements allow programs to make decisions.
    
*   Python uses `if`, `elif`, and `else` for decision-making.
    
*   Conditions evaluate to `True` or `False`.
    
*   Comparison operators are commonly used inside conditions.
    
*   `if-else` provides two possible execution paths.
    
*   `elif` allows multiple conditions to be checked.
    
*   Python executes the first matching condition in an `if-elif-else` chain.
    
*   `else` executes when none of the previous conditions is `True`.
    
*   Conditional statements can be nested inside one another.
    
*   The `and` operator requires both conditions to be `True`.
    
*   Proper indentation is essential in Python.
    
*   User input can be combined with conditionals to create interactive programs.
    

Conditional statements are one of the fundamental building blocks of programming. Once you understand them, you can start creating programs that **respond differently depending on the input and situation**.

* * *

## 📂 Day 14 Resources

%[https://github.com/SriteshSuranjan/100-Days-of-Python/tree/main/14-Day14-If-Else-Conditionals] 

* * *
