{
"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 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
}