|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693 |
- {
- "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"
- ]
- }
- ],
- "source": [
- "x = 10\n",
- "y = 12\n",
- "if x > y:\n",
- " print(\"x>y\")\n",
- "elif x < y:\n",
- " print(\"x<y\")\n",
- "else:\n",
- " print(\"x=y\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "if statement inside a if statement or if-elif or if-else are called as nested if statements."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "x<y\n",
- "x=10\n"
- ]
- }
- ],
- "source": [
- "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": "markdown",
- "metadata": {},
- "source": [
- "## 4. Loops"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 4.1 For"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "for variable in something:\n",
- " \n",
- " algorithm"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "0\n",
- "1\n",
- "2\n",
- "3\n",
- "4\n"
- ]
- }
- ],
- "source": [
- "for i in range(5):\n",
- " print(i)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1\n",
- "2\n",
- "5\n",
- "6\n"
- ]
- }
- ],
- "source": [
- "a = [1, 2, 5, 6]\n",
- "for i in a:\n",
- " print(i)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "In the above example, i iterates over the 0,1,2,3,4. Every time it takes each value and executes the algorithm inside the loop. It is also possible to iterate over a nested list illustrated below."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[1, 2, 3]\n",
- "[4, 5, 6]\n",
- "[7, 8, 9]\n"
- ]
- }
- ],
- "source": [
- "list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
- "for list1 in list_of_lists:\n",
- " print(list1)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "A use case of a nested for loop in this case would be,"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1\n",
- "2\n",
- "3\n",
- "\n",
- "4\n",
- "5\n",
- "6\n",
- "\n",
- "7\n",
- "8\n",
- "9\n",
- "\n"
- ]
- }
- ],
- "source": [
- "list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n",
- "for list1 in list_of_lists:\n",
- " for x in list1:\n",
- " print(x)\n",
- " print()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "### 4.2 While"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "while some_condition:\n",
- " \n",
- " algorithm"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1\n",
- "4\n",
- "Bye\n",
- "looping 4\n",
- "looping 5\n",
- "looping 6\n",
- "looping 7\n",
- "looping 8\n",
- "looping 9\n",
- "looping 10\n",
- "looping 11\n"
- ]
- }
- ],
- "source": [
- "i = 1\n",
- "while i < 3:\n",
- " print(i ** 2)\n",
- " i = i+1\n",
- "print('Bye')\n",
- "\n",
- "# do-untile\n",
- "while True:\n",
- " #do something\n",
- " i = i+1\n",
- " print('looping %3d' % i)\n",
- " \n",
- " # check \n",
- " if i>10: 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
- }
|