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