14 .. 1. getting started with ipython |
14 .. 1. getting started with ipython |
15 .. #. getting started with for |
15 .. #. getting started with for |
16 .. #. conditionals |
16 .. #. conditionals |
17 |
17 |
18 |
18 |
19 .. Author : |
19 .. Author : Puneeth |
20 Internal Reviewer : |
20 Internal Reviewer : Anoop Jacob Thomas<anoop@fossee.in> |
21 External Reviewer : |
21 External Reviewer : |
22 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
22 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
23 |
23 |
24 Script |
24 Script |
25 ------ |
25 ------ |
26 |
26 |
27 {{{ Show the slide containing title }}} |
27 {{{ Show the slide containing title }}} |
28 |
28 |
29 Hello Friends. Welcome this 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 the ``break``, ``continue`` and ``pass`` keywords |
35 and how to use them. |
35 and how to use them. |
|
36 |
|
37 .. #[[Anoop: for loop is a pre-requisite and has been already covered, |
|
38 so i think our emphasize can be on while loops]] |
|
39 |
|
40 .. #[[Anoop: Instead of saying we will learn keywords pass, break and |
|
41 continue, I think it is better to tell them that we will learn more |
|
42 about loops]] |
36 |
43 |
37 {{{ switch to the ipython terminal }}} |
44 {{{ switch to the ipython terminal }}} |
38 |
45 |
39 We have an ``ipython`` terminal, that we shall use through out this |
46 We have an ``ipython`` terminal, that we shall use through out this |
40 tutorial. |
47 tutorial. |
58 The ``while`` loop, repeatedly checks if the condition is true and |
65 The ``while`` loop, repeatedly checks if the condition is true and |
59 executes the block of code within the loop, if it is. As with any |
66 executes the block of code within the loop, if it is. As with any |
60 other block in Python, the code within the ``while`` block is indented |
67 other block in Python, the code within the ``while`` block is indented |
61 to the right by 4 spaces. |
68 to the right by 4 spaces. |
62 |
69 |
|
70 {{{ switch to next slide }}} |
|
71 |
63 Following is an exercise that you must do. |
72 Following is an exercise that you must do. |
64 |
73 |
65 %%1%% Write a ``while`` loop to print the squares of all the even |
74 %%1%% Write a ``while`` loop to print the squares of all the even |
66 numbers below 10. |
75 numbers below 10. |
67 |
76 |
68 Please, pause the video here. Do the exercise and then continue. |
77 Please, pause the video here. Do the exercise and then continue. |
|
78 |
|
79 {{{ switch to next slide after a seconds break}}} |
69 |
80 |
70 :: |
81 :: |
71 |
82 |
72 i = 2 |
83 i = 2 |
73 |
84 |
86 for n in range(1, 10, 2): |
97 for n in range(1, 10, 2): |
87 print n*n |
98 print n*n |
88 |
99 |
89 Following is an exercise that you must do. |
100 Following is an exercise that you must do. |
90 |
101 |
|
102 {{{ switch to next slide }}} |
|
103 |
91 %%2%% Write a ``for`` loop to print the squares of all the even |
104 %%2%% Write a ``for`` loop to print the squares of all the even |
92 numbers below 10. |
105 numbers below 10. |
93 |
106 |
94 Please, pause the video here. Do the exercise and then continue. |
107 Please, pause the video here. Do the exercise and then continue. |
|
108 |
|
109 {{{ switch to next slide after a seconds break }}} |
95 |
110 |
96 :: |
111 :: |
97 |
112 |
98 for n in range(2, 10, 2): |
113 for n in range(2, 10, 2): |
99 print n*n |
114 print n*n |
124 break |
139 break |
125 |
140 |
126 ``continue`` is used to skip execution of the rest of the loop on this |
141 ``continue`` is used to skip execution of the rest of the loop on this |
127 iteration and continue to the end of this iteration. |
142 iteration and continue to the end of this iteration. |
128 |
143 |
|
144 .. #[[Anoop: should add slides for break, continue, pass]] |
|
145 |
129 Say, we wish to print the squares of all the odd numbers below 10, |
146 Say, we wish to print the squares of all the odd numbers below 10, |
130 which are not multiples of 3, we would modify the for loop as follows. |
147 which are not multiples of 3, we would modify the for loop as follows. |
131 :: |
148 :: |
132 |
149 |
133 for n in range(1, 10, 2): |
150 for n in range(1, 10, 2): |
136 print n*n |
153 print n*n |
137 |
154 |
138 |
155 |
139 Following is an exercise that you must do. |
156 Following is an exercise that you must do. |
140 |
157 |
|
158 {{{ switch to next slide }}} |
|
159 |
141 %%3%%Using the ``continue`` keyword modify the ``for`` loop to print |
160 %%3%%Using the ``continue`` keyword modify the ``for`` loop to print |
142 the squares of even numbers below 10, to print the squares of only |
161 the squares of even numbers below 10, to print the squares of only |
143 multiples of 4. (Do not modify the range function call.) |
162 multiples of 4. (Do not modify the range function call.) |
144 |
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 |
145 Please, pause the video here. Do the exercise and then continue. |
167 Please, pause the video here. Do the exercise and then continue. |
|
168 |
|
169 {{{ switch to next slide after a seconds break}}} |
|
170 |
146 :: |
171 :: |
147 |
172 |
148 for n in range(2, 10, 2): |
173 for n in range(2, 10, 2): |
149 if n%4: |
174 if n%4: |
150 continue |
175 continue |