---
title: "How to implement if-else conditions in python code?"  
description: "How to implement if-else conditions in python code?"  
author: "ICSM Computer"  
published: 2025-09-23  
updated: 2025-09-24  
canonical: https://www.mindstick.com/forum/161926/how-to-implement-if-else-conditions-in-python-code  
category: "python"  
tags: ["python-3.4", "Python 3"]  
reading_time: 2 minutes  

---

# How to implement if-else conditions in python code?

**How to implement if-else [conditions](https://www.mindstick.com/blog/394/javascript-if-else) in [python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python) [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii)?**

## Replies

### Reply by Anubhav Sharma

> In Python, you use `if`**–**`elif`**–**`else` statements for conditional branching.

## Basic Syntax

```python
if condition1:
    # block of code if condition1 is True
elif condition2:
    # block of code if condition2 is True
else:
    # block of code if none of the above are True
```

- `if` → checks the first condition.
- `elif` (else-if) → checks more conditions if the previous ones failed.
- `else` → runs if no condition is `True`.

## Example 1: Simple If-Else

```python
age = 18

if age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")
```

## Example 2: If-Elif-Else Chain

```python
marks = 72

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

## Example 3: Nested If

```python
num = 15

if num > 0:
    if num % 2 == 0:
        print("Positive even number")
    else:
        print("Positive odd number")
else:
    print("Negative number or zero")
```

## Example 4: One-line If (Ternary Operator)

```python
x = 10
result = "Even" if x % 2 == 0 else "Odd"
print(result)  # Output: Even
```

## Summary:

- Use `if` when you want to check a condition.
- Use `elif` when you need multiple conditions.
- Use `else` as a fallback when no condition matches.


---

Original Source: https://www.mindstick.com/forum/161926/how-to-implement-if-else-conditions-in-python-code

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
