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