|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## 1. For Loop\n", |
| 8 | + "The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. \n", |
| 9 | + "\n", |
| 10 | + "**Syntax of for loop**<br>\n", |
| 11 | + "for val in sequence:<br>\n", |
| 12 | + " loop body\n", |
| 13 | + "\n", |
| 14 | + "Here, `val` is the variable that takes the value of the item inside the sequence on each iteration.\n", |
| 15 | + "`Loop continues` until we reach the `last item` in the sequence. The body of for loop is separated from the rest of the code using indentation." |
| 16 | + ] |
| 17 | + }, |
| 18 | + { |
| 19 | + "cell_type": "code", |
| 20 | + "execution_count": 2, |
| 21 | + "metadata": {}, |
| 22 | + "outputs": [ |
| 23 | + { |
| 24 | + "name": "stdout", |
| 25 | + "output_type": "stream", |
| 26 | + "text": [ |
| 27 | + "The sum is 48\n" |
| 28 | + ] |
| 29 | + } |
| 30 | + ], |
| 31 | + "source": [ |
| 32 | + "#Example of for Loop\n", |
| 33 | + "#Program to find the sum of all numbers stored in a list\n", |
| 34 | + "\n", |
| 35 | + "# List of numbers\n", |
| 36 | + "numbers = [6, 5, 3, 8, 4, 2, 5, 4, 11]\n", |
| 37 | + "\n", |
| 38 | + "# variable to store the sum\n", |
| 39 | + "sum = 0\n", |
| 40 | + "\n", |
| 41 | + "# iterate over the list\n", |
| 42 | + "for val in numbers:\n", |
| 43 | + " sum = sum+val\n", |
| 44 | + "\n", |
| 45 | + "print(\"The sum is\", sum)" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "markdown", |
| 50 | + "metadata": {}, |
| 51 | + "source": [ |
| 52 | + "## 2. Range() function\n", |
| 53 | + "- We can generate a sequence of numbers using `range()` function. `range(10)` will generate numbers from 0 to 9 (10 numbers).\n", |
| 54 | + "- We can also define the start, stop and step size as `range(start, stop,step_size)`. step_size defaults to 1 if not provided.\n", |
| 55 | + "- The `range` object is \"lazy\" in a sense because it doesn't generate every number that it \"contains\" when we create it. However, it is not an iterator since it supports `in`, `len` and `__getitem__` operations.\n", |
| 56 | + "- This function does not store all the values in memory; it would be inefficient. So it remembers the start, stop, step size and generates the next number on the go.\n", |
| 57 | + "- To force this function to output all the items, we can use the function `list()`.\n", |
| 58 | + "- The following example will clarify this." |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "code", |
| 63 | + "execution_count": 3, |
| 64 | + "metadata": {}, |
| 65 | + "outputs": [ |
| 66 | + { |
| 67 | + "name": "stdout", |
| 68 | + "output_type": "stream", |
| 69 | + "text": [ |
| 70 | + "range(0, 10)\n", |
| 71 | + "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", |
| 72 | + "[2, 3, 4, 5, 6, 7]\n", |
| 73 | + "[2, 5, 8, 11, 14, 17]\n" |
| 74 | + ] |
| 75 | + } |
| 76 | + ], |
| 77 | + "source": [ |
| 78 | + "print(range(10))\n", |
| 79 | + "\n", |
| 80 | + "print(list(range(10)))\n", |
| 81 | + "\n", |
| 82 | + "print(list(range(2, 8)))\n", |
| 83 | + "\n", |
| 84 | + "print(list(range(2, 20, 3)))" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "markdown", |
| 89 | + "metadata": {}, |
| 90 | + "source": [ |
| 91 | + "## 3. Using range() with for loops\n", |
| 92 | + "We can use the range() function in for loops to iterate through a sequence of numbers. It can be combined with the len() function to iterate through a sequence using indexing. Here is an example." |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + "cell_type": "code", |
| 97 | + "execution_count": 4, |
| 98 | + "metadata": {}, |
| 99 | + "outputs": [ |
| 100 | + { |
| 101 | + "name": "stdout", |
| 102 | + "output_type": "stream", |
| 103 | + "text": [ |
| 104 | + "I like pop\n", |
| 105 | + "I like rock\n", |
| 106 | + "I like jazz\n" |
| 107 | + ] |
| 108 | + } |
| 109 | + ], |
| 110 | + "source": [ |
| 111 | + "# Program to iterate through a list using indexing\n", |
| 112 | + "\n", |
| 113 | + "genre = ['pop', 'rock', 'jazz']\n", |
| 114 | + "\n", |
| 115 | + "# iterate over the list using index\n", |
| 116 | + "for i in range(len(genre)):\n", |
| 117 | + " print(\"I like\", genre[i])" |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "cell_type": "markdown", |
| 122 | + "metadata": {}, |
| 123 | + "source": [ |
| 124 | + "## 4. Combining loops with if/else\n", |
| 125 | + "A `for` loop can have an optional `else` block as well. The `else` part is executed if the items in the sequence used in for loop exhausts.\n", |
| 126 | + "\n", |
| 127 | + "The `break` keyword can be used to stop a for loop. In such cases, the else part is ignored.\n", |
| 128 | + "\n", |
| 129 | + "Hence, a for loop's else part runs if no break occurs.\n", |
| 130 | + "\n", |
| 131 | + "Here is an example to illustrate this." |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "code", |
| 136 | + "execution_count": 6, |
| 137 | + "metadata": {}, |
| 138 | + "outputs": [ |
| 139 | + { |
| 140 | + "name": "stdout", |
| 141 | + "output_type": "stream", |
| 142 | + "text": [ |
| 143 | + "0\n", |
| 144 | + "1\n", |
| 145 | + "5\n", |
| 146 | + "No items left.\n" |
| 147 | + ] |
| 148 | + } |
| 149 | + ], |
| 150 | + "source": [ |
| 151 | + "digits = [0, 1, 5]\n", |
| 152 | + "\n", |
| 153 | + "for i in digits:\n", |
| 154 | + " print(i)\n", |
| 155 | + "else:\n", |
| 156 | + " print(\"No items left.\")" |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": 7, |
| 162 | + "metadata": {}, |
| 163 | + "outputs": [ |
| 164 | + { |
| 165 | + "name": "stdout", |
| 166 | + "output_type": "stream", |
| 167 | + "text": [ |
| 168 | + "No entry with that name found.\n" |
| 169 | + ] |
| 170 | + } |
| 171 | + ], |
| 172 | + "source": [ |
| 173 | + "# program to display student's marks from record\n", |
| 174 | + "student_name = 'Soyuj'\n", |
| 175 | + "\n", |
| 176 | + "marks = {'James': 90, 'Jules': 55, 'Arthur': 77}\n", |
| 177 | + "\n", |
| 178 | + "for student in marks:\n", |
| 179 | + " if student == student_name:\n", |
| 180 | + " print(marks[student])\n", |
| 181 | + " break\n", |
| 182 | + "else:\n", |
| 183 | + " print('No entry with that name found.')" |
| 184 | + ] |
| 185 | + }, |
| 186 | + { |
| 187 | + "cell_type": "code", |
| 188 | + "execution_count": null, |
| 189 | + "metadata": {}, |
| 190 | + "outputs": [], |
| 191 | + "source": [] |
| 192 | + } |
| 193 | + ], |
| 194 | + "metadata": { |
| 195 | + "kernelspec": { |
| 196 | + "display_name": "Python 3", |
| 197 | + "language": "python", |
| 198 | + "name": "python3" |
| 199 | + }, |
| 200 | + "language_info": { |
| 201 | + "codemirror_mode": { |
| 202 | + "name": "ipython", |
| 203 | + "version": 3 |
| 204 | + }, |
| 205 | + "file_extension": ".py", |
| 206 | + "mimetype": "text/x-python", |
| 207 | + "name": "python", |
| 208 | + "nbconvert_exporter": "python", |
| 209 | + "pygments_lexer": "ipython3", |
| 210 | + "version": "3.8.5" |
| 211 | + } |
| 212 | + }, |
| 213 | + "nbformat": 4, |
| 214 | + "nbformat_minor": 4 |
| 215 | +} |
0 commit comments