equal
deleted
inserted
replaced
29 Hello Friends. Welcome to the tutorial on loops in Python. |
29 Hello Friends. Welcome to the tutorial on loops in Python. |
30 |
30 |
31 {{{ Show the outline slide }}} |
31 {{{ Show the outline slide }}} |
32 |
32 |
33 In this tutorial, we shall look at ``while`` and ``for`` loops. We |
33 In this tutorial, we shall look at ``while`` and ``for`` loops. We |
34 shall then look at the ``break``, ``continue`` and ``pass`` keywords |
34 shall then look at how to break out of them, or skip some iterations |
35 and how to use them. |
35 in loops. |
36 |
36 |
37 .. #[[Anoop: for loop is a pre-requisite and has been already covered, |
37 .. #[[Anoop: for loop is a pre-requisite and has been already covered, |
38 so i think our emphasize can be on while loops]] |
38 so i think our emphasize can be on while loops]] |
39 |
39 |
40 .. #[[Anoop: Instead of saying we will learn keywords pass, break and |
40 .. #[[punch: I think, we should have both of them. It gives a better |
41 continue, I think it is better to tell them that we will learn more |
41 .. context and comparison.] |
42 about loops]] |
|
43 |
42 |
44 {{{ switch to the ipython terminal }}} |
43 {{{ switch to the ipython terminal }}} |
45 |
44 |
46 We have an ``ipython`` terminal, that we shall use through out this |
45 We have an ``ipython`` terminal, that we shall use through out this |
47 tutorial. |
46 tutorial. |
142 iteration and continue to the end of this iteration. |
141 iteration and continue to the end of this iteration. |
143 |
142 |
144 .. #[[Anoop: should add slides for break, continue, pass]] |
143 .. #[[Anoop: should add slides for break, continue, pass]] |
145 |
144 |
146 Say, we wish to print the squares of all the odd numbers below 10, |
145 Say, we wish to print the squares of all the odd numbers below 10, |
147 which are not multiples of 3, we would modify the for loop as follows. |
146 which are not multiples of 3, we would modify the ``for`` loop as |
148 :: |
147 follows. :: |
149 |
148 |
150 for n in range(1, 10, 2): |
149 for n in range(1, 10, 2): |
151 if n%3 == 0: |
150 if n%3 == 0: |
152 continue |
151 continue |
153 print n*n |
152 print n*n |
155 |
154 |
156 Following is an exercise that you must do. |
155 Following is an exercise that you must do. |
157 |
156 |
158 {{{ switch to next slide }}} |
157 {{{ switch to next slide }}} |
159 |
158 |
160 %%3%%Using the ``continue`` keyword modify the ``for`` loop to print |
159 %%3%%Using the ``continue`` keyword modify the ``for`` loop, with the |
161 the squares of even numbers below 10, to print the squares of only |
160 ``range(2, 10, 2)``, to print the squares of even numbers below 10, to |
162 multiples of 4. (Do not modify the range function call.) |
161 print the squares of only multiples of 4. |
163 |
|
164 .. #[[Anoop: can you be more explicit/specific on do no modify say we |
|
165 can ask them to use range(2, 10, 2) and solve the problem]] |
|
166 |
162 |
167 Please, pause the video here. Do the exercise and then continue. |
163 Please, pause the video here. Do the exercise and then continue. |
168 |
164 |
169 {{{ switch to next slide after a seconds break}}} |
165 {{{ switch to next slide after a seconds break}}} |
170 |
166 |