1 ======== |
1 .. Objectives |
2 Script |
2 .. ---------- |
3 ======== |
|
4 |
3 |
5 {{{ show the welcome slide }}} |
4 .. By the end of this tutorial, you will be able to |
6 |
5 |
7 Welcome this tutorial on loops in Python. |
6 .. 1. use the ``for`` loop |
|
7 .. #. use the ``while`` loop |
|
8 .. #. Use ``break``, ``continue`` and ``pass`` statements to play around |
|
9 .. with loops. |
8 |
10 |
9 {{{ show the outline slide }}} |
11 .. Prerequisites |
|
12 .. ------------- |
|
13 |
|
14 .. 1. getting started with ipython |
|
15 .. #. getting started with for |
|
16 .. #. conditionals |
|
17 |
|
18 |
|
19 .. Author : |
|
20 Internal Reviewer : |
|
21 External Reviewer : |
|
22 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
|
23 |
|
24 Script |
|
25 ------ |
|
26 |
|
27 {{{ Show the slide containing title }}} |
|
28 |
|
29 Hello Friends. Welcome this tutorial on loops in Python. |
|
30 |
|
31 {{{ Show the outline slide }}} |
10 |
32 |
11 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 |
12 shall then look at the ``break``, ``continue`` and ``pass`` keywords |
34 shall then look at the ``break``, ``continue`` and ``pass`` keywords |
13 and how to use them. |
35 and how to use them. |
14 |
36 |
36 The ``while`` loop, repeatedly checks if the condition is true and |
58 The ``while`` loop, repeatedly checks if the condition is true and |
37 executes the block of code within the loop, if it is. As with any |
59 executes the block of code within the loop, if it is. As with any |
38 other block in Python, the code within the ``while`` block is indented |
60 other block in Python, the code within the ``while`` block is indented |
39 to the right by 4 spaces. |
61 to the right by 4 spaces. |
40 |
62 |
41 E%% %% Pause the video here and write a ``while`` loop to print the |
63 Following is an exercise that you must do. |
42 squares of all the even numbers below 10. Then, return to the video. |
64 |
|
65 %%1%% Write a ``while`` loop to print the squares of all the even |
|
66 numbers below 10. |
|
67 |
|
68 Please, pause the video here. Do the exercise and then continue. |
43 |
69 |
44 :: |
70 :: |
45 |
71 |
46 i = 2 |
72 i = 2 |
47 |
73 |
58 :: |
84 :: |
59 |
85 |
60 for n in range(1, 10, 2): |
86 for n in range(1, 10, 2): |
61 print n*n |
87 print n*n |
62 |
88 |
63 E%% %% Pause the video here and write a ``for`` loop to print the |
89 Following is an exercise that you must do. |
64 squares of all the even numbers below 10. Then, return to the video. |
90 |
|
91 %%2%% Write a ``for`` loop to print the squares of all the even |
|
92 numbers below 10. |
|
93 |
|
94 Please, pause the video here. Do the exercise and then continue. |
65 |
95 |
66 :: |
96 :: |
67 |
97 |
68 for n in range(2, 10, 2): |
98 for n in range(2, 10, 2): |
69 print n*n |
99 print n*n |
104 if n%3 == 0: |
134 if n%3 == 0: |
105 continue |
135 continue |
106 print n*n |
136 print n*n |
107 |
137 |
108 |
138 |
109 E%% %%Pause the video here and using the ``continue`` keyword modify |
139 Following is an exercise that you must do. |
110 the ``for`` loop to print the squares of even numbers below 10, to |
140 |
111 print the squares of only multiples of 4. (Do not modify the range |
141 %%3%%Using the ``continue`` keyword modify the ``for`` loop to print |
112 function call.) Then, resume the video. |
142 the squares of even numbers below 10, to print the squares of only |
|
143 multiples of 4. (Do not modify the range function call.) |
|
144 |
|
145 Please, pause the video here. Do the exercise and then continue. |
113 :: |
146 :: |
114 |
147 |
115 for n in range(2, 10, 2): |
148 for n in range(2, 10, 2): |
116 if n%4: |
149 if n%4: |
117 continue |
150 continue |
118 print n*n |
151 print n*n |
119 |
152 |
|
153 {{{ Show summary slide }}} |
|
154 |
120 That brings us to the end of this tutorial. In this tutorial, we have |
155 That brings us to the end of this tutorial. In this tutorial, we have |
121 learnt about looping structures in Python and the use of the keywords |
156 learnt about looping structures in Python and the use of the keywords |
122 ``pass``, ``break`` and ``continue``. |
157 ``pass``, ``break`` and ``continue``. |
123 |
158 |
124 Thank You! |
159 {{{ Show the "sponsored by FOSSEE" slide }}} |
|
160 |
|
161 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
162 |
|
163 Hope you have enjoyed and found it useful. |
|
164 Thank you! |