author | Nishanth <nishanth@fossee.in> |
Wed, 06 Oct 2010 16:15:42 +0530 | |
changeset 221 | 7cd975ff5f0d |
parent 171 | 6537f447efc0 |
permissions | -rw-r--r-- |
171
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
1 |
Hello friends. Welcome to this spoken tutorial on Getting started with |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
2 |
strings. |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
3 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
4 |
{{{ Show the slide containing the title }}} |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
5 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
6 |
{{{ Show the slide containing the outline }}} |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
7 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
8 |
In this tutorial, we will learn the basic conditional constructs |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
9 |
available in Python. We learn the if/else, if/elif/else and ternary |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
10 |
conditional constructs available in Python. |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
11 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
12 |
{{{ Shift to terminal and start ipython }}} |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
13 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
14 |
To begin with let us start ipython, by typing:: |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
15 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
16 |
ipython |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
17 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
18 |
on the terminal |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
19 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
20 |
Whenever we have two possible states that can occur depending on a |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
21 |
whether a certain condition we can use if/else construct in |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
22 |
Python. Say for example we have a variable "a" which stores integers |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
23 |
and we are required to find out whether the value of the variable "a" |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
24 |
is an even number or an odd number. To test out conditional statements |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
25 |
as an example, let us say the value of the variable "a" is 5:: |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
26 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
27 |
a = 5 |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
28 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
29 |
In such a case we can write the if/else block as:: |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
30 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
31 |
if a % 2 == 0: |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
32 |
print "Even" |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
33 |
else: |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
34 |
print "Odd" |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
35 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
36 |
When the value of the variable "a" is divided by 2 and the remainder |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
37 |
is 0 i.e. the result of the operation "a modulo 2" is 0 the condition |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
38 |
"a % 2 == 0" evaluates to True, so the code within the if block gets |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
39 |
executed. This means that the value of "a" is Even. |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
40 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
41 |
If the operation "a modulo 2" is not 0 the condition "a % 2 == 0" |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
42 |
evaluates to False and hence the code block within else gets executed |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
43 |
which means that the value of "a" is Odd. |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
44 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
45 |
Note in such a case only one of the two blocks get executed depending |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
46 |
on whether the condition is True or False. |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
47 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
48 |
There is a very important sytactic element to understand here. All the |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
49 |
statements which are inside a certain code block are indented by 4 |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
50 |
spaces. The statement which starts a new code block after it, i.e. the |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
51 |
if statement in this example ends with a colon (:). So the next |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
52 |
immediate line will be inside the if block and hence indented by 4 |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
53 |
spaces. To come out of the code block we have to come back to the |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
54 |
previous indentation level as shown in the else line here. Again the |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
55 |
line following else will be in a new block so else line ends with a |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
56 |
colon and the following block of code is indented by 4. |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
57 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
58 |
As we use if/else statement when we have a condition which can take |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
59 |
one of the two states, we may have conditions which can take more than |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
60 |
two states. In such a scenario Python provides if/elif/else |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
61 |
statements. Let us take an example. We have a variable "a" which holds |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
62 |
integer values. We need to print "positive" if the value of a is |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
63 |
positive, "negative" if it is negative and "zero" if the value of the |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
64 |
variable "a" is 0. Let us use if/elif/else ladder for it. For the |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
65 |
purposes of testing our code let us assume that the value of a is -3:: |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
66 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
67 |
a = -3 |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
68 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
69 |
if a > 0: |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
70 |
print "positive" |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
71 |
elif a < 0: |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
72 |
print "negative" |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
73 |
else: |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
74 |
print "zero" |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
75 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
76 |
This if/elif/else ladder is self explanatory. All the syntax and rules |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
77 |
as said for if/else statements hold. The only addition here is the |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
78 |
elif statement which can have another condition of its own. |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
79 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
80 |
Here, exactly one block of code is executed and that block of code |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
81 |
corresponds to the condition which first evaluates to True. Even if |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
82 |
there is a situation where multiple conditions evaluate to True all |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
83 |
the subsequent conditions other than the first one which evaluates to |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
84 |
True are neglected. Consequently, the else block gets executed if and |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
85 |
only if all the conditions evaluate to False. |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
86 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
87 |
Also, the else block in both if/else statement and if/elif/else is |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
88 |
optional. We can have a single if statement or just if/elif statements |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
89 |
without having else block at all. Also, there can be any number of |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
90 |
elif's within an if/elif/else ladder. For example |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
91 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
92 |
{{{ Show slide for this }}} |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
93 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
94 |
if user == 'admin': |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
95 |
# Do admin operations |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
96 |
elif user == 'moderator': |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
97 |
# Do moderator operations |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
98 |
elif user == 'client': |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
99 |
# Do customer operations |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
100 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
101 |
{{{ end of slide switch to ipython }}} |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
102 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
103 |
is completely valid. Note that there are multiple elif blocks and there |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
104 |
is no else block. |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
105 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
106 |
In addition to these conditional statements, Python provides a very |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
107 |
convenient ternary conditional operator. Let us take the following |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
108 |
example where we read the marks data from a data file which is |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
109 |
obtained as a string as we read a file. The marks can be in the range |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
110 |
of 0 to 100 or 'AA' if the student is absent. In such a case to obtain |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
111 |
the marks as an integer we can use the ternary conditional |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
112 |
operator. Let us say the string score is stored in score_str |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
113 |
variable:: |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
114 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
115 |
score_str = 'AA' |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
116 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
117 |
Now let us use the ternary conditional operator:: |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
118 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
119 |
score = int(score_str) if score_str != 'AA' else 0 |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
120 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
121 |
This is just the if/else statement block which written in a more |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
122 |
convenient form and is very helpful when we have only one statement |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
123 |
for each block. This conditional statement effectively means as we |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
124 |
would have exactly specified in the English language which will be |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
125 |
like score is integer of score_str is score_str is not 'AA' otherwise |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
126 |
it is 0. This means that we make the scores of the students who were |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
127 |
absent for the exam 0. |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
128 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
129 |
Moving on, there are certain situations where we will have to no |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
130 |
operations or statements within the block of code. For example, we |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
131 |
have a code where we are waiting for the keyboard input. If the user |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
132 |
enters "s" as the input we would perform some operation nothing |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
133 |
otherwise. In such cases "pass" statement comes very handy:: |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
134 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
135 |
a = raw_input("Enter 'c' to calculate and exit, 'd' to display the existing |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
136 |
results exit and 'x' to exit and any other key to continue: ") |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
137 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
138 |
if a == 'c': |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
139 |
# Calculate the marks and exit |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
140 |
elif a == 'd': |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
141 |
# Display the results and exit |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
142 |
elif a == 'x': |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
143 |
# Exit the program |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
144 |
else: |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
145 |
pass |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
146 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
147 |
In this case "pass" statement acts as a place holder for the block of |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
148 |
code. It is equivalent to a null operation. It literally does |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
149 |
nothing. So "pass" statement can be used as a null operation |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
150 |
statement, or it can used as a place holder when the actual code |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
151 |
implementation for a particular block of code is not known yet but has |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
152 |
to be filled up later. |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
153 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
154 |
{{{ Show summary slide }}} |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
155 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
156 |
This brings us to the end of the tutorial session on conditional |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
157 |
statements in Python. In this tutorial session we learnt |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
158 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
159 |
* What are conditional statements |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
160 |
* if/else statement |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
161 |
* if/elif/else statement |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
162 |
* Ternary conditional statement - C if X else Y |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
163 |
* and the "pass" statement |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
164 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
165 |
{{{ Show the "sponsored by FOSSEE" slide }}} |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
166 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
167 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
168 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
169 |
Hope you have enjoyed and found it useful. |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
170 |
Thankyou |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
171 |
|
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
172 |
.. Author : Madhu |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
173 |
Internal Reviewer 1 : [potential reviewer: Puneeth] |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
174 |
Internal Reviewer 2 : [potential reviewer: Anoop] |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
175 |
External Reviewer : |
6537f447efc0
Adding the script for the session on Python conditional constructs.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
diff
changeset
|
176 |