author | Shantanu <shantanu@fossee.in> |
Wed, 28 Apr 2010 16:33:18 +0530 | |
changeset 115 | d35eccbf206d |
parent 112 | 7de6be45182e |
permissions | -rw-r--r-- |
108
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
1 |
* Control statements |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
2 |
*** Outline |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
3 |
***** Introduction |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
4 |
******* What are we going to do? |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
5 |
******* How are we going to do? |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
6 |
******* Arsenal Required |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
7 |
********* working knowledge of arrays |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
8 |
|
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
9 |
*** Script |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
10 |
Welcome. |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
11 |
|
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
12 |
In this tutorial we shall be covering conditional statements and |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
13 |
control loops. We have used them already in some of our previous |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
14 |
sessions with brief introduction. We shall be covering 'if-else' |
112
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
15 |
statement, 'while' and 'for' statements formally. |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
16 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
17 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
18 |
For understanding of if-else statement we will write a python |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
19 |
script that takes a number as input from user and prints 0 if it is zero |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
20 |
and prints "Be positive" if it is negative, prints "Single" if the input is 1 |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
21 |
and if the number is not 0 or 1 or negative, it prints "More". |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
22 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
23 |
To write the program, open Scite text editor by going to Applications-> |
108
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
24 |
Programming->Scite: |
112
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
25 |
|
108
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
26 |
First we prompt user for entering a integer by using raw_input |
112
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
27 |
str_x = raw_input("Enter an integer: ") |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
28 |
since we know raw_input gives string, we convert the input string to an integer |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
29 |
by typing |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
30 |
x = int(str_x) |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
31 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
32 |
now we check if the number is less than zero. |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
33 |
type |
108
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
34 |
if x < 0: |
112
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
35 |
Please not |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
36 |
#if number is negative we have to print "Be positive" |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
37 |
#so we give four spaces for indentation and type |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
38 |
print 'Be positive!' |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
39 |
|
108
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
40 |
elif x == 0: |
112
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
41 |
to check if the number is equal to zero |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
42 |
#This is else-if condition |
108
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
43 |
print 'Zero' |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
44 |
elif x == 1: |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
45 |
print 'Single' |
112
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
46 |
then we type the else statement which gets executed when all the if and elif statements fail |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
47 |
so type |
108
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
48 |
else: |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
49 |
print 'More' |
112
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
50 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
51 |
Save this script by going to file menu and clicking on save. |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
52 |
save it in home folder with name 'ladder.py' |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
53 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
54 |
let us check the program on ipython interpreter for various inputs |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
55 |
open ipython terminal and type |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
56 |
%run ladder.py |
108
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
57 |
It will prompt us to enter a integer and based on our input it |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
58 |
prints appropriate message. |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
59 |
|
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
60 |
We can use binary operators like and/or/not to check for multiple |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
61 |
conditions. |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
62 |
|
112
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
63 |
Python supports only if-elif-else conditional constructs, |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
64 |
switch-case statements are not available/supported in Python. |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
65 |
|
108
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
66 |
Now lets look at loop constructs available. Python supports 'while' |
112
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
67 |
and 'for' statements. |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
68 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
69 |
To understand the while we shall write a script that prints all the fibonacci |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
70 |
numbers less than 10. In fibonacci series the sum of previous two elements |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
71 |
is equal to the next element. |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
72 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
73 |
In Scite go to file menu and click on new and it opens a new tab. |
108
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
74 |
|
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
75 |
First we initialize two variable to first and second number of |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
76 |
series |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
77 |
a, b = 0, 1 |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
78 |
while b < 10: |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
79 |
This block will be executed till this condition holds True |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
80 |
print b, |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
81 |
Note ',' here for printing values in one continues line. |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
82 |
a, b = b, a+b |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
83 |
This is one powerful feature of Python, swapping and assigning |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
84 |
new values at the same time. After this statement a will have |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
85 |
present 'b' value and b will have value of 'a+b'(phew this can be close) |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
86 |
|
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
87 |
Save this file as 'fabonacci.py' and lets run it from IPython |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
88 |
interpreter by |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
89 |
%run fabonacci.py |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
90 |
|
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
91 |
'for' in python works any kind of iterable objects. In our |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
92 |
previous sessions we used 'for' to iterate through files and lists. |
112
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
93 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
94 |
So we are going to use for loop print the squares of first five whole numbers. |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
95 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
96 |
To generate squares, we have to iterate on list of numbers and we are |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
97 |
going to use the range function to get the list of numbers. |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
98 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
99 |
let us look at the documentation of range function by typing |
108
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
100 |
range? |
112
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
101 |
we see that it takes three arguments, first is the start/initial value |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
102 |
second one is the stop/last value and third is the step size. |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
103 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
104 |
Out of these, 'start' and 'step' arguments are optional. |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
105 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
106 |
So to get list of first five natural numbers, we use |
108
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
107 |
range(5) |
112
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
108 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
109 |
Note here that last/stop value is not included in resulting list. |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
110 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
111 |
So to get square of first five number all we have to do is |
108
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
112 |
iterate over this list. |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
113 |
for i in range(5): |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
114 |
....print i, i*i |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
115 |
.... |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
116 |
.... |
112
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
117 |
|
108
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
118 |
Similarly to get square of all odd numbers from 3 to 9 we can do |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
119 |
something like |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
120 |
|
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
121 |
for i in range(3, 10, 2): |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
122 |
so the list returned from range this time will start from 3 and |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
123 |
end at 10(excluding it) with step size of 2 so we get odd numbers |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
124 |
only |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
125 |
....print i, i*i |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
126 |
.... |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
127 |
.... |
112
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
128 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
129 |
since the for statement in python works on any iterable we can also iterate through strings |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
130 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
131 |
to print each character in a string we do |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
132 |
for c in "Guido Van Rossum": |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
133 |
print c |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
134 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
135 |
we see that it prints all the characters one by one |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
136 |
|
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
137 |
That brings us to the end of this tutorial. We have learnt |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
138 |
conditional statements in Python. How to write loops |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
139 |
using 'while' statement. Range function and using for loop |
7de6be45182e
Added loops presentation and final script for loops.
Shantanu <shantanu@fossee.in>
parents:
109
diff
changeset
|
140 |
|
108
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
141 |
|
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
142 |
Thank you! |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
143 |
|
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
144 |
*** Notes |
89668e9251cf
Added control flows and conditional part.
Shantanu <shantanu@fossee.in>
parents:
diff
changeset
|
145 |