conditionals/script.rst
author anand
Mon, 08 Nov 2010 01:36:47 +0530
changeset 455 f5b7d0b693d9
parent 315 7944a4504769
child 437 0840aa06d2e6
permissions -rw-r--r--
checklist OK
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
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     4
.. Clearly state the objectives of the LO (along with RBT level)
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     5
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     6
.. Prerequisites
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     7
.. -------------
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     8
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     9
..   1. Name of LO-1
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    10
..   2. Name of LO-2
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    11
..   3. Name of LO-3
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
.. Author              : Madhu
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    14
   Internal Reviewer   : 
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    15
   External Reviewer   :
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    16
   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
    17
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
Script
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    20
------
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    21
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    22
{{{ Show the slide containing the title }}}
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
Hello friends. Welcome to this spoken tutorial on Getting started with
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    25
strings.
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
{{{ Show the slide containing the outline }}}
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    28
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    29
In this tutorial, we will learn the basic conditional constructs
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    30
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
    31
conditional constructs available in Python. 
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    32
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    33
{{{ Shift to terminal and start ipython }}}
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    34
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    35
To begin with let us start ipython, by typing::
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    36
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    37
  ipython
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    38
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    39
on the terminal
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    40
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    41
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
    42
whether a certain condition we can use if/else construct in
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    43
Python. Say for example we have a variable "a" which stores integers
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    44
and we are required to find out whether the value of the variable "a"
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
is an even number or an odd number. To test out conditional statements
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    46
as an example, let us say the value of the variable "a" is 5::
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    47
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    48
  a = 5
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    49
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    50
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
    51
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    52
  if a % 2 == 0:
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    53
      print "Even"
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    54
  else:
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    55
      print "Odd"
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
When the value of the variable "a" is divided by 2 and the remainder
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    58
is 0 i.e. the result of the operation "a modulo 2" is 0 the condition
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    59
"a % 2 == 0" evaluates to True, so the code within the if block gets
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    60
executed. This means that the value of "a" is Even. 
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    61
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    62
If the operation "a modulo 2" is not 0 the condition "a % 2 == 0"
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    63
evaluates to False and hence the code block within else gets executed
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    64
which means that the value of "a" is Odd. 
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    65
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    66
Note in such a case only one of the two blocks get executed depending
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    67
on whether the condition is True or False.
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    68
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    69
There is a very important sytactic element to understand here. All the
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    70
statements which are inside a certain code block are indented by 4
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    71
spaces. The statement which starts a new code block after it, i.e. the
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    72
if statement in this example ends with a colon (:). So the next
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    73
immediate line will be inside the if block and hence indented by 4
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    74
spaces. To come out of the code block we have to come back to the
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    75
previous indentation level as shown in the else line here. Again the
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    76
line following else will be in a new block so else line ends with a
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    77
colon and the following block of code is indented by 4.
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    78
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    79
As we use if/else statement when we have a condition which can take
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    80
one of the two states, we may have conditions which can take more than
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    81
two states. In such a scenario Python provides if/elif/else
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    82
statements. Let us take an example. We have a variable "a" which holds
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    83
integer values. We need to print "positive" if the value of a is
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    84
positive, "negative" if it is negative and "zero" if the value of the
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    85
variable "a" is 0. Let us use if/elif/else ladder for it. For the
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    86
purposes of testing our code let us assume that the value of a is -3::
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
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    97
This if/elif/else ladder is self explanatory. All the syntax and rules
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    98
as said for if/else statements hold. The only addition here is the
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    99
elif statement which can have another condition of its own.
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   100
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   101
Here, exactly one block of code is executed and that block of code
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   102
corresponds to the condition which first evaluates to True. Even if
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   103
there is a situation where multiple conditions evaluate to True all
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   104
the subsequent conditions other than the first one which evaluates to
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   105
True are neglected. Consequently, the else block gets executed if and
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   106
only if all the conditions evaluate to False.
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   107
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   108
Also, the else block in both if/else statement and if/elif/else is
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
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   127
In addition to these conditional statements, Python provides a very
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   128
convenient ternary conditional operator. Let us take the following
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   129
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
   130
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
   131
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
   132
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
   133
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
   134
variable::
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   135
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   136
  score_str = 'AA'
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   137
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   138
Now let us use the ternary conditional operator::
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   139
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   140
  score = int(score_str) if score_str != 'AA' else 0
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
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
   143
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
   144
for each block. This conditional statement effectively means as we
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   145
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
   146
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
   147
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
   148
absent for the exam 0.
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   149
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   150
Moving on, there are certain situations where we will have to no
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   151
operations or statements within the block of code. For example, we
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   152
have a code where we are waiting for the keyboard input. If the user
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   153
enters "s" as the input we would perform some operation nothing
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   154
otherwise. In such cases "pass" statement comes very handy::
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   155
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   156
  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
   157
  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
   158
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   159
  if a == 'c':
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   160
     # Calculate the marks and exit
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   161
  elif a == 'd':
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   162
     # Display the results and exit
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   163
  elif a == 'x':
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   164
     # Exit the program
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   165
  else:
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   166
     pass
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
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
   169
code. It is equivalent to a null operation. It literally does
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   170
nothing. So "pass" statement can be used as a null operation
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   171
statement, or it can used as a place holder when the actual code
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   172
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
   173
to be filled up later.
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   174
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   175
{{{ Show summary slide }}}
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   176
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   177
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
   178
statements in Python. In this tutorial session we learnt
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
  * What are conditional statements
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   181
  * if/else statement
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   182
  * if/elif/else statement
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   183
  * Ternary conditional statement - C if X else Y
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   184
  * and the "pass" statement
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 the "sponsored by FOSSEE" 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 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
   189
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   190
Hope you have enjoyed and found it useful.
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   191
Thank you!
7944a4504769 Conditionals LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   192