Python-3.4.3/C2/Additional-features-of-IPython/English-timed
| |
|
| 00:01 | Hello friends and welcome to the Spoken Tutorial on Additional Features of IPython. |
| 00:07 | At the end of this tutorial, you will be able to: retrieve your IPython history, |
| 00:14 | view a part of the history, |
| 00:17 | save a part of a history to a file, |
| 00:21 | run a script from within IPython. |
| 00:25 | To record this tutorial, I am using:
Ubuntu Linux 14.04 operating system, |
| 00:32 | Python 3.4.3,
IPython 5.1.0 |
| 00:38 | To practice this tutorial, you should know how to
use plots interactively and embellish a plot. |
| 00:48 | If not, see the pre-requisite Python tutorials on this website. |
| 00:54 | Let us now open the Terminal by pressing Ctrl+Alt+T keys simultaneously. |
| 01:01 | Now, type ipython3 and press Enter. |
| 01:07 | Let us initialise the pylab package. Type percentage pylab and press Enter. |
| 01:14 | To start plotting, type: x is equal to linspace inside the brackets minus 2pi comma 2pi comma 100 and press Enter. |
| 01:31 | Next, type: plot inside the brackets x comma xsin(x) and press Enter. |
| 01:42 | We got an error saying "xsin is not defined".
This is because xsin(x) is actually x multiplied by sin(x). |
| 01:54 | Here, the multiplication sign is missing. So, let us now type:
plot inside the brackets x comma x multiplied by sin(x) and press Enter. |
| 02:13 | Next, let us add title and the labels for both x and y axes. |
| 02:19 | Type: xlabel inside the brackets inside inverted commas inside dollar sign x and press Enter. |
| 02:31 | ylabel inside the brackets inside inverted commas inside dollar sign f(x) . |
| 02:43 | title inside the brackets inside inverted commas inside dollar sign x and xsin(x). |
| 02:57 | We can now see the labelled plot. |
| 03:01 | The history of typed commands can be retrieved by the percentage history command. |
| 03:07 | Type: percentage history and press Enter. |
| 03:13 | percentage history itself is a command and is displayed as the most recent command. |
| 03:20 | Whatever we executed in the terminal, is stored as history. |
| 03:25 | If we want to see what was the fifth command, pass 5 as an argument to percentage history command. |
| 03:33 | Type: percentage history space 5 and press Enter.
This displays the fifth command which we typed. |
| 03:43 | Now, pause the video here and try out the following exercise and resume the video. |
| 03:49 | Find out how to list the recent commands between 5 and 10. |
| 03:55 | Switch back to the terminal. |
| 03:58 | Let us look at the solution. |
| 04:00 | Type clf() and press Enter.
Type: percentage history question mark |
| 04:08 | Read through the information of percentage history command. |
| 04:13 | We can see, percentage history hyphen n 4 hyphen 6 displays the commands from 4 to 6. |
| 04:24 | Here, hyphen n is an optional argument which prints the line numbers.
Type q to quit the documentation. |
| 04:37 | Now, type: percentage history space 5 hyphen 10 and press Enter. |
| 04:46 | To save the history, we use the percentage save command. |
| 04:51 | Before we do that, let us first look at the history and identify which lines of code we require. |
| 04:58 | Type percentage history and press Enter. |
| 05:03 | The second command is linspace.
But the third command is a command that gave us an error. |
| 05:10 | Hence we do not need it. |
| 05:13 | The commands from fourth to seventh are required. |
| 05:16 | So, we need the second command and then the fourth to seventh commands for our program. |
| 05:22 | Let us save it in the present working directory. Hence the syntax will be-
percentage save space plot underscore script.py space 2 space 4 hyphen 7 and press Enter. |
| 05:47 | The first argument in percentage save command is the name of the file in which the commands are saved. |
| 05:56 | The second argument gives the numbers of the commands that are being saved separated by spaces. |
| 06:04 | Let us now open the file plot underscore script.py and see the contents. |
| 06:13 | Let us learn how to run the file as a python script.
We use the command percentage run to do this. |
| 06:22 | Type: percentage run space hyphen i space plot underscore script.py and press Enter. |
| 06:38 | Here, hyphen i parameter runs the code written in a text editor.
The code is run within the current ipython session. |
| 06:50 | It uses the variables defined interactively in the ipython session. |
| 06:56 | The script runs but we do not see the plot. |
| 07:01 | This is because when we are running a script, we are not in the interactive mode. |
| 07:07 | To view the plot, type show() on your terminal and press Enter. |
| 07:15 | Pause the video here and try out the following exercise and resume. |
| 07:21 | Use percentage history and percentage save to create a script that has the function show() in it. |
| 07:30 | Run the script to produce the plot and display the same. |
| 07:35 | Let us look at the solution.
We first look at the history using the percentage history hyphen n command. |
| 07:44 | Type: percentage history space hyphen n and press Enter. |
| 07:54 | Let us clear the plot window.
Type clf() and press Enter. |
| 08:01 | Now, let us save the script using the command percentage save. |
| 08:07 | We need the lines 2, then 4 to 7 and 16. |
| 08:20 | Type: percentage save space show underscore included.py space 2 space 4 hyphen 7 space 16 and press Enter. |
| 08:41 | To run the script, type:
percentage run space hyphen i space show underscore included.py and press Enter. |
| 08:57 | We get the desired plot. |
| 09:01 | Go to the previous command.
Modify it by removing ‘hyphen i’ in the command to percentage run space show included.py and Press Enter. |
| 09:16 | We see that it raises a NameError saying that the name 'linspace' is not defined.
This happens because we did not run the script interactively. |
| 09:30 | This brings us to the end of this tutorial. In this tutorial, we have learnt to:
retrieve the history using percentage history command, |
| 09:41 | view only a part of history by passing argument to percentage history command, |
| 09:48 | save the required lines of code, in required order using percentage save command. |
| 09:55 | Use 'percentage run space hyphen i' command to run a saved script. |
| 10:04 | Here are some self assessment questions. |
| 10:08 | How do you save the command lines 2 3 4 5 7 9 10 and 11? |
| 10:17 | percentage save filename 2-5 7 9 hyphen 11 |
| 10:25 | percentage save filename 2 hyphen 11 |
| 10:30 | percentage save filename
percentage save 2 hyphen 5 7 9 10 and 11 |
| 10:40 | Which is the command to run the script?
Percentage execute the script name |
| 10:46 | percentage run hyphen i script name
percentage run script name |
| 10:53 | percentage execute hyphen i script name |
| 10:58 | And the solutions are-
To save the commands 2 3 4 5 7 9 10 and 11, we issue the command percentage save filename 2 hyphen 5 space 7 space 9 hyphen 11. |
| 11:18 | To run the script, we use percentage run space hyphen i space scriptname. |
| 11:27 | Please post your timed queries in this forum. |
| 11:32 | Please post your general queries on Python in this forum. |
| 11:37 | The FOSSEE team coordinates the TBC project. |
| 11:41 | Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
| 11:52 | This is Usha from IIT Bombay (or FOSSEE, if you wish), signing off. |