conditionals/script.rst
changeset 315 7944a4504769
child 437 0840aa06d2e6
equal deleted inserted replaced
314:11869b16d86b 315:7944a4504769
       
     1 .. Objectives
       
     2 .. ----------
       
     3 
       
     4 .. Clearly state the objectives of the LO (along with RBT level)
       
     5 
       
     6 .. Prerequisites
       
     7 .. -------------
       
     8 
       
     9 ..   1. Name of LO-1
       
    10 ..   2. Name of LO-2
       
    11 ..   3. Name of LO-3
       
    12      
       
    13 .. Author              : Madhu
       
    14    Internal Reviewer   : 
       
    15    External Reviewer   :
       
    16    Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
       
    17 
       
    18 
       
    19 Script
       
    20 ------
       
    21 
       
    22 {{{ Show the slide containing the title }}}
       
    23 
       
    24 Hello friends. Welcome to this spoken tutorial on Getting started with
       
    25 strings.
       
    26 
       
    27 {{{ Show the slide containing the outline }}}
       
    28 
       
    29 In this tutorial, we will learn the basic conditional constructs
       
    30 available in Python. We learn the if/else, if/elif/else and ternary
       
    31 conditional constructs available in Python. 
       
    32 
       
    33 {{{ Shift to terminal and start ipython }}}
       
    34 
       
    35 To begin with let us start ipython, by typing::
       
    36 
       
    37   ipython
       
    38 
       
    39 on the terminal
       
    40 
       
    41 Whenever we have two possible states that can occur depending on a
       
    42 whether a certain condition we can use if/else construct in
       
    43 Python. Say for example we have a variable "a" which stores integers
       
    44 and we are required to find out whether the value of the variable "a"
       
    45 is an even number or an odd number. To test out conditional statements
       
    46 as an example, let us say the value of the variable "a" is 5::
       
    47 
       
    48   a = 5
       
    49 
       
    50 In such a case we can write the if/else block as::
       
    51 
       
    52   if a % 2 == 0:
       
    53       print "Even"
       
    54   else:
       
    55       print "Odd"
       
    56 
       
    57 When the value of the variable "a" is divided by 2 and the remainder
       
    58 is 0 i.e. the result of the operation "a modulo 2" is 0 the condition
       
    59 "a % 2 == 0" evaluates to True, so the code within the if block gets
       
    60 executed. This means that the value of "a" is Even. 
       
    61 
       
    62 If the operation "a modulo 2" is not 0 the condition "a % 2 == 0"
       
    63 evaluates to False and hence the code block within else gets executed
       
    64 which means that the value of "a" is Odd. 
       
    65 
       
    66 Note in such a case only one of the two blocks get executed depending
       
    67 on whether the condition is True or False.
       
    68 
       
    69 There is a very important sytactic element to understand here. All the
       
    70 statements which are inside a certain code block are indented by 4
       
    71 spaces. The statement which starts a new code block after it, i.e. the
       
    72 if statement in this example ends with a colon (:). So the next
       
    73 immediate line will be inside the if block and hence indented by 4
       
    74 spaces. To come out of the code block we have to come back to the
       
    75 previous indentation level as shown in the else line here. Again the
       
    76 line following else will be in a new block so else line ends with a
       
    77 colon and the following block of code is indented by 4.
       
    78 
       
    79 As we use if/else statement when we have a condition which can take
       
    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 
       
    88   a = -3
       
    89 
       
    90   if a > 0:
       
    91       print "positive"
       
    92   elif a < 0:
       
    93       print "negative"
       
    94   else:
       
    95       print "zero"
       
    96 
       
    97 This if/elif/else ladder is self explanatory. All the syntax and rules
       
    98 as said for if/else statements hold. The only addition here is the
       
    99 elif statement which can have another condition of its own.
       
   100 
       
   101 Here, exactly one block of code is executed and that block of code
       
   102 corresponds to the condition which first evaluates to True. Even if
       
   103 there is a situation where multiple conditions evaluate to True all
       
   104 the subsequent conditions other than the first one which evaluates to
       
   105 True are neglected. Consequently, the else block gets executed if and
       
   106 only if all the conditions evaluate to False.
       
   107 
       
   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
       
   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
       
   112 
       
   113 {{{ Show slide for this }}}
       
   114 
       
   115   if user == 'admin':
       
   116       # Do admin operations
       
   117   elif user == 'moderator':
       
   118       # Do moderator operations
       
   119   elif user == 'client':
       
   120       # Do customer operations
       
   121 
       
   122 {{{ end of slide switch to ipython }}}
       
   123 
       
   124 is completely valid. Note that there are multiple elif blocks and there
       
   125 is no else block.
       
   126 
       
   127 In addition to these conditional statements, Python provides a very
       
   128 convenient ternary conditional operator. Let us take the following
       
   129 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
       
   131 of 0 to 100 or 'AA' if the student is absent. In such a case to obtain
       
   132 the marks as an integer we can use the ternary conditional
       
   133 operator. Let us say the string score is stored in score_str
       
   134 variable::
       
   135 
       
   136   score_str = 'AA'
       
   137 
       
   138 Now let us use the ternary conditional operator::
       
   139 
       
   140   score = int(score_str) if score_str != 'AA' else 0
       
   141 
       
   142 This is just the if/else statement block which written in a more
       
   143 convenient form and is very helpful when we have only one statement
       
   144 for each block. This conditional statement effectively means as we
       
   145 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
       
   147 it is 0. This means that we make the scores of the students who were
       
   148 absent for the exam 0.
       
   149 
       
   150 Moving on, there are certain situations where we will have to no
       
   151 operations or statements within the block of code. For example, we
       
   152 have a code where we are waiting for the keyboard input. If the user
       
   153 enters "s" as the input we would perform some operation nothing
       
   154 otherwise. In such cases "pass" statement comes very handy::
       
   155 
       
   156   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: ")
       
   158 
       
   159   if a == 'c':
       
   160      # Calculate the marks and exit
       
   161   elif a == 'd':
       
   162      # Display the results and exit
       
   163   elif a == 'x':
       
   164      # Exit the program
       
   165   else:
       
   166      pass
       
   167 
       
   168 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
       
   170 nothing. So "pass" statement can be used as a null operation
       
   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
       
   173 to be filled up later.
       
   174 
       
   175 {{{ Show summary slide }}}
       
   176 
       
   177 This brings us to the end of the tutorial session on conditional
       
   178 statements in Python. In this tutorial session we learnt
       
   179 
       
   180   * What are conditional statements
       
   181   * if/else statement
       
   182   * if/elif/else statement
       
   183   * Ternary conditional statement - C if X else Y
       
   184   * and the "pass" statement
       
   185 
       
   186 {{{ Show the "sponsored by FOSSEE" slide }}}
       
   187 
       
   188 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   189 
       
   190 Hope you have enjoyed and found it useful.
       
   191 Thank you!
       
   192