{ "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 Flow-Control (Decision-Making) Statements\n", "\n", "In Python programming, decision-making statements are those that **help in deciding the flow of the program**. \n", "\n", "For example, at times, you might want to execute a block of code only if a particular condition is satisfied. Well, in this case, decision-making statement will be of great help. Let us consider an example to understand this better.\n", "\n", "Decision making is required when we want to execute a code only if a certain condition is satisfied.\n", "\n", "The key thing to note about Python's control flow statements and program structure is that it uses **`_indentation_`** to mark blocks. Hence the amount of white space (space or tab characters) at the start of a line is very important. This generally helps to make code more readable but can catch out new users of python." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* Suppose you are given a number and are asked to check if it is an even number or not. How would you do it?\n", "\n", "> The first thought that would pop up your mind is this - **\"If the number is divisible by 2, then it is an even number, else it is an odd number\"**. That's absolutely the right logic. But when this has to be coded, you will need the help of decision-making statements. To understand this, let's now look at how they function." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Control Flow Statements\n", "\n", "The flow control statements are divided into **three** categories:\n", "\n", "1. **Conditional statements**\n", "2. **Iterative statements**\n", "3. **Transfer/Control statements**\n", "\n", "<div>\n", "<img src=\"img/fcs.png\" width=\"500\"/>\n", "</div>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conditional statements\n", "\n", "In Python, condition statements act depending on whether a given condition is true or false. You can execute different blocks of codes depending on the outcome of a condition. Condition statements always evaluate to either **`True`** or **`False`**.\n", "\n", "There are **four** types of conditional statements:\n", "\n", "1. **[if](https://github.com/milaan9/03_Python_Flow_Control/blob/main/001_Python_if_statement.ipynb)**\n", "2. **[if-else](https://github.com/milaan9/03_Python_Flow_Control/blob/main/002_Python_if_else_statement.ipynb)**\n", "3. **[if-elif-else](https://github.com/milaan9/03_Python_Flow_Control/blob/main/003_Python_if_elif_else_statement%20.ipynb)**\n", "4. **[nested-if](https://github.com/milaan9/03_Python_Flow_Control/blob/main/004_Python_Nested_if_statement.ipynb)**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Iterative statements\n", "\n", "In Python, iterative statements allow us to execute a block of code repeatedly as long as the condition is **`True`**. We also call it a loop statements.\n", "\n", "Python provides us the following **two** loop statement to perform some actions repeatedly\n", "\n", "1. **[for loop](https://github.com/milaan9/03_Python_Flow_Control/blob/main/005_Python_for_Loop.ipynb)**\n", "2. **[while loop](https://github.com/milaan9/03_Python_Flow_Control/blob/main/006_Python_while_Loop.ipynb)**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Transfer statements\n", "\n", "In Python, transfer statements are used to alter the program’s way of execution in a certain manner. For this purpose, we use **three** types of transfer statements.\n", "\n", "1. **[break statement](https://github.com/milaan9/03_Python_Flow_Control/blob/main/007_Python_break_continue_pass_statements.ipynb)**\n", "2. **[continue statement](https://github.com/milaan9/03_Python_Flow_Control/blob/main/007_Python_break_continue_pass_statements.ipynb)**\n", "3. **[pass statements](https://github.com/milaan9/03_Python_Flow_Control/blob/main/007_Python_break_continue_pass_statements.ipynb)**" ] }, { "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 }