Difference between revisions of "Python/C2/Getting-started-with-ipython/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with '{| border=1 !Visual Cue !Narration |- | Show slide 1 Title Slide | Hello Friends and Welcome to the tutorial on "getting started with <tt>ipython</tt>". |- | Show slide 2 L…')
 
 
(No difference)

Latest revision as of 11:53, 2 December 2012

Visual Cue Narration
Show slide 1

Title Slide

Hello Friends and Welcome to the tutorial on "getting started with ipython".
Show slide 2

Learning objectives

At the end of this tutorial, you will be able to,
  1. invoke the ipython interpreter .
  2. quit the ipython interpreter.
  3. navigate the ipython session history.
  4. use tab-completion for writing ipython functions.
  5. look-up documentation of functions.
  6. interrupt incomplete or incorrect commands.


ipython IPython is an enhanced Python interpreter that provides features like tab-completion, easier access to help and many other functionalities which are not available in the vanilla Python interpreter.

Let us first see how to start the ipython interpreter. First open the terminal, type ipython in the terminal and hit enter.

Point to the version information with mouse Point to the In[1]: prompt with mouse After getting some information about the version of Python installed and some help commands, we get a prompt with In[1]:. But, if you get an error saying 'ipython is not installed' then refer to the tutorial on how to install the packages.
Type Ctrl-D. Now, lets see how we can quit the ipython interpreter, press Ctrl-D.
Press y and hit enter. A prompt will appear to confirm whether you really want to exit, type y to say yes and quit ipython and n to say no if you don't want to quit the ipython. Press y.
ipython Now since we have quit the interpretor, let us start it again by typing ipython
1+2

Point at the Out[1] prompt

And now let's see, how to use the interpreter.

Start with the simplest thing, addition.

type 1+2 at the prompt. IPython promptly gives back the output as 3. Notice that the output is displayed with an Out[1] indication.

5 - 3
7 - 4
6 * 5
Let us now try few more operations such as, 5 minus 3, 7 minus 4, 6 into 5.
Use the up arrow key to go back to the command 1+2 Use left arrow to navigate to start of line Type
print

Hit Enter. Point at the Out[] prompt

Now let's see how the ipython remembers the history of commands. For example print 1+2.

Instead of typing the whole thing,use the up arrow key to go back to the command 1+2 which we did before, now use the left-arrow key to navigate to the beginning of the line and type the word``print``and press space. We have changed the line to print 1+2, now press enter. The interpreter prints the result as 3. Please note that the indication Out square brackets is not shown here.

Change from print 1+2 to print 10*2 Now let us do print 10 into 2. We use the up arrow key to navigate to the previous command 1+2. Now change 1 plus 2 to 10 into 2 and press enter.
ro<tab> Till now, we saw how to invoke the ipython interpreter,quit the ipython and navigate through previous commands in ipython. Now, let's see, what is tab-completion?. let's take an example, suppose we want to use the function round. For this we just type ro at the prompt and press the tab key.
Stay on the terminal As you can see on the terminal, IPython completes the command ro into round, This feature of ipython is called the tab-completion.
r<tab> Let's see some more possibilities of tab completion just type r and then press the tab.
Stay on the terminal As you can see that IPython does not complete the command. This is because, there are many possibilities of r therefore it just lists out all the possible completions of r.
Show slide 3

Assignment 1

Now let's try out an exercise. Pause the video,solve the problem and resume the video.
  1. find out the commands starting with "ab"?
  2. list out the commands starting with "a"?


ab<tab>
a<tab>
Switch to the terminal for solution. ab tab completes to abs and a<tab> gives us a list of all the commands starting with a.
abs? Now, let's see what the functions abs is used for. We will use the help features of ipython to find out this. To see the documentation of a function, type the function name followed by a exercise mark. Ipython interpreter will show the documentation for the function. et us see the documentation of the function abs, type abs? and press enter
abs(-19)
abs(19)
As the documentation says, abs accepts a number as an input and returns it's absolute value.

lets see few examples,

Type abs(-19) and abs(19) on the interpreter.

We get 19, as expected, in both the cases.

abs(-10.5) Now lets try it for decimal numbers; lets try abs(-10.5), we got 10.5 as the result.
Show slide 4

Assignment 2


round?
Pause the video here, try out the following exercise and resume the video.

Look-up the documentation of round and see how to use it.

Switch to the terminal for solution. And you can look up the documentation of the function round by typing round exercise mark in the ipython interpreter.
Highlight the syntax with mouse hover on terminal If you notice, there are extra square brackets around ndigits. This means that ndigits is optional and 0 is the default value. Optional parameters are shown in square brackets in Python documentation.

The function round, rounds a number to a given precision.

Show slide 5

Assignment 3

Pause the video here, try out the following exercise and resume the video. et us now try few more examples with the function round.

Check the output of round(2.48) round(2.48, 1) round(2.48, 2) round(2.484) round(2.484, 1) round(2.484, 2)

Show slide 6

Solution 3

We get 2.0, 2.5 and 2.48, which are what we expect.
Switch to terminal
round(2.484

Hit Enter

Let's now see how to correct typing errors which we often make while typing at the terminal. As already shown, if we haven't hit the enter key already, we could navigate using the arrow keys and make deletions using delete or backspace key and correct the errors.

let us make a typing error deliberately, type

round(2.484

and hit enter, without closing the parenthesis.

Point at the prompt with three dots
)
We get a prompt with dots. This prompt is the continuation prompt of ipython. It appears when, the previous line is incomplete. now complete the command of the same examples with close parenthesis and press enter. We got the expected output that is 2.0
<Type any big random number>
round(35.488762320
Ctrl-C
In other instances, if we commit a typing error with a longer and more complex expression and end up with the continuation prompt, we can type Ctrl-C to interrupt the command and to get back to the ipython input prompt.
Show slide 7

Assignment 4

Pause the video here, try out the following exercise and resume the video.
  1. type round(2.484, and press enter. and then cancel the command using Ctrl-C.
  2. type the command, round(2.484, 2)


Show the solution on terminal
round(2.484
^C

round(2.484, 2)
Show slide 8

Summary slide

This brings us to the end of this tutorial. In this tutorial,we have learnt to,
  1. Invoke the ipython interpreter by typing ipython.
  2. Quit the ipython interpreter by using <ctrl>d.
  3. Navigate in the history of ipython by using the arrow keys.
  4. Use the tab-completion feature for writing python functions.
  5. See the documentation of functions using exercise mark.
  6. Interrupt using <ctrl>c when we make an error.


Show slide 9

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. ipython is a programming language similar to Python.
    True or False
  2. Which key combination quits ipython?
    • Ctrl + C
    • Ctrl + D
    • Alt + C
    • Alt + D
  1. Which character is used at the end of a command, in Ipython to display the documentation.
    • under score (_)
    • exercise mark (?)
    • exclamation mark (!)
    • ampersand (&)


Show slide 10

Solution of self assessment questions on slide

And the answers,
  1. Ipython is not a programming language, it is just an interpreter.
  2. We use Ctrl D to quit Ipython interpreter.
  3. We use ? at the end of the function name to display its documentation.


Show slide 11

Acknowledgment slide

Hope you have enjoyed this tutorial and found it useful. Thank you!

Contributors and Content Editors

Chandrika, Pravin1389