Python-3.4.3/C2/Getting-started-with-IPython/English

From Script | Spoken-Tutorial
Revision as of 17:56, 8 February 2017 by Vineeta (Talk | contribs)

Jump to: navigation, search

Python/C2/Getting-started-with-IPython/English

Title of script: Getting started with IPython

Author: Puneeth, Jovina, Thirumalesh H S

Keywords: Python, IPython


Visual Cue
Narration
Show Slide

[Slide with MHRD logo]

Hello Friends. Welcome to the spoken tutorial on "getting started with IPython".
Show Slide

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 within IPython.
  5. look-up documentation in IPython.
  6. interrupt incomplete or incorrect commands.


Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 14.04 operating system
  • Python 3.5.2
  • IPython 5.1.0


Show Slide:

What is IPython

What is IPython?
  • IPython is an enhanced interactive Python interpreter.
  • It provides features like tab-completion, and easier access to help.


[Terminal]

Type ipython >> press Enter

Let us first see how to start the IPython interpreter.


First open the terminal by pressing Ctrl+Alt+T keys simultaneously on the keyboard.


Type ipython3 at the prompt and press Enter.

If IPython is not installed, please refer to the instructions sheet.

[IPython Terminal]

Point to the version information with mouse.

If IPython is installed, the IPython interpreter is loaded by running the ipython command in the terminal


The versions of Python and IPython that are installed, are shown on the terminal.

[IPython Terminal]

Point to In[1]: prompt with mouse

Some additional helpful information is printed by IPython.


After this, we get a prompt with In[1]:



[IPython Terminal]

Type Ctrl+D

Now, Let us see how we can quit the IPython interpreter.


Press Ctrl+D keys.

]IPython Terminal]


A prompt will appear to confirm if we really want to exit.


Type y for yes and quit IPython.


Note that y is given in square brackets so it is default.

We could also press Enter and it will exit.


Else type n for no if you don't want to quit IPython.

[IPython Terminal]


Type y >> press Enter.


Let us type y.


We have quit IPython interpreter and are back at the terminal prompt.

[Terminal]

Type ipython >> press Enter

Let us start it again.


Type ipython3 in the terminal and press Enter.

[IPython Terminal] Now, Let us learn how to use the interpreter.


Let us start with the simplest operation - addition.

[IPython Terminal]

1+2

Point at the Out[1] prompt


Type 1 plus 2 at the IPython prompt and press Enter.


We press Enter to execute the python command. Please do so after typing every command.


IPython promptly displays the output as 3.


Notice that the output is shown with an Out square brackets 1 indication.

[IPython Terminal]

5 – 3

7 * 4

Let us now try a few more operations such as,

5 minus 3,

7 multiplied by 4,


Each time we press Enter and see the output on the IPython console window.

IPython Terminal Let us see how we can navigate to previous commands in IPython
[IPython Terminal] For example, say, we want to execute print open parenthesis 1 plus 2 close parenthesis.


Instead of typing the whole command, we can recall the command 1 plus 2 we typed earlier.

[IPython Terminal]

Use the up arrow key to go back to the command 1+2.

Use the up arrow key to go back to the command 1 plus 2.
[IPython Terminal]

Use left arrow to navigate to start of line

Now use the left-arrow key to navigate to the beginning of the line.



[IPython Terminal]

Type print (>> press space) and press Enter.


Type the word print open parenthesis and press space key close parenthesis on the keyboard.


We have changed the command to print (1 plus 2). Now press Enter.



[IPython Terminal]

Highlight the result line.


Point at the Output

The interpreter prints the result as 3.


Notice that this time, the indication Out square brackets is not displayed.

[IPython Terminal]

Press up-arrow key

Now let us execute print 10 multiplied by 2.

We use the up arrow key to navigate to the previous command print (1 plus 2).

[IPython Terminal]

Change print (1+2) to print (10*2)

Now replace 1 plus 2 with 10 multiplied by 2 and press Enter.


As with any programming language, asterix is used for multiplication operator.

[IPython Terminal]

Highlight output

Observe the output on the console.

<<PAUSE>>

[IPython Terminal] Now, Let us see, what is tab-completion.


Let us consider an example.

[IPython Terminal]

pri<tab>

Suppose we want to use the function print.


For this we just type pri at the prompt and press the tab key.

[IPython Terminal] As you can see on the console that IPython has autocompleted the command pri to print.


This feature of IPython is called the tab-completion.

[IPython Terminal]

p<tab>

Let us see some more possibilities of tab completion.


Just type p and then press the tab.

[IPython Terminal] In this case, we see that IPython does not complete the command.


This is because, there are more than one command beginning with p.


Therefore, it just lists out all the possible tab-completion of p.

Show Slide:

Exercise 1

Now Let us 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".


[IPython Terminal]


ab<tab>

a<tab>

ab tab autocompletes to abs.


a<tab> displays a list of all the commands starting with a.

[IPython Terminal] Now, Let us see what the function abs is used for.


We will use the help feature of IPython to find out this.

[IPython Terminal] To see the documentation of a function, type the function name followed by a question mark.


IPython interpreter will show the documentation for the function.


Let us see the documentation of the function abs.

[IPython Terminal]

Type abs? >> press Enter


Type abs? and press Enter. From the displayed information, it says abs accepts a number as input and returns it's absolute value.
[Ipython Terminal]

abs(-19)

abs(19)

Let us see few examples,


On the console, type abs(-19) and then abs(19).


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

[Ipython Terminal]

abs(-10.5)

Now Let us try it for decimal numbers.


Let us try abs(-10.5).


We get 10.5 as the result.

Show Slide

Exercise 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.

[IPython Terminal]

Type round?


Highlight the definition of round

Switch to the console for the solution.


You can look up the documentation of the function round by typing round question mark.


It says here that the function round, rounds a number to a given precision.

[IPython Terminal]

Highlight the syntax of ndigits

ndigits is the precision value for round function.


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.

Show Slide

Exercise 3

Pause the video here.


Try out the following exercise and resume the video.

Check the output of

round(2.48) round(2.48, 1)round(2.484) round(2.484, 2)

Show Slide

Solution 3

We get


round(2.48) = 2.0round(2.48, 1)= 2.5 round(2.484) = 2.0 round(2.484, 2) = 2.48


which are what we expect.

[IPython Terminal]


round(2.484

Press Enter

Let us now see how to correct typing errors, which we could make while typing on the console.


Let us make a typing error deliberately.


Type round open parenthesis 2.484 and press Enter, without closing the parenthesis.



[IPython Terminal]

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.

Type ) >> press Enter Now complete the command with close parenthesis and press Enter.


We get the expected output, that is 2.0

[IPython Terminal]


round(1a >> press Enter


press Ctrl+C


What if we type an incorrect command and end up with the continuation prompt?


In such case, we can press Ctrl+C keys, to interrupt the command and get back to the IPython prompt.


E.g, round takes only numbers as input. Type round(1a


Do not close the paranthesis and press Enter.


We gave alpha-numeric value '1a' as input.


Press Ctrl+C to iterrupt the execution.

Show Slide

Exercise 4

Pause the video here.


Try out the following exercise and resume the video.

  1. Type round(2.484, and press Enter.
  2. Then cancel the command using Ctrl+C.
  3. Type the command, round(2.484, 2)


[IPython Terminal]

round(2.484

Ctrl+C

round(2.484, 2)

The output on your console should look like this.
Show Slide


Summary


This brings us to the end of this tutorial.


In this tutorial,we have learnt to,

  1. invoke the IPython interpreter by typing ipython in the terminal.
  2. quit the IPython interpreter by using Ctrl+D.
  3. navigate IPython session history by using the arrow keys.
  4. use the tab-completion to work faster.
  5. see the documentation of functions using question mark.
  6. interrupt commands using Ctrl+C when we make an error.


Show Slide

Assignment


Here are some self assessment questions for you to solve


  1. IPython is a programming language similar to Python.True or False
  1. Which key combination quits IPython?
    • Ctrl + C
    • Ctrl + D
    • Alt + C
    • Alt + D


3. Which character is used at the end of a command, in IPython to display the documentation?

    • under score (_)
    • question mark (?)
    • exclamation mark (!)
    • ampersand (&)



Show Slide

Solutions


And the answers are-


  1. False. IPython is not a new programming language. It is just an enhanced interactive Python interpreter.
  2. We use Ctrl + D to quit IPython interpreter.
  3. We use ? at the end of the command to display its documentation.


Show Slide

About the Spoken Tutorial Project


This video summarises the Spoken Tutorial project.


If you do not have good bandwidth, you may download and watch it.

Show Slide

Spoken tutorial workshops

We conduct workshops using Spoken Tutorials.

Give Certificates.

Please contact us.

Show Slide

Forum to answer questions

Do you have questions in THIS Spoken Tutorial?

Choose the minute and second where you have the question.

Explain your question briefly.

Someone from the FOSSEE team will answer them.

Please visit this site.

Show Slide

Forum to answer questions

Do you have any general / technical questions on Python?

Please visit the FOSSEE forum and post your question.

Show Slide

Textbook Companion

The FOSSEE team coordinates coding of solved examples of popular books.

We give honorarium and certificates for those who do this.

For more details, please visit this site.

Show Slide

Acknowledgement

The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India
Show Slide

Thank You

This is __________ from IIT Bombay signing off. Thanks for watching

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, PoojaMoolya, Vineeta