author | anand |
Sun, 07 Nov 2010 01:18:26 +0530 | |
changeset 390 | 69f285b3a5ed |
parent 365 | 0fbe8a18587f |
child 410 | 226b6e789da5 |
child 452 | 0a39cb296fb2 |
permissions | -rw-r--r-- |
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
1 |
.. Objectives |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
2 |
.. ---------- |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
3 |
|
254
c43677920502
added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
253
diff
changeset
|
4 |
.. By the end of this tutorial, you will be able to |
c43677920502
added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
253
diff
changeset
|
5 |
|
c43677920502
added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
253
diff
changeset
|
6 |
.. 1. use the ``for`` loop |
c43677920502
added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
253
diff
changeset
|
7 |
.. #. use the ``while`` loop |
c43677920502
added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
253
diff
changeset
|
8 |
.. #. Use ``break``, ``continue`` and ``pass`` statements to play around |
c43677920502
added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
253
diff
changeset
|
9 |
.. with loops. |
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
10 |
|
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
11 |
.. Prerequisites |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
12 |
.. ------------- |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
13 |
|
254
c43677920502
added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
253
diff
changeset
|
14 |
.. 1. getting started with ipython |
c43677920502
added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
253
diff
changeset
|
15 |
.. #. getting started with for |
c43677920502
added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
253
diff
changeset
|
16 |
.. #. conditionals |
c43677920502
added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
253
diff
changeset
|
17 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
18 |
|
365
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
19 |
.. Author : Puneeth |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
20 |
Internal Reviewer : Anoop Jacob Thomas<anoop@fossee.in> |
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
21 |
External Reviewer : |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
22 |
Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
23 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
24 |
Script |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
25 |
------ |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
26 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
27 |
{{{ Show the slide containing title }}} |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
28 |
|
365
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
29 |
Hello Friends. Welcome to the tutorial on loops in Python. |
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
30 |
|
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
31 |
{{{ Show the outline slide }}} |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
32 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
33 |
In this tutorial, we shall look at ``while`` and ``for`` loops. We |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
34 |
shall then look at the ``break``, ``continue`` and ``pass`` keywords |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
35 |
and how to use them. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
36 |
|
365
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
37 |
.. #[[Anoop: for loop is a pre-requisite and has been already covered, |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
38 |
so i think our emphasize can be on while loops]] |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
39 |
|
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
40 |
.. #[[Anoop: Instead of saying we will learn keywords pass, break and |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
41 |
continue, I think it is better to tell them that we will learn more |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
42 |
about loops]] |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
43 |
|
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
44 |
{{{ switch to the ipython terminal }}} |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
45 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
46 |
We have an ``ipython`` terminal, that we shall use through out this |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
47 |
tutorial. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
48 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
49 |
We shall first begin with the ``while`` loop. The ``while`` loop is |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
50 |
used for repeated execution as long as a condition is ``True``. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
51 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
52 |
Let us print the squares of all the odd numbers less than 10, using |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
53 |
the ``while`` loop. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
54 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
55 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
56 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
57 |
i = 1 |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
58 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
59 |
while i<10: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
60 |
print i*i |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
61 |
i += 2 |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
62 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
63 |
This loop prints the squares of the odd numbers below 10. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
64 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
65 |
The ``while`` loop, repeatedly checks if the condition is true and |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
66 |
executes the block of code within the loop, if it is. As with any |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
67 |
other block in Python, the code within the ``while`` block is indented |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
68 |
to the right by 4 spaces. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
69 |
|
365
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
70 |
{{{ switch to next slide }}} |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
71 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
72 |
Following is an exercise that you must do. |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
73 |
|
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
74 |
%%1%% Write a ``while`` loop to print the squares of all the even |
278
461a68dbefb1
Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
254
diff
changeset
|
75 |
numbers below 10. |
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
76 |
|
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
77 |
Please, pause the video here. Do the exercise and then continue. |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
78 |
|
365
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
79 |
{{{ switch to next slide after a seconds break}}} |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
80 |
|
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
81 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
82 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
83 |
i = 2 |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
84 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
85 |
while i<10: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
86 |
print i*i |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
87 |
i += 2 |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
88 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
89 |
Let us now solve the same problem of printing the squares of all odd |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
90 |
numbers less than 10, using the ``for`` loop. As we know, the ``for`` |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
91 |
loop iterates over a list or any other sequential data type. So, we |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
92 |
use the ``range`` function to get a list of odd numbers below 10, and |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
93 |
then iterate over it and print the required stuff. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
94 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
95 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
96 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
97 |
for n in range(1, 10, 2): |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
98 |
print n*n |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
99 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
100 |
Following is an exercise that you must do. |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
101 |
|
365
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
102 |
{{{ switch to next slide }}} |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
103 |
|
278
461a68dbefb1
Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
254
diff
changeset
|
104 |
%%2%% Write a ``for`` loop to print the squares of all the even |
461a68dbefb1
Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents:
254
diff
changeset
|
105 |
numbers below 10. |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
106 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
107 |
Please, pause the video here. Do the exercise and then continue. |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
108 |
|
365
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
109 |
{{{ switch to next slide after a seconds break }}} |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
110 |
|
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
111 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
112 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
113 |
for n in range(2, 10, 2): |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
114 |
print n*n |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
115 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
116 |
Let us now look at how to use the keywords, ``pass``, ``break`` and |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
117 |
``continue``. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
118 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
119 |
As we already know, ``pass`` is just a syntactic filler. It is used |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
120 |
for the sake of completion of blocks, that do not have any code within |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
121 |
them. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
122 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
123 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
124 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
125 |
for n in range(2, 10, 2): |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
126 |
pass |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
127 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
128 |
``break`` is used to break out of the innermost loop. The ``while`` |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
129 |
loop to print the squares of all the odd numbers below 10, can be |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
130 |
modified using the ``break`` statement, as follows |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
131 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
132 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
133 |
i = 1 |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
134 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
135 |
while True: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
136 |
print i*i |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
137 |
i += 2 |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
138 |
if i<10: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
139 |
break |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
140 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
141 |
``continue`` is used to skip execution of the rest of the loop on this |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
142 |
iteration and continue to the end of this iteration. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
143 |
|
365
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
144 |
.. #[[Anoop: should add slides for break, continue, pass]] |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
145 |
|
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
146 |
Say, we wish to print the squares of all the odd numbers below 10, |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
147 |
which are not multiples of 3, we would modify the for loop as follows. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
148 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
149 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
150 |
for n in range(1, 10, 2): |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
151 |
if n%3 == 0: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
152 |
continue |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
153 |
print n*n |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
154 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
155 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
156 |
Following is an exercise that you must do. |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
157 |
|
365
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
158 |
{{{ switch to next slide }}} |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
159 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
160 |
%%3%%Using the ``continue`` keyword modify the ``for`` loop to print |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
161 |
the squares of even numbers below 10, to print the squares of only |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
162 |
multiples of 4. (Do not modify the range function call.) |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
163 |
|
365
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
164 |
.. #[[Anoop: can you be more explicit/specific on do no modify say we |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
165 |
can ask them to use range(2, 10, 2) and solve the problem]] |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
166 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
167 |
Please, pause the video here. Do the exercise and then continue. |
365
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
168 |
|
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
169 |
{{{ switch to next slide after a seconds break}}} |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
170 |
|
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
171 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
172 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
173 |
for n in range(2, 10, 2): |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
174 |
if n%4: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
175 |
continue |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
176 |
print n*n |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
177 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
178 |
{{{ Show summary slide }}} |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
179 |
|
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
180 |
That brings us to the end of this tutorial. In this tutorial, we have |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
181 |
learnt about looping structures in Python and the use of the keywords |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
182 |
``pass``, ``break`` and ``continue``. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
183 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
184 |
{{{ Show the "sponsored by FOSSEE" slide }}} |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
185 |
|
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
186 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
187 |
|
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
188 |
Hope you have enjoyed and found it useful. |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
189 |
Thank you! |