Difference between revisions of "Python/C2/Additional-features-of-IPython/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 294: Line 294:
 
|10:30
 
|10:30
 
|then Use 'percentage run hyphen i' command to run the saved script.
 
|then Use 'percentage run hyphen i' command to run the saved script.
 
 
  
 
|-
 
|-

Revision as of 12:30, 10 July 2014

Time Narration
00:00 Hello friends and welcome to the tutorial on "Additional Features of IPython".
00:06 At the end of this tutorial, you will be able to,
  1. Retrieve your ipython history.
  2. View a part of the history.
  3. Save a part of your history to a file.
  4. Run a script from within ipython.
00:19 Before beginning this tutorial,we would suggest you to complete the tutorial on "Embellishing a plot".
00:24 Let us start ipython with pylab loaded, by typing ipython space hyphen pylab on the terminal.
00:33 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


01:00 We got an error saying "xsinx is not defined".
01:04 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


02:15 We now have the plot.
02:18 Let us look at the commands that we have typed in.
02:21 The history can be retrieved by using modulo hist command.
02:28 Type modulo that is percentage sign then hist and hit enter
02:37 As you can see, it displays a list of recent commands that we typed.
02:41 Every command has a number in front, to specify in which order and when it was typed.
02:48 So, Please note that there is a percentage sign before the hist command.
02:52 This implies that percentage hist is a command that is specific to IPython only and not to any other version of python.
03:00 These other type of commands are called as magic commands.
03:04 Also note that, the %hist itself is a command and is displayed as the most recent command.
03:15 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.
03:27 If we want only the recent 5 commands to be displayed, we pass the number as an argument to percentage hist command.
03:35 Hence percentage hist 5 displays the recent 5 commands, inclusive of the percentage hist command that we gave.
03:46 The default number is 40.
03:48 So to try this we can type in the terminal percentage hist space 5 and hit enter.
03:58 Pause the video here, try out the following exercise and resume the video.
04:02 Read through the documentation of %hist and find out how to list all the commands between 5 and 10.
04:11 As we can see from percentage hist documentation,percentage hist 5 space 10 displays the commands from 5 to 10
04:22 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.
04:35 we would like to save the required line of code from history.
04:39 So, This is possible by using the percentage save command.
04:47 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.
04:58 The first command is linspace.
05:00 But second command is a command that gave us an error.
05:06 Hence we do not need second command.
05:09 The commands from third to sixth are required.
05:12 The seventh command although is correct, we do not need it since we are setting the title correctly in the eighth command.
05:20 So we need first third to sixth and the eighth command for our program.
05:26 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
05:46 This command saves the first line of code and then third to sixth followed by the eighth lines of code into the specified file.
05:55 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.
06:08 Pause the video here, try out the following exercise and resume the video.
06:11 Change the label on y-axis to "y" and save the lines of code accordingly.
06:17 we use the command ylabel on interpreter
06:21 So you can type, ylabel within brackets in double quotes y and hit enter
06:29 and then you can do percentage save command
06:36 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
06:51 Now that we have the required lines of code in a file, let us learn how to run the file as a python script.
06:58 We use the IPython magic command percentage run to do this.
07:03 So Type percentage run space hyphen i space slash home slash fossee slash plot underscore script dot py
07:18 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.
07:27 Hence to view the plot, you have to type, show and then brackets on your terminal
07:38 So now Pause the video here, try out the following exercise
07:41 Use percentage hist and percentage save and create a script that has the function show() in it.
07:49 Run the script to produce the plot and display the name.
07:56 So you can type percentage hist space 20 on the terminal
08:06 We first look at the history using this command only,
08:09 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
08:37 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.

09:03 We get the desired plot now.
09:07 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.
09:17 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.
09:32 So now, Pause the video here, try out the following exercise and resume the video.
09:36 Run the script without using the hyphen i option.
09:42 So Do you find any difference?
09:47 So you can type percentage run slash home slash fossee slash included hypen included dot py
10:04 We see that it raises Name Error saying that the name linspace is not found.
10:12 This brings us to the end of this tutorial.
10:14 In this tutorial,we have learnt to, Retrieve the history using percentage hist command.
10:21 then View only a part of history by passing an argument to 'percentage hist' command.
10:25 then Save the required lines of code in required order using 'percentage save' command.
10:30 then Use 'percentage run hyphen i' command to run the saved script.
10;39 Here are some self assessment questions for you to solve
10:43 5. How do you retrieve the recent 5 commands
10:46 And there are four options percentage hist,percentage hist minus 5,percentage hist 5,percentage hist 5 to 10
10:57 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
11:24 And the final question is What will the command percentage hist 5 space 10 display.
11:30 The recently typed commands from 5 to 10 inclusive of the history command
11:34 The recently typed commands from 5 to 10 excluding the history command
11:38 So now the answers,
11:41 In order to retrieve the recently typed 5 commands,we have to say percentage hist 5 for the first question.
11:49 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.
12:02 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.
12:14 This brings us to the end of this tutorial
12:17 So we Hope you have enjoyed and found it useful.Thank you.

Contributors and Content Editors

Gaurav, Kavita salve, Krupali, Minal, PoojaMoolya, Sneha