conditionals/script.rst
author Puneeth Chaganti <punchagan@fossee.in>
Wed, 10 Nov 2010 12:32:16 +0530
changeset 437 0840aa06d2e6
parent 315 7944a4504769
child 438 4523b2048663
permissions -rw-r--r--
Parially cleaned up conditionals LO.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
315
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     1
.. Objectives
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     2
.. ----------
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     3
437
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
     4
.. By the end of this tutorial, you will be able to 
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
     5
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
     6
.. * Use if/else blocks 
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
     7
.. * Use if/elif/else blocks
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
     8
.. * Use the Ternary conditional statement - C if X else Y
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
     9
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    10
.. to check conditions in your programs. 
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    11
315
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    12
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    13
.. Prerequisites
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    14
.. -------------
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    15
437
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    16
..   1. Basic datatypes and operators
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    17
315
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    18
     
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    19
.. Author              : Madhu
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    20
   Internal Reviewer   : 
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    21
   External Reviewer   :
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    22
   Checklist OK?       : <put date stamp here, if OK> [2010-10-05]
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    23
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    24
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    25
Script
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    26
------
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    27
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    28
{{{ Show the slide containing the title }}}
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    29
437
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    30
Hello friends. Welcome to this spoken tutorial on Conditionals
315
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    31
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    32
{{{ Show the slide containing the outline }}}
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    33
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    34
In this tutorial, we will learn the basic conditional constructs
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    35
available in Python. We learn the if/else, if/elif/else and ternary
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    36
conditional constructs available in Python. 
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    37
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    38
{{{ Shift to terminal and start ipython }}}
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    39
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    40
To begin with let us start ipython, by typing::
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    41
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    42
  ipython
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    43
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    44
on the terminal
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    46
Whenever we have two possible states that can occur depending on a
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    47
whether a certain condition we can use if/else construct in
437
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    48
Python. 
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    49
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    50
For example, say, we have a variable ``a`` which stores integers and
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    51
we are required to find out whether ``a`` is even or odd.  an even
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    52
number or an odd number. Let's say the value of ``a`` is 5, now.
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    53
::
315
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    54
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    55
  a = 5
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    56
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    57
In such a case we can write the if/else block as::
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    58
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    59
  if a % 2 == 0:
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    60
      print "Even"
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    61
  else:
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    62
      print "Odd"
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    63
437
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    64
If ``a`` is divisible by 2, i.e., the result of "a modulo 2" is 0, it
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    65
prints "Even", otherwise it prints "Odd". 
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    66
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    67
Note that in such a case, only one of the two blocks gets executed
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    68
depending on whether the condition is ``True`` or ``False``.
315
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    69
437
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    70
There is a very important sytactic element to understand here. Every
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    71
code block begins with a line that ends with a ``:``, in this example
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    72
the ``if`` and the ``else`` lines. Also, all the statements inside a
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    73
code block are intended by 4 spaces. Returning to the previous
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    74
indentation level, ends the code block. 
315
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    75
437
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    76
The if/else blocks work for a condition, which can take one of two
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    77
states. What do we do for conditions, which can take more than two
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    78
states? 
315
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    79
437
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    80
Python provides if/elif/else blocks, for such conditions. Let us take
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    81
an example. We have a variable ``a`` which holds integer values. We
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    82
need to print "positive" if ``a`` is positive, "negative" if
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    83
it is negative or "zero" if it is 0. 
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    84
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    85
Let us use if/elif/else ladder for it. For the purposes of testing our
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    86
code let us assume that the value of a is -3::
315
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    87
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    88
  a = -3
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    89
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    90
  if a > 0:
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    91
      print "positive"
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    92
  elif a < 0:
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    93
      print "negative"
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    94
  else:
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    95
      print "zero"
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    96
437
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    97
All the syntax and rules as said for if/else statements hold. The only
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    98
addition here is the ``elif`` statement which can have another
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
    99
condition of its own.
315
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   100
437
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   101
Here too, exactly one block of code is executed -- the block of code
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   102
which first evaluates to ``True``. Even if there is a situation where
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   103
multiple conditions evaluate to True all the subsequent conditions
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   104
other than the first one which evaluates to True are neglected.
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   105
Consequently, the else block gets executed if and only if all the
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   106
conditions evaluate to False.
315
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   107
437
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   108
Also, the ``else`` block in both if/else statement and if/elif/else is
315
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   109
optional. We can have a single if statement or just if/elif statements
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   110
without having else block at all. Also, there can be any number of
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   111
elif's within an if/elif/else ladder. For example
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   112
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   113
{{{ Show slide for this }}}
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   114
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   115
  if user == 'admin':
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   116
      # Do admin operations
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   117
  elif user == 'moderator':
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   118
      # Do moderator operations
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   119
  elif user == 'client':
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   120
      # Do customer operations
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   121
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   122
{{{ end of slide switch to ipython }}}
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   123
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   124
is completely valid. Note that there are multiple elif blocks and there
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   125
is no else block.
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   126
437
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   127
Following is an exercise that you must do. 
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   128
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   129
%% %% 
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   130
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   131
Please, pause the video here. Do the exercise and then continue. 
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   132
315
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   133
In addition to these conditional statements, Python provides a very
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   134
convenient ternary conditional operator. Let us take the following
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   135
example where we read the marks data from a data file which is
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   136
obtained as a string as we read a file. The marks can be in the range
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   137
of 0 to 100 or 'AA' if the student is absent. In such a case to obtain
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   138
the marks as an integer we can use the ternary conditional
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   139
operator. Let us say the string score is stored in score_str
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   140
variable::
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   141
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   142
  score_str = 'AA'
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   143
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   144
Now let us use the ternary conditional operator::
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   145
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   146
  score = int(score_str) if score_str != 'AA' else 0
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   147
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   148
This is just the if/else statement block which written in a more
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   149
convenient form and is very helpful when we have only one statement
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   150
for each block. This conditional statement effectively means as we
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   151
would have exactly specified in the English language which will be
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   152
like score is integer of score_str is score_str is not 'AA' otherwise
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   153
it is 0. This means that we make the scores of the students who were
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   154
absent for the exam 0.
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   155
437
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   156
Following is an exercise that you must do. 
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   157
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   158
%% %%     
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   159
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   160
Please, pause the video here. Do the exercise and then continue. 
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   161
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   162
Moving on, there are certain situations where we will have no
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   163
operations or statements within a block of code. For example, we have
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   164
a code where we are waiting for the keyboard input. If the user enters
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   165
"c", "d" or "x" as the input we would perform some operation nothing
315
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   166
otherwise. In such cases "pass" statement comes very handy::
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   167
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   168
  a = raw_input("Enter 'c' to calculate and exit, 'd' to display the existing
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   169
  results exit and 'x' to exit and any other key to continue: ")
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   170
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   171
  if a == 'c':
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   172
     # Calculate the marks and exit
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   173
  elif a == 'd':
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   174
     # Display the results and exit
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   175
  elif a == 'x':
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   176
     # Exit the program
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   177
  else:
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   178
     pass
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   179
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   180
In this case "pass" statement acts as a place holder for the block of
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   181
code. It is equivalent to a null operation. It literally does
437
0840aa06d2e6 Parially cleaned up conditionals LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 315
diff changeset
   182
nothing. It can used as a place holder when the actual code
315
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   183
implementation for a particular block of code is not known yet but has
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   184
to be filled up later.
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   185
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   186
{{{ Show summary slide }}}
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   187
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   188
This brings us to the end of the tutorial session on conditional
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   189
statements in Python. In this tutorial session we learnt
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   190
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   191
  * What are conditional statements
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   192
  * if/else statement
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   193
  * if/elif/else statement
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   194
  * Ternary conditional statement - C if X else Y
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   195
  * and the "pass" statement
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   196
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   197
{{{ Show the "sponsored by FOSSEE" slide }}}
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   198
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   199
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   200
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   201
Hope you have enjoyed and found it useful.
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   202
Thank you!
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   203