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 }}} |
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 |