Python/C2/Additional-features-of-IPython/English
Visual Cue | Narration |
---|---|
Show Slide 1
containing title, name of the production team along with the logo of MHRD |
Hello friends and welcome to the tutorial on "Additional Features of IPython". |
Show Slide 2
Learning objectives |
At the end of this tutorial, you will be able to,
|
Show Slide 3
Pre-requisite Shift to terminal and type ipython -pylab ipython -pylab |
Before beginning this tutorial,we would suggest you to complete the tutorial on "Embellishing a plot".
Let us start ipython with pylab loaded, by typing ipython -pylab on the terminal. |
x = linspace(-2*pi, 2*pi, 100)
plot(x, xsinx(x)) |
We shall first make a plot and then view the history and save it. |
plot(x, x*sin(x))
plot(x, sin(x)) xlabel("x") ylabel("$f(x)$") title("x and xsin(x)") title("$x and xsin(x)$") |
We got an error saying "xsinx is not defined".This is because xsin(x) is actually x * sin(x) |
%hist | We now have the plot. Let us look at the commands that we have typed in. The history can be retrieved by using =%hist= command. Type %hist in your terminal. |
As you can see, it displays a list of recent commands that we typed. Every command has a number in front, to specify in which order and when it was typed.
Please note that there is a % sign before the hist command. This implies that %hist is a command that is specific to IPython only and not to any other version of python. These type of commands are called as magic commands. Also note that, the =%hist= itself is a command and is displayed as the most ecent command. We should note that anything we type in is stored as history, irrespective of whether it is command or an error or IPython magic command. | |
%hist 5 | If we want only the recent 5 commands to be displayed, we pass the number as an argument to =%hist= command. Hence %hist 5 displays the recent 5 commands, inclusive of the =%hist= command. The default number is 40. |
Show Slide 4
Assignment 1 |
Pause the video here, try out the following exercise and resume the video.
Read through the documentation of %hist and find out how to list all the commands between 5 and 10. |
Continue from paused state Switch to the terminal
%hist 5 10 |
As we can see from =%hist= documentation,%hist 5 10 displays the commands from 5 to 10
Now that we have the history, we would like to save the required line of code from history. This is possible by using the =%save= command. |
%hist | Before we do that, let us first look at history and identify what lines of code we require. Type %hist again. |
Point to the lines | The first command is linspace. But second command is a command that gave us an error. Hence we do not need second command. The commands from third to sixth are required. The seventh command although is correct, we do not need it since we are setting the title correctly in the eighth command. |
Type as you say
%save /home/fossee/plot_script.py 1 3-6 8 |
So we need first third to sixth and the eighth command for our program. Hence the syntax of =%save= will be |
Point to the output of the command | The command saves the first line of code and then third to sixth followed by the eighth lines of code into the specified file.
The first argument to %save is the path of file to save the commands and the arguments there after are the commands to be saved in the given order. |
Goto the file and open it and show it | |
Show Slide 5
Assignment 2 |
Pause the video here, try out the following exercise and resume the video.
Change the label on y-axis to "y" and save the lines of code accordingly. |
Continue from paused state
ylabel("y") |
we use the command =ylabel= on interpreter |
Type as you say
%save /home/fossee/example_plot.py 1 3-6 10 |
and then do |
%run -i /home/fossee/plot_script.py | Now that we have the required lines of code in a file, let us learn how to run the file as a python script.
We use the IPython magic command =%run= to do this. Type |
show() | The script runs but we do not see the plot. This happens because when we are running a script, we are not in the interactive mode anymore.
Hence to view the plot type show() on your terminal |
Show Slide 6
Assignment 3 %hist 20 |
Pause the video here, try out the following exercise and resume the video.
Use %hist and %save and create a script that has the function show() in it. Run the script to produce the plot and display the same. <Pause> We first look at the history using |
Say as you type
%save /home/fossee/show_included.py 1 3-6 8 14 17 %run -i /home/fossee/show_included.py |
Then save the script using |
We get the desired plot.
The reason for including a -i after run is to tell the interpreter that if any name is not found in script, search for it in the interpreter. Hence all these sin, plot, pi and show which are not available in script, are taken from the interpreter and used to run the script. | |
Show Slide 7
Assignment 4 |
Pause the video here, try out the following exercise and resume the video.
Run the script without using the -i option. Do you find any difference? <Pause> |
%run /home/fossee/show_included.py | We see that it raises NameError saying that the name linspace is not found. |
Show Slide 8
Summary slide |
This brings us to the end of this tutorial. In this tutorial,we have learnt to,
|
Show Slide 9
Self assessment questions slide |
Here are some self assessment questions for you to solve
|
Show Slide 10
Solution of self assessment questions on slide |
And the answers,
|
Show Slide 11
Acknowledgment slide |
Hope you have enjoyed and found it useful. Thank You! |