{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "All the IPython Notebooks in this lecture series are available at https://github.com/rajathkumarmp/Python-Lectures" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Control Flow Statements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## If" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "if some_condition:\n", " \n", " algorithm" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Hello\n" ] } ], "source": [ "x = 12\n", "if x >10:\n", " print(\"Hello\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 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": [ "## 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(\"x=7:\n", " break" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 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": 10, "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": [ "## 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": 11, "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": 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,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": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[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": 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": [ "[27*z for i in range(50) if i==27 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.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }