Posts

Showing posts with the label Selection in Python

Selection in Python

Selection in Python refers to the use of conditional statements to control the flow of a program based on specific conditions. The primary keywords used are `if`, `elif` (short for "else if"), and `else`. ---  **1. Basic `if` Statement** - Executes a block of code **only if** a condition is `True`. - **Syntax**:   ```python   if condition:       # code to execute if condition is True   ``` **Example**: ```python num = 10 if num > 0:     print("Positive number") ``` ---  **2. `if-else` Statement** - Executes one block if a condition is `True`, and another block if it is `False`. - **Syntax**:   ```python   if condition:       # code if True   else:       # code if False   ``` **Example**: ```python num = -5 if num >= 0:     print("Non-negative") else:     print("Negative") ``` ---  **3. `if-elif-else` Statement** - Checks multiple conditions in sequence. Stop...