conditionals/script.rst
changeset 438 4523b2048663
parent 437 0840aa06d2e6
equal deleted inserted replaced
437:0840aa06d2e6 438:4523b2048663
   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 
   126 
   127 Following is an exercise that you must do. 
   127 Following is an exercise that you must do. 
   128 
   128 
   129 %% %% 
   129 %% %% Given a number, num. Write an if else block to print num, as is,
       
   130       if it is divisible by 10, else print 10 * num.                  
   130 
   131 
   131 Please, pause the video here. Do the exercise and then continue. 
   132 Please, pause the video here. Do the exercise and then continue. 
       
   133 
       
   134 :: 
       
   135 
       
   136   if num%10 == 0:
       
   137       print num
       
   138   else:
       
   139       print 10*num
       
   140 
   132 
   141 
   133 In addition to these conditional statements, Python provides a very
   142 In addition to these conditional statements, Python provides a very
   134 convenient ternary conditional operator. Let us take the following
   143 convenient ternary conditional operator. Let us take the following
   135 example where we read the marks data from a data file which is
   144 example where we read the marks data from a data file which is
   136 obtained as a string as we read a file. The marks can be in the range
   145 obtained as a string as we read a file. The marks can be in the range
   153 it is 0. This means that we make the scores of the students who were
   162 it is 0. This means that we make the scores of the students who were
   154 absent for the exam 0.
   163 absent for the exam 0.
   155 
   164 
   156 Following is an exercise that you must do. 
   165 Following is an exercise that you must do. 
   157 
   166 
   158 %% %%     
   167 %% %% Given a number, num. Write a ternary operator to print num, as is,
       
   168       if it is divisible by 10, else print 10 * num. 
   159 
   169 
   160 Please, pause the video here. Do the exercise and then continue. 
   170 Please, pause the video here. Do the exercise and then continue. 
       
   171 
       
   172 :: 
       
   173 
       
   174    print num if num%10 == 0 else 10*num
   161 
   175 
   162 Moving on, there are certain situations where we will have no
   176 Moving on, there are certain situations where we will have no
   163 operations or statements within a block of code. For example, we have
   177 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
   178 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
   179 "c", "d" or "x" as the input we would perform some operation nothing