1 ======== |
1 ======== |
2 Script |
2 Script |
3 ======== |
3 ======== |
4 |
4 |
|
5 {{{ show the welcome slide }}} |
|
6 |
|
7 Welcome this tutorial on loops in Python. |
|
8 |
|
9 {{{ show the outline slide }}} |
|
10 |
|
11 In this tutorial, we shall look at ``while`` and ``for`` loops. We |
|
12 shall then look at the ``break``, ``continue`` and ``pass`` keywords |
|
13 and how to use them. |
|
14 |
|
15 {{{ switch to the ipython terminal }}} |
|
16 |
|
17 We have an ``ipython`` terminal, that we shall use through out this |
|
18 tutorial. |
|
19 |
|
20 We shall first begin with the ``while`` loop. The ``while`` loop is |
|
21 used for repeated execution as long as a condition is ``True``. |
|
22 |
|
23 Let us print the squares of all the odd numbers less than 10, using |
|
24 the ``while`` loop. |
|
25 |
|
26 :: |
|
27 |
|
28 i = 1 |
|
29 |
|
30 while i<10: |
|
31 print i*i |
|
32 i += 2 |
|
33 |
|
34 This loop prints the squares of the odd numbers below 10. |
|
35 |
|
36 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 |
|
38 other block in Python, the code within the ``while`` block is indented |
|
39 to the right by 4 spaces. |
|
40 |
|
41 E%% %% Pause the video here and write a ``while`` loop to print the |
|
42 squares of all the even numbers below 10. Then, return to the video. |
|
43 |
|
44 :: |
|
45 |
|
46 i = 2 |
|
47 |
|
48 while i<10: |
|
49 print i*i |
|
50 i += 2 |
|
51 |
|
52 Let us now solve the same problem of printing the squares of all odd |
|
53 numbers less than 10, using the ``for`` loop. As we know, the ``for`` |
|
54 loop iterates over a list or any other sequential data type. So, we |
|
55 use the ``range`` function to get a list of odd numbers below 10, and |
|
56 then iterate over it and print the required stuff. |
|
57 |
|
58 :: |
|
59 |
|
60 for n in range(1, 10, 2): |
|
61 print n*n |
|
62 |
|
63 E%% %% Pause the video here and write a ``for`` loop to print the |
|
64 squares of all the even numbers below 10. Then, return to the video. |
|
65 |
|
66 :: |
|
67 |
|
68 for n in range(2, 10, 2): |
|
69 print n*n |
|
70 |
|
71 Let us now look at how to use the keywords, ``pass``, ``break`` and |
|
72 ``continue``. |
|
73 |
|
74 As we already know, ``pass`` is just a syntactic filler. It is used |
|
75 for the sake of completion of blocks, that do not have any code within |
|
76 them. |
|
77 |
|
78 :: |
|
79 |
|
80 for n in range(2, 10, 2): |
|
81 pass |
|
82 |
|
83 ``break`` is used to break out of the innermost loop. The ``while`` |
|
84 loop to print the squares of all the odd numbers below 10, can be |
|
85 modified using the ``break`` statement, as follows |
|
86 :: |
|
87 |
|
88 i = 1 |
|
89 |
|
90 while True: |
|
91 print i*i |
|
92 i += 2 |
|
93 if i<10: |
|
94 break |
|
95 |
|
96 ``continue`` is used to skip execution of the rest of the loop on this |
|
97 iteration and continue to the end of this iteration. |
|
98 |
|
99 Say, we wish to print the squares of all the odd numbers below 10, |
|
100 which are not multiples of 3, we would modify the for loop as follows. |
|
101 :: |
|
102 |
|
103 for n in range(1, 10, 2): |
|
104 if n%3 == 0: |
|
105 continue |
|
106 print n*n |
|
107 |
|
108 |
|
109 E%% %%Pause the video here and using the ``continue`` keyword modify |
|
110 the ``for`` loop to print the squares of even numbers below 10, to |
|
111 print the squares of only multiples of 4. (Do not modify the range |
|
112 function call.) Then, resume the video. |
|
113 :: |
|
114 |
|
115 for n in range(2, 10, 2): |
|
116 if n%4: |
|
117 continue |
|
118 print n*n |
|
119 |
|
120 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 |
|
122 ``pass``, ``break`` and ``continue``. |
|
123 |
|
124 Thank You! |