1 00:00:00 --> 00:00:05 Hello friends and welcome to the tutorial on "Additional Features of IPython". 2 00:00:06 --> 00:00:18 At the end of this tutorial, you will be able to, Retrieve your ipython history. View a part of the history. Save a part of your history to a file. Run a script from within ipython. 3 00:00:19 --> 00:00:23 Before beginning this tutorial,we would suggest you to complete the tutorial on "Embellishing a plot". 4 00:00:24 --> 00:00:32 Let us start ipython with pylab loaded, by typing ipython space hyphen pylab on the terminal. 5 00:00:33 --> 00:00:59 We shall first make a plot and then view the history and save it, for that we can type x = linspace within brackets minus 2 into pi comma 2 into pi comma 100 plot within bracket x comma xsinx cosx and hit enter 6 00:01:00 --> 00:01:03 We got an error saying "xsinx is not defined". 7 00:01:04 --> 00:02:14 This is because xsin(x) is actually x star sin(x), for that we have to type on the terminal plot within bracket x comma x star sin(x) plot x, sin(x) xlabel within bracket in double quotes x then ylabel within bracket in double quotes dollar sign f(x) again a dollar sign then title within bracket in double quotes x and xsin(x) then again title within bracket in double quotes dollar sign x and xsin(x) and again a dollar sign 8 00:02:15 --> 00:02:17 We now have the plot. 9 00:02:18 --> 00:02:20 Let us look at the commands that we have typed in. 10 00:02:21 --> 00:02:27 The history can be retrieved by using modulo hist command. 11 00:02:28 --> 00:02:36 Type modulo that is percentage sign then hist and hit enter 12 00:02:37 --> 00:02:40 As you can see, it displays a list of recent commands that we typed. 13 00:02:41 --> 00:02:47 Every command has a number in front, to specify in which order and when it was typed. 14 00:02:48 --> 00:02:51 So, Please note that there is a percentage sign before the hist command. 15 00:02:52 --> 00:02:59 This implies that percentage hist is a command that is specific to IPython only and not to any other version of python. 16 00:03:00 --> 00:03:03 These other type of commands are called as magic commands. 17 00:03:04 --> 00:03:14 Also note that, the %hist itself is a command and is displayed as the most recent command. 18 00:03:15 --> 00:03:26 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. 19 00:03:27 --> 00:03:34 If we want only the recent 5 commands to be displayed, we pass the number as an argument to percentage hist command. 20 00:03:35 --> 00:03:45 Hence percentage hist 5 displays the recent 5 commands, inclusive of the percentage hist command that we gave. 21 00:03:46 --> 00:03:47 The default number is 40. 22 00:03:48 --> 00:03:57 So to try this we can type in the terminal percentage hist space 5 and hit enter. 23 00:03:58 --> 00:04:01 Pause the video here, try out the following exercise and resume the video. 24 00:04:02 --> 00:04:10 Read through the documentation of %hist and find out how to list all the commands between 5 and 10. 25 00:04:11 --> 00:04:21 As we can see from percentage hist documentation,percentage hist 5 space 10 displays the commands from 5 to 10 26 00:04:22 --> 00:04:34 Now that we have the history, we will try it on the command prompt, so you can type percentage hist 5 space 10 and hit enter. 27 00:04:35 --> 00:04:38 we would like to save the required line of code from history. 28 00:04:39 --> 00:04:46 So, This is possible by using the percentage save command. 29 00:04:47 --> 00:04:57 So, Before we do that, let us first look at history and identify what lines of code we require. So type percentage hist on the terminal and hit enter. 30 00:04:58 --> 00:04:59 The first command is linspace. 31 00:05:00 --> 00:05:05 But second command is a command that gave us an error. 32 00:05:06 --> 00:05:08 Hence we do not need second command. 33 00:05:09 --> 00:05:11 The commands from third to sixth are required. 34 00:05:12 --> 00:05:19 The seventh command although is correct, we do not need it since we are setting the title correctly in the eighth command. 35 00:05:20 --> 00:05:25 So we need first third to sixth and the eighth command for our program. 36 00:05:26 --> 00:05:45 Hence you can type in the syntax of percentage save slash home slash fossee slash plot underscore script dot py space 1 space 3 hyphen 6 space 8 37 00:05:46 --> 00:05:54 This command saves the first line of code and then third to sixth followed by the eighth lines of code into the specified file. 38 00:05:55 --> 00:06:07 The first argument to percentage save is the path of file and save the commands and the arguments there after are the commands to be saved in the given order. 39 00:06:08 --> 00:06:10 Pause the video here, try out the following exercise and resume the video. 40 00:06:11 --> 00:06:16 Change the label on y-axis to "y" and save the lines of code accordingly. 41 00:06:17 --> 00:06:20 we use the command ylabel on interpreter 42 00:06:21 --> 00:06:28 So you can type, ylabel within brackets in double quotes y and hit enter 43 00:06:29 --> 00:06:35 and then you can do percentage save command 44 00:06:36 --> 00:06:50 That is you can type percentage save then slash home slash fossee slash example underscore plot dot py then 1 space 3 hyphen 6 space 10 45 00:06:51 --> 00:06:57 Now that we have the required lines of code in a file, let us learn how to run the file as a python script. 46 00:06:58 --> 00:07:02 We use the IPython magic command percentage run to do this. 47 00:07:03 --> 00:07:17 So Type percentage run space hyphen i space slash home slash fossee slash plot underscore script dot py 48 00:07:18 --> 00:07:26 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. 49 00:07:27 --> 00:07:37 Hence to view the plot, you have to type, show and then brackets on your terminal 50 00:07:38 --> 00:07:40 So now Pause the video here, try out the following exercise 51 00:07:41 --> 00:07:48 Use percentage hist and percentage save and create a script that has the function show() in it. 52 00:07:49 --> 00:07:55 Run the script to produce the plot and display the name. 53 00:07:56 --> 00:08:05 So you can type percentage hist space 20 on the terminal 54 00:08:06 --> 00:08:08 We first look at the history using this command only, 55 00:08:09 --> 00:08:36 Then save the script using the command percentage save slash home slash fossee slash show underscore included dot py space 1 space 3 hyphen 6 then space 8 then 14 and 17 56 00:08:37 --> 00:09:02 then on the next line you can type percentage run hyphen i slash home slash fossee slash show underscore included dot py, then type show, show function to get the plot. 57 00:09:03 --> 00:09:06 We get the desired plot now. 58 00:09:07 --> 00:09:16 The reason for including a hyphen i after run is to tell the interpreter that if any name is not found in script, search for it in the interpreter. 59 00:09:17 --> 00:09:31 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. 60 00:09:32 --> 00:09:35 So now, Pause the video here, try out the following exercise and resume the video. 61 00:09:36 --> 00:09:41 Run the script without using the hyphen i option. 62 00:09:42 --> 00:09:46 So Do you find any difference? 63 00:09:47 --> 00:10:03 So you can type percentage run slash home slash fossee slash included hypen included dot py 64 00:10:04 --> 00:10:11 We see that it raises Name Error saying that the name linspace is not found. 65 00:10:12 --> 00:10:13 This brings us to the end of this tutorial. 66 00:10:14 --> 00:10:20 In this tutorial,we have learnt to, Retrieve the history using percentage hist command. 67 00:10:21 --> 00:10:24 then View only a part of history by passing an argument to 'percentage hist' command. 68 00:10:25 --> 00:10:29 then Save the required lines of code in required order using 'percentage save' command. 69 00:10:30 --> 00:10:38 then Use 'percentage run hyphen i' command to run the saved script. 70 00:10:39 --> 00:10:42 Here are some self assessment questions for you to solve 71 00:10:43 --> 00:10:45 5. How do you retrieve the recent 5 commands 72 00:10:46 --> 00:10:56 And there are four options percentage hist,percentage hist minus 5,percentage hist 5,percentage hist 5 to 10 73 00:10:57 --> 00:11:23 And in second, How do you save the lines 2 3 4 5 7 9 10 11 and the options are percentage save filepath 2 hyphen 5 7 9 hyphen 11,then percentage save filepath 2 hyphen 11,then percentage save filepath then after that percentage save 2 hyphen 5 7 9 10 11 74 00:11:24 --> 00:11:29 And the final question is What will the command percentage hist 5 space 10 display. 75 00:11:30 --> 00:11:33 The recently typed commands from 5 to 10 inclusive of the history command 76 00:11:34 --> 00:11:37 The recently typed commands from 5 to 10 excluding the history command 77 00:11:38 --> 00:11:40 So now the answers, 78 00:11:41 --> 00:11:48 In order to retrieve the recently typed 5 commands,we have to say percentage hist 5 for the first question. 79 00:11:49 --> 00:12:01 So second question answer is percentage save file path 2 hyphen 5 space 7 space 9 hyphen 11 is the correct option to the specified lines of codes. 80 00:12:02 --> 00:12:13 And Then third answer is percentage hist 5 space 10 will display the recently typed commands from 5 to 10 inclusive of the history command. 81 00:12:14 --> 00:12:16 This brings us to the end of this tutorial 82 00:12:17 --> 00:12:22 So we Hope you have enjoyed and found it useful.Thank you.