{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<small><small><i>\n", "All the IPython Notebooks in **Python Flow Control Statements** lecture series by Dr. Milaan Parmar are available @ **[GitHub](https://github.com/milaan9/03_Python_Flow_Control)**\n", "</i></small></small>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Python Nested `if` statement\n", "\n", "We can have a nested-**[if-else](https://github.com/milaan9/03_Python_Flow_Control/blob/main/002_Python_if_else_statement.ipynb)** or nested-**[if-elif-else](https://github.com/milaan9/03_Python_Flow_Control/blob/main/003_Python_if_elif_else_statement%20.ipynb)** statement inside another **`if-else`** statement. This is called **nesting** in computer programming. The nested if statements is useful when we want to make a series of decisions.\n", "\n", "Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of nesting. They can get confusing, so they must be avoided unless necessary.\n", "\n", "We can use nested if statements for situations where we want to check for a **secondary condition** if the first condition executes as **`True`**." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Syntax:\n", "\n", "#### Example 1:\n", "```python\n", "if conditon_outer:\n", " if condition_inner:\n", " statement of nested if\n", " else:\n", " statement of nested if else:\n", " statement ot outer if\n", "else:\n", " Outer else \n", "statement outside if block\n", "```\n", "\n", "#### Example 2:\n", "```python\n", "if expression1:\n", " statement(s)\n", " if expression2:\n", " statement(s)\n", " elif expression3:\n", " statement(s)\n", " elif expression4:\n", " statement(s)\n", " else:\n", " statement(s)\n", "else:\n", " statement(s)\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2021-06-10T21:32:46.158585Z", "start_time": "2021-06-10T21:32:46.150794Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "All Conditions are false\n" ] } ], "source": [ "# Example 1:\n", "\n", "a=10\n", "if a>=20: # Condition FALSE\n", " print (\"Condition is True\")\n", "else: # Code will go to ELSE body\n", " if a>=15: # Condition FALSE\n", " print (\"Checking second value\")\n", " else: # Code will go to ELSE body\n", " print (\"All Conditions are false\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2021-06-10T21:32:46.684934Z", "start_time": "2021-06-10T21:32:46.672274Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x<y\n", "x=10\n" ] } ], "source": [ "# Example 2:\n", "\n", "x = 10\n", "y = 12\n", "if x > y:\n", " print( \"x>y\")\n", "elif x < y:\n", " print( \"x<y\")\n", " if x==10:\n", " print (\"x=10\")\n", " else:\n", " print (\"invalid\")\n", "else:\n", " print (\"x=y\")" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2021-06-10T21:32:47.528747Z", "start_time": "2021-06-10T21:32:47.507326Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "num1 is neither positive nor negative\n" ] } ], "source": [ "# Example 3:\n", "\n", "num1 = 0\n", "if (num1 != 0): # For zero condition is FALSE\n", " if(num1 > 0): \n", " print(\"num1 is a positive number\")\n", " else: \n", " print(\"num1 is a negative number\")\n", "else: # For zero condition is TRUE\n", " print(\"num1 is neither positive nor negative\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2021-06-10T21:33:30.124628Z", "start_time": "2021-06-10T21:33:16.450727Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter a number: 9\n", "Positive number\n" ] } ], "source": [ "# Example 4:\n", "\n", "'''In this program, we input a number check if the number is \n", "positive or negative or zero and display an appropriate message. \n", "This time we use nested if statement'''\n", "\n", "num = float(input(\"Enter a number: \"))\n", "if num >= 0:\n", " if num == 0:\n", " print(\"Zero\")\n", " else:\n", " print(\"Positive number\")\n", "else:\n", " print(\"Negative number\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2021-06-10T21:33:37.996992Z", "start_time": "2021-06-10T21:33:37.975571Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "96 is greater than 66\n", "96 and 96 are equal\n" ] } ], "source": [ "# Example 5:\n", "\n", "def number_arithmetic(num1, num2):\n", " if num1 >= num2:\n", " if num1 == num2:\n", " print(f'{num1} and {num2} are equal')\n", " else:\n", " print(f'{num1} is greater than {num2}')\n", " else:\n", " print(f'{num1} is smaller than {num2}')\n", "\n", "number_arithmetic(96, 66)\n", "# Output 96 is greater than 66\n", "number_arithmetic(96, 96)\n", "# Output 56 and 56 are equal" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "hide_input": false, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.8.8" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }