loops/script.rst
author anand
Thu, 11 Nov 2010 03:01:56 +0530
changeset 481 a4056cec859e
parent 410 226b6e789da5
child 457 68813d8d80fb
permissions -rw-r--r--
Internal Review not don for `getting starte with functions`
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   :
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
410
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 365
diff changeset
    34
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
    35
in loops.
217
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
410
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 365
diff changeset
    40
.. #[[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
    41
.. context and comparison.]
365
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
    42
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    43
{{{ switch to the ipython terminal }}}
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    44
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
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
    46
tutorial. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    47
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    48
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
    49
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
    50
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    51
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
    52
the ``while`` loop.
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    53
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
  i = 1
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
  while i<10:
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    59
      print i*i
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    60
      i += 2
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    61
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    62
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
    63
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    64
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
    65
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
    66
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
    67
to the right by 4 spaces. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    68
365
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
    69
{{{ switch to next slide }}}
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
    70
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    71
Following is an exercise that you must do. 
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    72
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    73
%%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
    74
numbers below 10. 
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    75
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    76
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
    77
365
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
    78
{{{ switch to next slide after a seconds break}}}
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
    79
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    80
::
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
  i = 2
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
  while i<10:
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    85
      print i*i
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    86
      i += 2
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    87
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    88
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
    89
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
    90
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
    91
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
    92
then iterate over it and print the required stuff. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    93
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
  for n in range(1, 10, 2):
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    97
      print n*n
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    98
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    99
Following is an exercise that you must do. 
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   100
365
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   101
{{{ switch to next slide }}}
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   102
278
461a68dbefb1 Slides for loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 254
diff changeset
   103
%%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
   104
numbers below 10. 
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   105
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   106
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
   107
365
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   108
{{{ switch to next slide after a seconds break }}}
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   109
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   110
::
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
  for n in range(2, 10, 2):
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   113
      print n*n
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   114
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   115
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
   116
``continue``.
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   117
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   118
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
   119
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
   120
them. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   121
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
  for n in range(2, 10, 2):
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   125
      pass
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   126
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   127
``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
   128
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
   129
modified using the ``break`` statement, as follows
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   130
::
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
  i = 1
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
  while True:
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   135
      print i*i
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   136
      i += 2
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   137
      if i<10:
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   138
          break
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   139
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   140
``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
   141
iteration and continue to the end of this iteration. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   142
365
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   143
.. #[[Anoop: should add slides for break, continue, pass]]
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   144
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   145
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
   146
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
   147
follows.  ::
217
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
  for n in range(1, 10, 2):
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   150
      if n%3 == 0:
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   151
          continue      
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   152
      print n*n
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   153
  
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   154
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   155
Following is an exercise that you must do. 
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   156
365
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   157
{{{ switch to next slide }}}
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   158
410
226b6e789da5 Minor changes to loops, input-output and advanced functions.
Puneeth Chaganti <punchagan@fossee.in>
parents: 365
diff changeset
   159
%%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
   160
``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
   161
print the squares of only multiples of 4.
365
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   162
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   163
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
   164
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   165
{{{ switch to next slide after a seconds break}}}
0fbe8a18587f reviewed the script loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 278
diff changeset
   166
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   167
::
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
  for n in range(2, 10, 2):
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   170
      if n%4:
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   171
          continue      
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   172
      print n*n
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   173
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   174
{{{ Show summary slide }}}
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   175
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   176
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
   177
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
   178
``pass``, ``break`` and ``continue``. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   179
253
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   180
{{{ Show the "sponsored by FOSSEE" slide }}}
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   181
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   182
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
   183
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   184
Hope you have enjoyed and found it useful.
8a117c6e75f1 Fixed loops LO script.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
   185
Thank you!