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