Learning Conditional Statements in Python
Tags: webdev, python, softwaredevelopment
Published: 2024-07-03 03:53:49

## Introduction

Conditional statements are an essential part of programming that allows you to execute certain pieces of code based on specific conditions. In Python, these are typically implemented using `if`, `elif`, and `else` statements. This guide will help you understand how to use these statements effectively.

## Basic `if` Statement

The `if` statement allows you to execute a block of code only if a specified condition is true.

### Syntax

```bash
if condition:
    # code to execute if condition is true
```

### Example
```bash
x = 10
if x > 5:
    print("x is greater than 5")
```

## `if-else` Statement

The `if-else` statement allows you to execute one block of code if the condition is true and another block of code if the condition is false.

### Syntax
```bash
if condition:
    # code to execute if condition is true
else:
    # code to execute if condition is false
```

### Example
```bash
x = 3
if x > 5:
    print("x is greater than 5")
else:
    print("x is not greater than 5")
```

## `if-elif-else` Statement

The `if-elif-else` statement is used to check multiple conditions. The first condition that evaluates to true will have its block of code executed, and the rest will be skipped.

### Syntax
```bash
if condition1:
    # code to execute if condition1 is true
elif condition2:
    # code to execute if condition2 is true
else:
    # code to execute if none of the above conditions are true
```

### Example
```bash
x = 7
if x > 10:
    print("x is greater than 10")
elif x > 5:
    print("x is greater than 5 but less than or equal to 10")
else:
    print("x is 5 or less")
```

# Nested `if` Statements

You can also nest `if` statements within each other to check multiple conditions.

## Syntax
```python
if condition1:
    if condition2:
        # code to execute if both condition1 and condition2 are true
```

### Example
```python
x = 8
y = 12
if x > 5:
    if y > 10:
        print("x is greater than 5 and y is greater than 10")
```

# Logical Operators

Logical operators such as `and`, `or`, and `not` can be used to combine multiple conditions in a single if statement.

## Example with `and`
```python
x = 7
y = 12
if x > 5 and y > 10:
    print("Both conditions are true")
```

## Example with `or`
```python
x = 4
y = 12
if x > 5 or y > 10:
    print("At least one condition is true")
```

## Example with `not`
```python
x = 4
if not x > 5:
    print("x is not greater than 5")
```

# Simple Program to Illustrate Conditional Statements

Here is a simple Python program to illustrate the use of conditional statements (`if`, `elif`, and `else`).

```python
# Get user input
age = int(input("Enter your age: "))

# Check the age and print the appropriate message
if age < 0:
    print("Invalid age")
elif age < 18:
    print("You are a minor.")
elif age <= 65:
    print("You are an adult.")
else:
    print("You are a senior.")
```
## Output
`age = int(input("Enter your age: "))`


## Conclusion

Conditional statements are a fundamental part of controlling the flow of your Python programs. By using `if`, `elif`, and `else` statements effectively, along with logical operators, you can create more complex and useful programs. Practice writing your own conditional statements to become more comfortable with these concepts.