|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "**Table of contents**<a id='toc0_'></a> \n", |
| 8 | + "- [Python if...else](#toc1_) \n", |
| 9 | + "- [Python for Loop](#toc2_) \n", |
| 10 | + " - [Python for loop with else](#toc2_1_) \n", |
| 11 | + "- [Python while Loop](#toc3_) \n", |
| 12 | + " - [Python While loop with else](#toc3_1_) \n", |
| 13 | + "- [Python break Statement](#toc4_) \n", |
| 14 | + "- [Python continue Statement](#toc5_) \n", |
| 15 | + "- [Python pass Statement](#toc6_) \n", |
| 16 | + "\n", |
| 17 | + "<!-- vscode-jupyter-toc-config\n", |
| 18 | + "\tnumbering=false\n", |
| 19 | + "\tanchor=true\n", |
| 20 | + "\tflat=false\n", |
| 21 | + "\tminLevel=1\n", |
| 22 | + "\tmaxLevel=6\n", |
| 23 | + "\t/vscode-jupyter-toc-config -->\n", |
| 24 | + "<!-- THIS CELL WILL BE REPLACED ON TOC UPDATE. DO NOT WRITE YOUR TEXT IN THIS CELL -->" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "# <a id='toc1_'></a>[Python if...else](#toc0_)\n", |
| 32 | + "\n", |
| 33 | + "\n", |
| 34 | + "- we use the if statement to run a block code only when a certain condition is met.\n", |
| 35 | + "\n", |
| 36 | + "If the condition evaluates to False,\n", |
| 37 | + "- the code inside else is executed\n", |
| 38 | + "- the code inside if is skipped\n", |
| 39 | + "\n", |
| 40 | + "\n", |
| 41 | + "The `if...else` statement is used to execute a block of code among two alternatives.\n", |
| 42 | + "- However, if we need to make a choice between more than two alternatives, then we use the `if...elif...else` statement.\n" |
| 43 | + ] |
| 44 | + }, |
| 45 | + { |
| 46 | + "cell_type": "code", |
| 47 | + "execution_count": 1, |
| 48 | + "metadata": {}, |
| 49 | + "outputs": [ |
| 50 | + { |
| 51 | + "name": "stdout", |
| 52 | + "output_type": "stream", |
| 53 | + "text": [ |
| 54 | + "Zero\n", |
| 55 | + "This statement is always executed\n" |
| 56 | + ] |
| 57 | + } |
| 58 | + ], |
| 59 | + "source": [ |
| 60 | + "number = 0\n", |
| 61 | + "\n", |
| 62 | + "if number > 0:\n", |
| 63 | + " print(\"Positive number\")\n", |
| 64 | + "\n", |
| 65 | + "elif number == 0:\n", |
| 66 | + " print('Zero')\n", |
| 67 | + "else:\n", |
| 68 | + " print('Negative number')\n", |
| 69 | + "\n", |
| 70 | + "print('This statement is always executed')" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "markdown", |
| 75 | + "metadata": {}, |
| 76 | + "source": [ |
| 77 | + "There are 2 types of loops in Python:\n", |
| 78 | + "- for loop\n", |
| 79 | + "- while loop\n", |
| 80 | + "\n", |
| 81 | + "# <a id='toc2_'></a>[Python for Loop](#toc0_)\n", |
| 82 | + "\n", |
| 83 | + "In Python, a for loop is used to iterate over sequences such as lists, tuples, string, etc." |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": 2, |
| 89 | + "metadata": {}, |
| 90 | + "outputs": [ |
| 91 | + { |
| 92 | + "name": "stdout", |
| 93 | + "output_type": "stream", |
| 94 | + "text": [ |
| 95 | + "Swift\n", |
| 96 | + "Python\n", |
| 97 | + "Go\n", |
| 98 | + "JavaScript\n" |
| 99 | + ] |
| 100 | + } |
| 101 | + ], |
| 102 | + "source": [ |
| 103 | + "languages = ['Swift', 'Python', 'Go', 'JavaScript']\n", |
| 104 | + "\n", |
| 105 | + "# run a loop for each item of the list\n", |
| 106 | + "for language in languages:\n", |
| 107 | + " print(language)" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "markdown", |
| 112 | + "metadata": {}, |
| 113 | + "source": [ |
| 114 | + "## <a id='toc2_1_'></a>[Python for loop with else](#toc0_)\n", |
| 115 | + "\n", |
| 116 | + "- A for loop can have an optional else block. \n", |
| 117 | + "- The else part is executed when the loop is exhausted (after the loop iterates through every item of a sequence).\n", |
| 118 | + "\n", |
| 119 | + "**Note: The else block will not execute if the for loop is stopped by a break statement.**" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "code", |
| 124 | + "execution_count": 4, |
| 125 | + "metadata": {}, |
| 126 | + "outputs": [ |
| 127 | + { |
| 128 | + "name": "stdout", |
| 129 | + "output_type": "stream", |
| 130 | + "text": [ |
| 131 | + "0\n", |
| 132 | + "1\n", |
| 133 | + "5\n", |
| 134 | + "No items left.\n", |
| 135 | + "101\n" |
| 136 | + ] |
| 137 | + } |
| 138 | + ], |
| 139 | + "source": [ |
| 140 | + "digits = [0, 1, 5]\n", |
| 141 | + "\n", |
| 142 | + "for i in digits:\n", |
| 143 | + " print(i)\n", |
| 144 | + "else:\n", |
| 145 | + " print(\"No items left.\") # Executed\n", |
| 146 | + "\n", |
| 147 | + "#############################################################\n", |
| 148 | + "\n", |
| 149 | + "digits = [101, 111, 523]\n", |
| 150 | + "\n", |
| 151 | + "for i in digits:\n", |
| 152 | + " print(i)\n", |
| 153 | + " break\n", |
| 154 | + "else:\n", |
| 155 | + " print(\"No items left.\") # Not Executed " |
| 156 | + ] |
| 157 | + }, |
| 158 | + { |
| 159 | + "cell_type": "markdown", |
| 160 | + "metadata": {}, |
| 161 | + "source": [ |
| 162 | + "# <a id='toc3_'></a>[Python while Loop](#toc0_)\n", |
| 163 | + "\n", |
| 164 | + "Python while loop is used to run a block code until a certain condition is met.\n", |
| 165 | + "\n", |
| 166 | + "## <a id='toc3_1_'></a>[Python While loop with else](#toc0_)\n", |
| 167 | + "\n", |
| 168 | + "- In Python, a while loop may have an optional else block.\n", |
| 169 | + "- Here, the else part is executed after the condition of the loop evaluates to False.\n", |
| 170 | + "\n", |
| 171 | + "**Note: The else block will not execute if the while loop is terminated by a break statement.**" |
| 172 | + ] |
| 173 | + }, |
| 174 | + { |
| 175 | + "cell_type": "code", |
| 176 | + "execution_count": 6, |
| 177 | + "metadata": {}, |
| 178 | + "outputs": [ |
| 179 | + { |
| 180 | + "name": "stdout", |
| 181 | + "output_type": "stream", |
| 182 | + "text": [ |
| 183 | + "Inside loop1\n", |
| 184 | + "Inside else2\n", |
| 185 | + "Outside loop2\n" |
| 186 | + ] |
| 187 | + } |
| 188 | + ], |
| 189 | + "source": [ |
| 190 | + "counter = 0\n", |
| 191 | + "\n", |
| 192 | + "while counter < 3:\n", |
| 193 | + " # loop ends because of break\n", |
| 194 | + " # the else part is not executed \n", |
| 195 | + " if counter == 1:\n", |
| 196 | + " break\n", |
| 197 | + "\n", |
| 198 | + " print('Inside loop1')\n", |
| 199 | + " counter = counter + 1\n", |
| 200 | + "else:\n", |
| 201 | + " print('Inside else1')\n", |
| 202 | + "\n", |
| 203 | + "#############################################################\n", |
| 204 | + "\n", |
| 205 | + "counter = 5\n", |
| 206 | + "\n", |
| 207 | + "while counter < 3:\n", |
| 208 | + " # loop ends because of break\n", |
| 209 | + " # the else part is not executed \n", |
| 210 | + " if counter == 1:\n", |
| 211 | + " break\n", |
| 212 | + "\n", |
| 213 | + " print('Inside loop2')\n", |
| 214 | + " counter = counter + 1\n", |
| 215 | + "else:\n", |
| 216 | + " print('Inside else2')\n", |
| 217 | + "\n", |
| 218 | + "print('Outside loop2')" |
| 219 | + ] |
| 220 | + }, |
| 221 | + { |
| 222 | + "cell_type": "markdown", |
| 223 | + "metadata": {}, |
| 224 | + "source": [ |
| 225 | + "# <a id='toc4_'></a>[Python break Statement](#toc0_)\n", |
| 226 | + "The break statement is used to terminate the loop immediately when it is encountered.\n", |
| 227 | + "\n", |
| 228 | + "# <a id='toc5_'></a>[Python continue Statement](#toc0_)\n", |
| 229 | + "The continue statement is used to skip the current iteration of the loop and the control flow of the program goes to the next iteration.\n", |
| 230 | + "\n", |
| 231 | + "# <a id='toc6_'></a>[Python pass Statement](#toc0_)\n", |
| 232 | + "- In Python programming, the pass statement is a null statement which can be used as a placeholder for future code.\n", |
| 233 | + "- Suppose we have a loop or a function that is not implemented yet, but we want to implement it in the future. In such cases, we can use the pass statement.\n" |
| 234 | + ] |
| 235 | + }, |
| 236 | + { |
| 237 | + "cell_type": "code", |
| 238 | + "execution_count": 9, |
| 239 | + "metadata": {}, |
| 240 | + "outputs": [ |
| 241 | + { |
| 242 | + "name": "stdout", |
| 243 | + "output_type": "stream", |
| 244 | + "text": [ |
| 245 | + "1\n", |
| 246 | + "3\n", |
| 247 | + "5\n", |
| 248 | + "7\n", |
| 249 | + "9\n" |
| 250 | + ] |
| 251 | + } |
| 252 | + ], |
| 253 | + "source": [ |
| 254 | + "# program to print odd numbers from 1 to 10\n", |
| 255 | + "num = 0\n", |
| 256 | + "\n", |
| 257 | + "while num < 10:\n", |
| 258 | + " num += 1\n", |
| 259 | + " if (num % 2) == 0:\n", |
| 260 | + " continue\n", |
| 261 | + " print(num)\n", |
| 262 | + "\n", |
| 263 | + "#########################################\n", |
| 264 | + "# program to print odd numbers from 1 to 10\n", |
| 265 | + "number = 1\n", |
| 266 | + "while (number%2 != 0) and (number < 10) :\n", |
| 267 | + " print(number) \n", |
| 268 | + " number += 2" |
| 269 | + ] |
| 270 | + }, |
| 271 | + { |
| 272 | + "cell_type": "code", |
| 273 | + "execution_count": null, |
| 274 | + "metadata": {}, |
| 275 | + "outputs": [], |
| 276 | + "source": [] |
| 277 | + } |
| 278 | + ], |
| 279 | + "metadata": { |
| 280 | + "kernelspec": { |
| 281 | + "display_name": "Python 3", |
| 282 | + "language": "python", |
| 283 | + "name": "python3" |
| 284 | + }, |
| 285 | + "language_info": { |
| 286 | + "codemirror_mode": { |
| 287 | + "name": "ipython", |
| 288 | + "version": 3 |
| 289 | + }, |
| 290 | + "file_extension": ".py", |
| 291 | + "mimetype": "text/x-python", |
| 292 | + "name": "python", |
| 293 | + "nbconvert_exporter": "python", |
| 294 | + "pygments_lexer": "ipython3", |
| 295 | + "version": "3.10.4" |
| 296 | + } |
| 297 | + }, |
| 298 | + "nbformat": 4, |
| 299 | + "nbformat_minor": 2 |
| 300 | +} |
0 commit comments