conditionals/script.rst
changeset 437 0840aa06d2e6
parent 315 7944a4504769
child 438 4523b2048663
equal deleted inserted replaced
436:e62bc810999c 437:0840aa06d2e6
     1 .. Objectives
     1 .. Objectives
     2 .. ----------
     2 .. ----------
     3 
     3 
     4 .. Clearly state the objectives of the LO (along with RBT level)
     4 .. By the end of this tutorial, you will be able to 
       
     5 
       
     6 .. * Use if/else blocks 
       
     7 .. * Use if/elif/else blocks
       
     8 .. * Use the Ternary conditional statement - C if X else Y
       
     9 
       
    10 .. to check conditions in your programs. 
       
    11 
     5 
    12 
     6 .. Prerequisites
    13 .. Prerequisites
     7 .. -------------
    14 .. -------------
     8 
    15 
     9 ..   1. Name of LO-1
    16 ..   1. Basic datatypes and operators
    10 ..   2. Name of LO-2
    17 
    11 ..   3. Name of LO-3
       
    12      
    18      
    13 .. Author              : Madhu
    19 .. Author              : Madhu
    14    Internal Reviewer   : 
    20    Internal Reviewer   : 
    15    External Reviewer   :
    21    External Reviewer   :
    16    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    22    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
    19 Script
    25 Script
    20 ------
    26 ------
    21 
    27 
    22 {{{ Show the slide containing the title }}}
    28 {{{ Show the slide containing the title }}}
    23 
    29 
    24 Hello friends. Welcome to this spoken tutorial on Getting started with
    30 Hello friends. Welcome to this spoken tutorial on Conditionals
    25 strings.
       
    26 
    31 
    27 {{{ Show the slide containing the outline }}}
    32 {{{ Show the slide containing the outline }}}
    28 
    33 
    29 In this tutorial, we will learn the basic conditional constructs
    34 In this tutorial, we will learn the basic conditional constructs
    30 available in Python. We learn the if/else, if/elif/else and ternary
    35 available in Python. We learn the if/else, if/elif/else and ternary
    38 
    43 
    39 on the terminal
    44 on the terminal
    40 
    45 
    41 Whenever we have two possible states that can occur depending on a
    46 Whenever we have two possible states that can occur depending on a
    42 whether a certain condition we can use if/else construct in
    47 whether a certain condition we can use if/else construct in
    43 Python. Say for example we have a variable "a" which stores integers
    48 Python. 
    44 and we are required to find out whether the value of the variable "a"
    49 
    45 is an even number or an odd number. To test out conditional statements
    50 For example, say, we have a variable ``a`` which stores integers and
    46 as an example, let us say the value of the variable "a" is 5::
    51 we are required to find out whether ``a`` is even or odd.  an even
       
    52 number or an odd number. Let's say the value of ``a`` is 5, now.
       
    53 ::
    47 
    54 
    48   a = 5
    55   a = 5
    49 
    56 
    50 In such a case we can write the if/else block as::
    57 In such a case we can write the if/else block as::
    51 
    58 
    52   if a % 2 == 0:
    59   if a % 2 == 0:
    53       print "Even"
    60       print "Even"
    54   else:
    61   else:
    55       print "Odd"
    62       print "Odd"
    56 
    63 
    57 When the value of the variable "a" is divided by 2 and the remainder
    64 If ``a`` is divisible by 2, i.e., the result of "a modulo 2" is 0, it
    58 is 0 i.e. the result of the operation "a modulo 2" is 0 the condition
    65 prints "Even", otherwise it prints "Odd". 
    59 "a % 2 == 0" evaluates to True, so the code within the if block gets
    66 
    60 executed. This means that the value of "a" is Even. 
    67 Note that in such a case, only one of the two blocks gets executed
    61 
    68 depending on whether the condition is ``True`` or ``False``.
    62 If the operation "a modulo 2" is not 0 the condition "a % 2 == 0"
    69 
    63 evaluates to False and hence the code block within else gets executed
    70 There is a very important sytactic element to understand here. Every
    64 which means that the value of "a" is Odd. 
    71 code block begins with a line that ends with a ``:``, in this example
    65 
    72 the ``if`` and the ``else`` lines. Also, all the statements inside a
    66 Note in such a case only one of the two blocks get executed depending
    73 code block are intended by 4 spaces. Returning to the previous
    67 on whether the condition is True or False.
    74 indentation level, ends the code block. 
    68 
    75 
    69 There is a very important sytactic element to understand here. All the
    76 The if/else blocks work for a condition, which can take one of two
    70 statements which are inside a certain code block are indented by 4
    77 states. What do we do for conditions, which can take more than two
    71 spaces. The statement which starts a new code block after it, i.e. the
    78 states? 
    72 if statement in this example ends with a colon (:). So the next
    79 
    73 immediate line will be inside the if block and hence indented by 4
    80 Python provides if/elif/else blocks, for such conditions. Let us take
    74 spaces. To come out of the code block we have to come back to the
    81 an example. We have a variable ``a`` which holds integer values. We
    75 previous indentation level as shown in the else line here. Again the
    82 need to print "positive" if ``a`` is positive, "negative" if
    76 line following else will be in a new block so else line ends with a
    83 it is negative or "zero" if it is 0. 
    77 colon and the following block of code is indented by 4.
    84 
    78 
    85 Let us use if/elif/else ladder for it. For the purposes of testing our
    79 As we use if/else statement when we have a condition which can take
    86 code let us assume that the value of a is -3::
    80 one of the two states, we may have conditions which can take more than
       
    81 two states. In such a scenario Python provides if/elif/else
       
    82 statements. Let us take an example. We have a variable "a" which holds
       
    83 integer values. We need to print "positive" if the value of a is
       
    84 positive, "negative" if it is negative and "zero" if the value of the
       
    85 variable "a" is 0. Let us use if/elif/else ladder for it. For the
       
    86 purposes of testing our code let us assume that the value of a is -3::
       
    87 
    87 
    88   a = -3
    88   a = -3
    89 
    89 
    90   if a > 0:
    90   if a > 0:
    91       print "positive"
    91       print "positive"
    92   elif a < 0:
    92   elif a < 0:
    93       print "negative"
    93       print "negative"
    94   else:
    94   else:
    95       print "zero"
    95       print "zero"
    96 
    96 
    97 This if/elif/else ladder is self explanatory. All the syntax and rules
    97 All the syntax and rules as said for if/else statements hold. The only
    98 as said for if/else statements hold. The only addition here is the
    98 addition here is the ``elif`` statement which can have another
    99 elif statement which can have another condition of its own.
    99 condition of its own.
   100 
   100 
   101 Here, exactly one block of code is executed and that block of code
   101 Here too, exactly one block of code is executed -- the block of code
   102 corresponds to the condition which first evaluates to True. Even if
   102 which first evaluates to ``True``. Even if there is a situation where
   103 there is a situation where multiple conditions evaluate to True all
   103 multiple conditions evaluate to True all the subsequent conditions
   104 the subsequent conditions other than the first one which evaluates to
   104 other than the first one which evaluates to True are neglected.
   105 True are neglected. Consequently, the else block gets executed if and
   105 Consequently, the else block gets executed if and only if all the
   106 only if all the conditions evaluate to False.
   106 conditions evaluate to False.
   107 
   107 
   108 Also, the else block in both if/else statement and if/elif/else is
   108 Also, the ``else`` block in both if/else statement and if/elif/else is
   109 optional. We can have a single if statement or just if/elif statements
   109 optional. We can have a single if statement or just if/elif statements
   110 without having else block at all. Also, there can be any number of
   110 without having else block at all. Also, there can be any number of
   111 elif's within an if/elif/else ladder. For example
   111 elif's within an if/elif/else ladder. For example
   112 
   112 
   113 {{{ Show slide for this }}}
   113 {{{ Show slide for this }}}
   121 
   121 
   122 {{{ end of slide switch to ipython }}}
   122 {{{ end of slide switch to ipython }}}
   123 
   123 
   124 is completely valid. Note that there are multiple elif blocks and there
   124 is completely valid. Note that there are multiple elif blocks and there
   125 is no else block.
   125 is no else block.
       
   126 
       
   127 Following is an exercise that you must do. 
       
   128 
       
   129 %% %% 
       
   130 
       
   131 Please, pause the video here. Do the exercise and then continue. 
   126 
   132 
   127 In addition to these conditional statements, Python provides a very
   133 In addition to these conditional statements, Python provides a very
   128 convenient ternary conditional operator. Let us take the following
   134 convenient ternary conditional operator. Let us take the following
   129 example where we read the marks data from a data file which is
   135 example where we read the marks data from a data file which is
   130 obtained as a string as we read a file. The marks can be in the range
   136 obtained as a string as we read a file. The marks can be in the range
   145 would have exactly specified in the English language which will be
   151 would have exactly specified in the English language which will be
   146 like score is integer of score_str is score_str is not 'AA' otherwise
   152 like score is integer of score_str is score_str is not 'AA' otherwise
   147 it is 0. This means that we make the scores of the students who were
   153 it is 0. This means that we make the scores of the students who were
   148 absent for the exam 0.
   154 absent for the exam 0.
   149 
   155 
   150 Moving on, there are certain situations where we will have to no
   156 Following is an exercise that you must do. 
   151 operations or statements within the block of code. For example, we
   157 
   152 have a code where we are waiting for the keyboard input. If the user
   158 %% %%     
   153 enters "s" as the input we would perform some operation nothing
   159 
       
   160 Please, pause the video here. Do the exercise and then continue. 
       
   161 
       
   162 Moving on, there are certain situations where we will have no
       
   163 operations or statements within a block of code. For example, we have
       
   164 a code where we are waiting for the keyboard input. If the user enters
       
   165 "c", "d" or "x" as the input we would perform some operation nothing
   154 otherwise. In such cases "pass" statement comes very handy::
   166 otherwise. In such cases "pass" statement comes very handy::
   155 
   167 
   156   a = raw_input("Enter 'c' to calculate and exit, 'd' to display the existing
   168   a = raw_input("Enter 'c' to calculate and exit, 'd' to display the existing
   157   results exit and 'x' to exit and any other key to continue: ")
   169   results exit and 'x' to exit and any other key to continue: ")
   158 
   170 
   165   else:
   177   else:
   166      pass
   178      pass
   167 
   179 
   168 In this case "pass" statement acts as a place holder for the block of
   180 In this case "pass" statement acts as a place holder for the block of
   169 code. It is equivalent to a null operation. It literally does
   181 code. It is equivalent to a null operation. It literally does
   170 nothing. So "pass" statement can be used as a null operation
   182 nothing. It can used as a place holder when the actual code
   171 statement, or it can used as a place holder when the actual code
       
   172 implementation for a particular block of code is not known yet but has
   183 implementation for a particular block of code is not known yet but has
   173 to be filled up later.
   184 to be filled up later.
   174 
   185 
   175 {{{ Show summary slide }}}
   186 {{{ Show summary slide }}}
   176 
   187