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