{ "cells": [ { "cell_type": "markdown", "id": "6bbcc246-5188-41d2-9126-32f44fee5e93", "metadata": {}, "source": [ "## Conditional Statements\n", "\n", "They are used to control the flow of execution in a program based on whether a certain condition is true or false.\n", "\n", "There are three main types of conditional statements in Python:\n", "\n", "**if statement**: Executes a block of code if a condition is true.\n", "\n", "**if...else statement**: Executes one block of code if a condition is true and another block of code if the condition is false.\n", "\n", "**if...elif...else statement**: Allows for multiple conditions to be checked sequentially. The elif keyword is short for \"else if.\" It allows you to check additional conditions if the previous if or elif conditions are false. The else block is executed if none of the preceding conditions are true.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "ac5c8f30-2a57-4afb-9d9d-8e4360b4fd7d", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "3e573a31-cde3-46fe-a59d-2ce02d1116df", "metadata": {}, "source": [ "```\n", "if condition1:\n", " # Code to execute if condition1 is True\n", "elif condition2:\n", " # Code to execute if condition1 is False and condition2 is True\n", "else:\n", " # Code to execute if both condition1 and condition2 are False\n", "```" ] }, { "cell_type": "markdown", "id": "63f0b303-ca3f-49e5-997e-6dc8cae15a4f", "metadata": {}, "source": [ "### IF Condition" ] }, { "cell_type": "code", "execution_count": 2, "id": "094c4de9-f71e-4ac7-87c2-f9f45c41e32a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "SUCCESS: API request successful. Processing response data...\n", "This is a new block of code\n" ] } ], "source": [ "# Simulating an API response\n", "api_response_status_code = 200\n", "\n", "if api_response_status_code == 200:\n", " print(\"SUCCESS: API request successful. Processing response data...\")\n", "else:\n", " print(f\"ERROR: API request failed with status code {api_response_status_code}.\")\n", " print(\"Action: Log error, possibly retry, or notify administrator.\")\n", "\n", "print(\"This is a new block of code\")" ] }, { "cell_type": "code", "execution_count": null, "id": "bce86fe8-6aca-4b5f-bc33-593db16fdac9", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "bd5f46b6-c830-4f42-b0dc-02eb671677d8", "metadata": {}, "source": [ "### Else IF" ] }, { "cell_type": "code", "execution_count": 11, "id": "abd7cc91-a9b8-40df-be2c-d90bc6b4fd19", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "✅ Good: request succeeded!\n" ] } ], "source": [ "# Simulating an API response\n", "api_response_status_code = 200\n", "\n", "if api_response_status_code == 200:\n", " print(\"✅ Good: request succeeded!\")\n", "elif 300 <= api_response_status_code < 400:\n", " print(\"ℹ️ Redirect: 3xx status code.\")\n", "elif 400 <= api_response_status_code < 500:\n", " print(\"❌ Client error: 4xx status code.\")\n", "elif 500 <= api_response_status_code < 600:\n", " print(\"🚨 Server error: 5xx status code.\")\n", "else:\n", " print(\"🤔 No matching status-code family.\")" ] }, { "cell_type": "markdown", "id": "ae1949b8-51f8-49f3-b3c5-5e9c5fa0ff0f", "metadata": {}, "source": [ "### Nested IF" ] }, { "cell_type": "code", "execution_count": 10, "id": "26fc69bb-7782-4e69-af73-73749b8db525", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Calculated error rate: 4.50%\n", "Error rate less than error threshold, we are good with the data\n" ] } ], "source": [ "total_records_processed = 10000\n", "failed_records = 450\n", "error_threshold = 0.05 # 5% max\n", "\n", "if total_records_processed > 0: # Avoid division by zero\n", " error_rate = failed_records / total_records_processed\n", " print(f\"Calculated error rate: {error_rate:.2%}\") # To print output with 2 decimal values, like 10.12\n", "\n", " if error_rate > error_threshold:\n", " print(f\"ALERT: Error rate ({error_rate:.2%}) exceeds threshold ({error_threshold:.2%}).\")\n", " else:\n", " print(\"Error rate less than error threshold, we are good with the data\")\n", "else:\n", " print(\"No records processed, cannot calculate error rate.\")" ] }, { "cell_type": "code", "execution_count": null, "id": "ee9ac180-19f1-4928-9b88-66e50c9760b2", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "d53751ac-219d-4378-b0a7-4535fe7bc9b6", "metadata": {}, "source": [ "### Ternary operator (short-hand if-else): \n", "A concise way to write an if-else statement in a single line." ] }, { "cell_type": "code", "execution_count": 13, "id": "2a6ab13d-92c7-4255-805d-00164e08c808", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "pandas\n" ] } ], "source": [ "# Decide between pandas and polars depending on row count\n", "\n", "row_count=1000\n", "\n", "data_engine = \"polars\" if row_count > 10000 else \"pandas\"\n", "\n", "print(data_engine)" ] }, { "cell_type": "code", "execution_count": null, "id": "91f198bb-f862-4a0a-99e6-b760035a7847", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:base] *", "language": "python", "name": "conda-base-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.13.5" } }, "nbformat": 4, "nbformat_minor": 5 }