{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Control Flow Statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. If" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "if some_condition:\n", " \n", " algorithm" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Welcome!\n" ] } ], "source": [ "x = 4\n", "if x >10:\n", " print(\"Hello\")\n", "else:\n", " print(\"Welcome!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. If-else" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "if some_condition:\n", " \n", " algorithm\n", " \n", "else:\n", " \n", " algorithm" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "x = 12\n", "if x > 10:\n", " print(\"hello\")\n", "else:\n", " print(\"world\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. if-elif" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "if some_condition:\n", " \n", " algorithm\n", "\n", "elif some_condition:\n", " \n", " algorithm\n", "\n", "else:\n", " \n", " algorithm" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x y:\n", " print(\"x>y\")\n", "elif x < y:\n", " print(\"x y:\n", " print(\"x>y\")\n", "elif x < y:\n", " print(\"x10: break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As the name says. It is used to break out of a loop when a condition becomes true when executing the loop." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "5\n", "6\n", "7\n" ] } ], "source": [ "for i in range(100):\n", " print(i)\n", " if i>=7:\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 6. Continue" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This continues the rest of the loop. Sometimes when a condition is satisfied there are chances of the loop getting terminated. This can be avoided using continue statement. " ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n", "1\n", "2\n", "3\n", "4\n", "The end.\n", "The end.\n", "The end.\n", "The end.\n", "The end.\n" ] } ], "source": [ "for i in range(10):\n", " if i>4:\n", " print(\"The end.\")\n", " continue\n", " elif i<7:\n", " print(i)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 7. List Comprehensions" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Python makes it simple to generate a required list with a single line of code using list comprehensions. For example If i need to generate multiples of say 27 I write the code using for loop as," ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]\n" ] } ], "source": [ "res = []\n", "for i in range(1,11):\n", " x = 27*i\n", " res.append(x)\n", "print(res)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Since you are generating another list altogether and that is what is required, List comprehensions is a more efficient way to solve this problem." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[27*x for x in range(1,11)]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "That's it!. Only remember to enclose it in square brackets" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Understanding the code, The first bit of the code is always the algorithm and then leave a space and then write the necessary loop. But you might be wondering can nested loops be extended to list comprehensions? Yes you can." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[27*x for x in range(1,20) if x<=10]" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "{'108': 108,\n", " '135': 135,\n", " '162': 162,\n", " '189': 189,\n", " '216': 216,\n", " '243': 243,\n", " '27': 27,\n", " '270': 270,\n", " '54': 54,\n", " '81': 81}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "{str(27*x):27*x for x in range(1,20) if x<=10}" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(27, 54, 81, 108, 135, 162, 189, 216, 243, 270)" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tuple((27*x for x in range(1,20) if x<=10))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let me add one more loop to make you understand better, " ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1,\n", " 2,\n", " 3,\n", " 4,\n", " 5,\n", " 6,\n", " 7,\n", " 8,\n", " 9,\n", " 10,\n", " 28,\n", " 29,\n", " 30,\n", " 31,\n", " 32,\n", " 33,\n", " 34,\n", " 35,\n", " 36,\n", " 37,\n", " 55,\n", " 56,\n", " 57,\n", " 58,\n", " 59,\n", " 60,\n", " 61,\n", " 62,\n", " 63,\n", " 64,\n", " 82,\n", " 83,\n", " 84,\n", " 85,\n", " 86,\n", " 87,\n", " 88,\n", " 89,\n", " 90,\n", " 91,\n", " 109,\n", " 110,\n", " 111,\n", " 112,\n", " 113,\n", " 114,\n", " 115,\n", " 116,\n", " 117,\n", " 118]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[27*i+z for i in range(50) if i<5 for z in range(1,11)]" ] } ], "metadata": { "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.6.8" } }, "nbformat": 4, "nbformat_minor": 1 }