loops/script.rst
author bhanu
Mon, 08 Nov 2010 01:34:02 +0530
changeset 454 0c69c2b32183
parent 452 0a39cb296fb2
child 457 68813d8d80fb
permissions -rw-r--r--
Language check done for the script `strings`
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
0a39cb296fb2 checked the language for the script `loops`
bhanu
parents: 365
diff changeset
    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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    35
shall then look at the ``break``, ``continue`` and ``pass`` keywords
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    36
and how to use them. 
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
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
    41
.. #[[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
    42
   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
    43
   about loops]]
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
    44
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
{{{ switch to the ipython terminal }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    46
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    47
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
    48
tutorial. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    49
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    50
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
    51
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
    52
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    53
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
    54
the ``while`` loop.
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    58
  i = 1
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    59
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    60
  while i<10:
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    61
      print i*i
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    62
      i += 2
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    63
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    64
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
    65
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    66
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
    67
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
    68
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
    69
to the right by 4 spaces. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    70
365
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
    71
{{{ switch to next slide }}}
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
    72
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    73
Following is an exercise that you must do. 
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    74
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    75
%%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
    76
numbers below 10. 
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    77
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    78
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
    79
365
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
    80
{{{ switch to next slide after a seconds break}}}
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
    81
217
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    84
  i = 2
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    85
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    86
  while i<10:
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    87
      print i*i
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    88
      i += 2
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    89
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    90
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
    91
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
    92
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
    93
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
    94
then iterate over it and print the required stuff. 
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    98
  for n in range(1, 10, 2):
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    99
      print n*n
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   100
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   101
Following is an exercise that you must do. 
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   102
365
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   103
{{{ switch to next slide }}}
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   104
278
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 254
diff changeset
   105
%%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
   106
numbers below 10. 
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   107
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   108
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
   109
365
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   110
{{{ switch to next slide after a seconds break }}}
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   111
217
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   114
  for n in range(2, 10, 2):
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   115
      print n*n
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   116
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   117
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
   118
``continue``.
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   119
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   120
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
   121
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
   122
them. 
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   126
  for n in range(2, 10, 2):
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   127
      pass
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   128
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   129
``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
   130
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
   131
modified using the ``break`` statement, as follows
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   134
  i = 1
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   135
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   136
  while True:
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   137
      print i*i
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   138
      i += 2
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   139
      if i<10:
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   140
          break
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   141
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   142
``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
   143
iteration and continue to the end of this iteration. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   144
365
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   145
.. #[[Anoop: should add slides for break, continue, pass]]
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   146
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   147
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
   148
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
   149
::
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   150
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   151
  for n in range(1, 10, 2):
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   152
      if n%3 == 0:
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   153
          continue      
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   154
      print n*n
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   155
  
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   156
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   157
Following is an exercise that you must do. 
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   158
365
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   159
{{{ switch to next slide }}}
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   160
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   161
%%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
   162
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
   163
multiples of 4. (Do not modify the range function call.) 
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   164
365
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   165
.. #[[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
   166
   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
   167
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   168
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
   169
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   170
{{{ switch to next slide after a seconds break}}}
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   171
217
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
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   174
  for n in range(2, 10, 2):
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   175
      if n%4:
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   176
          continue      
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   177
      print n*n
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   178
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   179
{{{ Show summary slide }}}
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   180
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   181
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
   182
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
   183
``pass``, ``break`` and ``continue``. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   184
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   185
{{{ Show the "sponsored by FOSSEE" slide }}}
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   186
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   187
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
   188
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   189
Hope you have enjoyed and found it useful.
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   190
Thank you!