1 .. Objectives |
|
2 .. ---------- |
|
3 |
|
4 .. At the end of this tutorial, you will be able to |
|
5 |
|
6 .. 1. invoke the ``ipython`` interpreter. |
|
7 .. #. quit the ``ipython`` interpreter. |
|
8 .. #. navigate in the history of ``ipython``. |
|
9 .. #. use tab-completion. |
|
10 .. #. look-up documentation of functions. |
|
11 .. #. interrupt incomplete or incorrect commands. |
|
12 |
|
13 .. Prerequisites |
|
14 .. ------------- |
|
15 |
|
16 .. should have ``ipython`` and ``pylab`` installed. |
|
17 |
|
18 .. Author : Puneeth |
|
19 Internal Reviewer : Anoop Jacob Thomas<anoop@fossee.in> |
|
20 Language Review : Bhanukiran |
|
21 External Reviewer : |
|
22 Checklist OK? : <put date stamp here, if OK> [2010-10-05] |
|
23 |
|
24 |
|
25 Script |
|
26 ------ |
|
27 |
|
28 {{{ Show the slide containing title }}} |
|
29 |
|
30 Hello Friends and Welcome to the tutorial on getting started with |
|
31 ``ipython``. |
|
32 |
|
33 {{{ Show slide with outline }}} |
|
34 |
|
35 This tutorial will cover the basic usage of the ``ipython`` |
|
36 interpreter. The following topics would be covered. |
|
37 |
|
38 IPython is an enhanced Python interpreter that provides features like |
|
39 tabcompletion, easier access to help and lot of other functionality |
|
40 which are not available in the vanilla Python interpreter. |
|
41 |
|
42 First let us see how to invoke the ``ipython`` interpreter. |
|
43 |
|
44 We type |
|
45 :: |
|
46 |
|
47 ipython |
|
48 |
|
49 at the terminal prompt to invoke the ipython interpreter. |
|
50 |
|
51 We get a prompt with ``In [1]:`` after getting some information about |
|
52 the version of Python installed and some help commands. |
|
53 |
|
54 If you get an error saying something like ``ipython is not |
|
55 installed``, refer to the tutorial on how to install the packages |
|
56 required for this course. |
|
57 |
|
58 Now, to quit the ipython interpreter, type Ctrl-D. You are prompted |
|
59 asking if you really want to exit, type y to say yes and quit ipython. |
|
60 |
|
61 Start ipython again, as you did before. |
|
62 |
|
63 The prompt that you have says ``In [1]``. ``In`` stands for input and the |
|
64 ipython interpreter is ready to accept input from you. |
|
65 |
|
66 Now let us see, how we can type some commands into the interpreter. |
|
67 |
|
68 Start with the simplest thing, addition. |
|
69 |
|
70 Let's type |
|
71 :: |
|
72 1+2 |
|
73 |
|
74 at the prompt. IPython promptly gives back the output as 3. Notice |
|
75 that the output is displayed with an ``Out[1]`` indication. |
|
76 |
|
77 .. #[[Anoop: I think we can illustrate In [] and Out[] in slides]] |
|
78 .. #[[Puneeth: I think we can do that on the terminal?]] |
|
79 |
|
80 Let's try out few other mathematical operations. |
|
81 :: |
|
82 |
|
83 5 - 3 |
|
84 7 - 4 |
|
85 6 * 5 |
|
86 |
|
87 Now let's ``print 1+2``. Instead of typing the whole thing, we make |
|
88 use of the fact that IPython remembers the history of the commands |
|
89 that you have already used. We use the up arrow key to go back the |
|
90 command ``1+2``. We then use the left-arrow key to navigate to the |
|
91 beginning of the line and add the word ``print`` and a space. Then hit |
|
92 enter and observe that the interpreter prints out the value as 3, |
|
93 without the Out[] indication. |
|
94 |
|
95 Now, let's change the previous command ``print 1+2`` to ``print |
|
96 10*2``. We use the up arrow again to navigate to the previous command |
|
97 and use the left arrow key to move the cursor on to the + symbol and |
|
98 then use the delete key to remove it and type 0 and * to change the |
|
99 expression as required. We hit enter to see the output of |
|
100 ``print``. |
|
101 |
|
102 Now, let's say we want to use the function ``round``. We type ``ro`` |
|
103 at the prompt and hit the tab key. As you can see, IPython |
|
104 completes the command. This feature is called the tab-completion. |
|
105 |
|
106 Now, we remove all the characters and just type ``r`` and then hit |
|
107 tab. IPython does not complete the command since there are many |
|
108 possibilities. It just lists out all the possible completions. |
|
109 |
|
110 Following is an exercise that you must do. |
|
111 |
|
112 %%1%% Type ``ab`` and hit tab to see what happens. Next, just type |
|
113 ``a`` and hit tab to see what happens. |
|
114 |
|
115 Please, pause the video here. Do the exercise and then continue. |
|
116 |
|
117 ``ab`` tab completes to ``abs`` and ``a<tab>`` gives us a list of all |
|
118 the commands starting with a. |
|
119 |
|
120 Now, let's see what these functions are used for. We will use the |
|
121 help features of ipython to find this out. |
|
122 |
|
123 .. #[[Anoop: Another slide which says about ? mark and round? etc, as |
|
124 few people cannot just follow by listening (like me) :)]] |
|
125 |
|
126 .. #[Punch: These things are shown on the terminal. I feel we don't |
|
127 .. need slide, here I guess.] |
|
128 |
|
129 To get the help of any function, we first type the function, ``abs`` |
|
130 in our case and then add a ? at the end and hit enter. |
|
131 |
|
132 As the documentation says, ``abs`` accepts a number as an input and |
|
133 returns it's absolute value. |
|
134 |
|
135 We say, |
|
136 :: |
|
137 |
|
138 abs(-19) |
|
139 |
|
140 abs(19) |
|
141 |
|
142 We get 19, as expected, in both the cases. |
|
143 |
|
144 Does it work for decimals (or floats)? Let's try typing abs(-10.5) |
|
145 and we do get back 10.5. |
|
146 |
|
147 Following is an exercise that you must do. |
|
148 |
|
149 %%2%% Look-up the documentation of ``round`` and see how to use it. |
|
150 |
|
151 Please, pause the video here. Do the exercise and then continue. |
|
152 |
|
153 :: |
|
154 |
|
155 round? |
|
156 |
|
157 If you notice, there are extra square brackets around the ``ndigits``. |
|
158 This means that ``ndigits`` is optional and 0 is the default value. |
|
159 Optional parameters are shown in square brackets anywhere in Python |
|
160 documentation. |
|
161 |
|
162 The function ``round``, rounds a number to a given precision. |
|
163 |
|
164 Following are exercises that you must do. |
|
165 |
|
166 %%3%% Check the output of:: |
|
167 |
|
168 round(2.48) |
|
169 round(2.48, 1) |
|
170 round(2.48, 2) |
|
171 |
|
172 round(2.484) |
|
173 round(2.484, 1) |
|
174 round(2.484, 2) |
|
175 |
|
176 Please, pause the video here. Do the exercises and then continue. |
|
177 |
|
178 We get 2.0, 2.5 and 2.48, which are what we expect. |
|
179 |
|
180 Let's now see how to correct typing errors that we make while typing at |
|
181 the terminal. As already shown, if we haven't hit the enter key |
|
182 already, we could navigate using the arrow keys and make deletions |
|
183 using delete or backspace key and correct the errors. |
|
184 |
|
185 Let's now type round(2.484 and hit enter, without closing the |
|
186 parenthesis. We get a prompt with dots. This prompt is the |
|
187 continuation prompt of ``ipython``. It appears, the previous line is |
|
188 incomplete in some way. We now complete the command by typing, the |
|
189 closing parenthesis and hitting enter. We get the expected output of |
|
190 2.5. |
|
191 |
|
192 In other instances, if we commit a typing error with a longer and more |
|
193 complex expression and end up with the continuation prompt, we can |
|
194 type Ctrl-C to interrupt the command and get back the ``ipython`` input |
|
195 prompt. |
|
196 |
|
197 Following is an exercise that you must do. |
|
198 |
|
199 %%4%% Try typing round(2.484, and hit enter. and then cancel the |
|
200 command using Ctrl-C. Then, type the command, round(2.484, 2) and |
|
201 resume the video. |
|
202 |
|
203 Please, pause the video here. Do the exercises and then continue. |
|
204 |
|
205 :: |
|
206 |
|
207 round(2.484 |
|
208 ^C |
|
209 |
|
210 round(2.484, 2) |
|
211 |
|
212 This brings us to the end of the tutorial on getting started with |
|
213 ``ipython``. |
|
214 |
|
215 .. #[[Anoop: add slides for interrupts, navigating history, I feel |
|
216 even a single point will also do]] |
|
217 |
|
218 .. #[Puneeth: I don't feel these things cannot be shown on a slide.] |
|
219 |
|
220 In this tutorial we have learnt, how to |
|
221 {{{ show the outline/summary slide. }}} |
|
222 |
|
223 1. invoke the ``ipython`` interpreter. |
|
224 #. quit the ``ipython`` interpreter. |
|
225 #. navigate in the history of ``ipython``. |
|
226 #. use tab-completion. |
|
227 #. look-up documentation of functions. |
|
228 #. interrupt incomplete or incorrect commands. |
|
229 |
|
230 {{{ Show the "sponsored by FOSSEE" slide }}} |
|
231 |
|
232 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
233 |
|
234 Hope you have enjoyed and found it useful. |
|
235 Thank you! |
|