author | Anoop Jacob Thomas<anoop@fossee.in> |
Sat, 18 Dec 2010 12:54:49 +0530 | |
changeset 524 | b602b4dcc87d |
parent 457 | 68813d8d80fb |
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 : |
452 | 22 |
Langauge Reviewer : Bhanukiran |
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
23 |
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
|
24 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
25 |
Script |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
26 |
------ |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
27 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
28 |
{{{ Show the slide containing title }}} |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
29 |
|
365
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
30 |
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
|
31 |
|
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
32 |
{{{ Show the outline slide }}} |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
33 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
34 |
In this tutorial, we shall look at ``while`` and ``for`` loops. We |
410
226b6e789da5
Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
365
diff
changeset
|
35 |
shall then look at how to break out of them, or skip some iterations |
226b6e789da5
Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
365
diff
changeset
|
36 |
in loops. |
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
37 |
|
365
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
38 |
.. #[[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
|
39 |
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
|
40 |
|
410
226b6e789da5
Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
365
diff
changeset
|
41 |
.. #[[punch: I think, we should have both of them. It gives a better |
226b6e789da5
Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
365
diff
changeset
|
42 |
.. context and comparison.] |
365
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, |
410
226b6e789da5
Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
365
diff
changeset
|
147 |
which are not multiples of 3, we would modify the ``for`` loop as |
226b6e789da5
Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
365
diff
changeset
|
148 |
follows. :: |
217
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 |
|
410
226b6e789da5
Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
365
diff
changeset
|
160 |
%%3%%Using the ``continue`` keyword modify the ``for`` loop, with the |
226b6e789da5
Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
365
diff
changeset
|
161 |
``range(2, 10, 2)``, to print the squares of even numbers below 10, to |
226b6e789da5
Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
365
diff
changeset
|
162 |
print the squares of only multiples of 4. |
365
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
163 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
164 |
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
|
165 |
|
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
166 |
{{{ switch to next slide after a seconds break}}} |
0fbe8a18587f
reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents:
278
diff
changeset
|
167 |
|
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
168 |
:: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
169 |
|
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
170 |
for n in range(2, 10, 2): |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
171 |
if n%4: |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
172 |
continue |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
173 |
print n*n |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
174 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
175 |
{{{ Show summary slide }}} |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
176 |
|
217
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
177 |
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
|
178 |
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
|
179 |
``pass``, ``break`` and ``continue``. |
b595f90016c5
Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff
changeset
|
180 |
|
253
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
181 |
{{{ Show the "sponsored by FOSSEE" slide }}} |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
182 |
|
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
183 |
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
|
184 |
|
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
185 |
Hope you have enjoyed and found it useful. |
8a117c6e75f1
Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents:
217
diff
changeset
|
186 |
Thank you! |