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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
{| border=1
 
{| border=1
!Timing
+
|'''Time'''
!Narration
+
|'''Narration'''
 
|-
 
|-
| 0:00
+
| 00:00
 
| Hello friends and welcome to the tutorial on "Additional Features of IPython".
 
| Hello friends and welcome to the tutorial on "Additional Features of IPython".
  
 
|-
 
|-
| 0:06
+
| 00:06
 
| At the end of this tutorial, you will be able to,
 
| At the end of this tutorial, you will be able to,
 
+
Retrieve your ipython history.
# Retrieve your ipython history.
+
View a part of the history.
# View a part of the history.
+
Save a part of your history to a file.
# Save a part of your history to a file.
+
Run a script from within ipython.
# Run a script from within ipython.
+
 
+
 
+
  
 
|-
 
|-
| 0:19
+
| 00:19
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Embellishing a plot".
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Embellishing a plot".
  
 
|-
 
|-
|0:24
+
|00:24
|Let us start ipython with pylab loaded, by typing ipython hyphen pylab on the terminal.
+
|Let us start ipython with pylab loaded, by typing ipython space hyphen pylab on the terminal.
  
 
|-
 
|-
| 0:33
+
|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
 
| 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
plot within bracket x comma  xsin cosx  and hit enter
+
 
+
  
 
|-
 
|-
|1:00
+
|01:00
 
| We got an error saying "xsinx is not defined".
 
| We got an error saying "xsinx is not defined".
  
 
|-
 
|-
|1:04
+
|01:04
|This is because xsin(x) is actually x star sin(x), for that we have type in the terminal
+
|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 within bracket x comma x star sin(x)
 
 
plot x, sin(x)
 
plot x, sin(x)
xlabel within bracket in double quotes x
+
xlabel within bracket in double quotes x then
ylabel within bracket in double quotes  dollar sign f(x) dollar sign
+
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)
+
title within bracket in double quotes x and xsin(x) then again
title within bracket in double quotes dollar sign x and xsin(x) dollar sign
+
title within bracket in double quotes dollar sign x and xsin(x) and again a dollar sign
 
+
  
 
|-
 
|-
2:15
+
02:15
 
| We now have the plot.  
 
| We now have the plot.  
  
 
|-
 
|-
|2:18
+
|02:18
 
|Let us look at the commands that we have typed in.
 
|Let us look at the commands that we have typed in.
  
 
|-
 
|-
|2:21
+
|02:21
| The history can be retrieved by using =modulo hist= command.
+
| The history can be retrieved by using modulo hist command.
  
 
|-
 
|-
|2:28
+
|02:28
| Type  modulo that is percentage sign hist in your terminal and hit enter
+
| Type  modulo that is percentage sign then hist and hit enter
  
 
|-
 
|-
| 2:37
+
| 02:37
 
| As you can see, it displays a list of recent commands that we typed.
 
| As you can see, it displays a list of recent commands that we typed.
  
 
|-
 
|-
|2:41
+
|02:41
 
| Every command has a number in front, to specify in which order and when it was typed.
 
| Every command has a number in front, to specify in which order and when it was typed.
  
 
|-
 
|-
|2:48
+
|02:48
|Please note that there is a percentage sign before the hist command.
+
|So, Please note that there is a percentage sign before the hist command.
  
 
|-
 
|-
|2:52
+
|02:52
 
| This implies that percentage hist is a command that is specific to IPython only and not to any other version of python.
 
| This implies that percentage hist is a command that is specific to IPython only and not to any other version of python.
  
 
|-
 
|-
|3:00
+
|03:00
 
| These other type of commands are called as magic commands.
 
| These other type of commands are called as magic commands.
  
 
|-
 
|-
|3:04
+
|03:04
|Also note that, the =%hist= itself is a command and is displayed as the most recent command.
+
|Also note that, the %hist itself is a command and is displayed as the most recent command.
  
 
|-
 
|-
|3:15
+
|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.
 
| 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.
  
 
|-
 
|-
| 3:27
+
|03:27
| If we want only the recent 5 commands to be displayed, we pass the number as an argument to =percentage hist= command.
+
| If we want only the recent 5 commands to be displayed, we pass the number as an argument to percentage hist command.
  
 
|-
 
|-
|3:35
+
|03:35
| Hence percentage hist 5 displays the recent 5 commands, inclusive of the =percentage hist= command.  
+
| Hence percentage hist 5 displays the recent 5 commands, inclusive of the percentage hist command that we gave.  
  
 
|-
 
|-
|3:46
+
|03:46
 
|The default number is 40.
 
|The default number is 40.
  
 
|-
 
|-
|3:48
+
|03:48
 
|So to try this we can type in the terminal percentage hist space 5 and hit enter.
 
|So to try this we can type in the terminal percentage hist space 5 and hit enter.
  
 
|-
 
|-
| 3:58
+
| 03:58
 
| Pause the video here, try out the following exercise and resume the video.
 
| Pause the video here, try out the following exercise and resume the video.
  
 
|-
 
|-
|4:02
+
|04:02
 
|Read through the documentation of %hist and find out how to list all the commands between 5 and 10.
 
|Read through the documentation of %hist and find out how to list all the commands between 5 and 10.
  
 
|-
 
|-
| 4:11
+
| 04:11
| As we can see from =percentage hist= documentation,percentage hist 5 10 displays the commands from 5 to 10
+
| As we can see from percentage hist documentation,percentage hist 5 space 10 displays the commands from 5 to 10
  
 
|-
 
|-
|4:22
+
|04:22
|Now that we have the history, we will try it on the command prompt so type percentage hist 5 space 10 and hit enter.
+
|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.
  
 
|-
 
|-
|4:35
+
|04:35
 
| we would like to save the required line of code from history.
 
| we would like to save the required line of code from history.
  
 
|-
 
|-
|4:39
+
|04:39
| This is possible by using the =percentage save= command.
+
| So, This is possible by using the percentage save command.
  
 
|-
 
|-
|4:47
+
|04:47
| Before we do that, let us first look at history and identify what lines of code we require. So type percentage hist and hit enter.
+
| 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.
  
 
|-
 
|-
| 4:58
+
| 04:58
 
| The first command is linspace.
 
| The first command is linspace.
  
 
|-
 
|-
| 5:00
+
| 05:00
 
| But second command is a command that gave us an error.
 
| But second command is a command that gave us an error.
  
 
|-
 
|-
|5:06
+
|05:06
 
| Hence we do not need second command.
 
| Hence we do not need second command.
  
 
|-
 
|-
|5:09
+
|05:09
 
| The commands from third to sixth are required.  
 
| The commands from third to sixth are required.  
  
 
|-
 
|-
|5:12
+
|05:12
 
|The seventh command although is correct, we do not need it since we are setting the title correctly in the eighth command.
 
|The seventh command although is correct, we do not need it since we are setting the title correctly in the eighth command.
  
 
|-
 
|-
| 5:20
+
| 05:20
 
| So we need first third to sixth and the eighth command for our program.
 
| So we need first third to sixth and the eighth command for our program.
  
 
|-
 
|-
|5:26
+
|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
 
| 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
  
 
|-
 
|-
5:46
+
05:46
| The command saves the first line of code and then third to sixth followed by the eighth lines of code into the specified file.
+
| This command saves the first line of code and then third to sixth followed by the eighth lines of code into the specified file.
  
 
|-
 
|-
|5:55
+
|05:55
|The first argument to percentage 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.
+
|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.
  
 
|-
 
|-
| 6:08
+
| 06:08
 
| Pause the video here, try out the following exercise and resume the video.
 
| Pause the video here, try out the following exercise and resume the video.
  
 
|-
 
|-
|6:11
+
|06:11
 
|Change the label on y-axis to "y" and save the lines of code accordingly.
 
|Change the label on y-axis to "y" and save the lines of code accordingly.
  
 
|-
 
|-
| 6:17
+
| 06:17
| we use the command =ylabel= on interpreter
+
| we use the command ylabel on interpreter
  
 
|-
 
|-
|6:21
+
|06:21
|ylabel within brackets in double quotes y and hit enter
+
|So you can type, ylabel within brackets in double quotes y and hit enter
  
 
|-
 
|-
| 6:29
+
| 06:29
 
| and then you can do percentage save command
 
| and then you can do percentage save command
  
 
|-
 
|-
|6:36
+
|06:36
| That is you can type percentage save slash home slash fossee slash example underscore plot dot py space 1 space 3 hyphen 6 space 10
+
| 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
  
 
|-
 
|-
|6:51
+
|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.
 
| Now that we have the required lines of code in a file, let us learn how to run the file as a python script.
  
 
|-
 
|-
|6:58
+
|06:58
|We use the IPython magic command =percentage run= to do this.  
+
|We use the IPython magic command percentage run to do this.  
  
 
|-
 
|-
|7:03
+
|07:03
|Type percentage run space hyphen i space slash home slash fossee slash plot underscore script dot py  
+
|So Type percentage run space hyphen i space slash home slash fossee slash plot underscore script dot py  
  
 
|-
 
|-
|7:18
+
|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.
 
| 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.
  
 
|-
 
|-
|7:27
+
|07:27
|Hence to view the plot type show then brackets on your terminal
+
|Hence to view the plot, you have to type, show and then brackets on your terminal
  
 
|-
 
|-
| 7:38
+
| 07:38
|  So now Pause the video here, try out the following exercise and resume the video.
+
|  So now Pause the video here, try out the following exercise  
  
 
|-
 
|-
|7:41
+
|07:41
 
|Use percentage hist and percentage save and create a script that has the function show() in it.
 
|Use percentage hist and percentage save and create a script that has the function show() in it.
  
 
|-
 
|-
|7:49
+
|07:49
 
| Run the script to produce the plot and display the name.
 
| Run the script to produce the plot and display the name.
  
 
|-
 
|-
|7:56
+
|07:56
 
|So you can type percentage hist space 20 on the terminal
 
|So you can type percentage hist space 20 on the terminal
  
 
|-
 
|-
|8:06
+
|08:06
 
|We first look at the history using this command only,
 
|We first look at the history using this command only,
  
 
|-
 
|-
| 8:09
+
| 08:09
| Then save the script using the command percentage save space slash home slash fossee slash show underscore included dot py space  1  space 3 hyphen 6  space 8 , 14  and 17
+
| 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
  
 
|-
 
|-
|8:37
+
|08:37
| then on the next line you can type
+
| 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.
percentage run hyphen i slash home slash fossee slash show underscore included dot py, then type show function to get the plot.
+
  
 
|-
 
|-
| 9:03
+
| 09:03
 
| We get the desired plot now.
 
| We get the desired plot now.
  
 
|-
 
|-
|9:07
+
|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.  
 
|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.  
  
 
|-
 
|-
|9:17
+
|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.
 
|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.
  
 
|-
 
|-
| 9:32
+
| 09:32
| Pause the video here, try out the following exercise and resume the video.
+
| So now, Pause the video here, try out the following exercise and resume the video.
  
 
|-
 
|-
|9:36
+
|09:36
 
|Run the script without using the hyphen i option.  
 
|Run the script without using the hyphen i option.  
  
 
|-
 
|-
|9:42
+
|09:42
|Do you find any difference?
+
|So Do you find any difference?
  
 
|-
 
|-
|9:47
+
|09:47
 
|So you can type percentage run slash home slash fossee slash included hypen  included dot py
 
|So you can type percentage run slash home slash fossee slash included hypen  included dot py
  
Line 283: Line 275:
 
|-
 
|-
 
|10:14
 
|10:14
|In this tutorial,we have learnt to, Retrieve the history using =percentage hist= command.
+
|In this tutorial,we have learnt to, Retrieve the history using percentage hist command.
  
 
|-
 
|-
Line 296: Line 288:
 
|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.
 
 
  
 
|-
 
|-
| 10;39
+
| 10:39
 
| Here are some self assessment questions for you to solve
 
| Here are some self assessment questions for you to solve
  
Line 312: Line 302:
 
|-
 
|-
 
|10:57
 
|10:57
|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,percentage save filepath 2 hyphen 11,percentage save filepath  percentage save 2 hyphen 5 7 9 10 11
+
|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
 
|11:24
|And the final question is What will the command percentage hist 5 10 display.
+
|And the final question is What will the command percentage hist 5 space 10 display.
  
 
|-
 
|-
Line 332: Line 322:
 
|-
 
|-
 
|11:41
 
|11:41
| In order to retrieve the recently typed 5 commands,we say percentage hist 5 for the first question.
+
| In order to retrieve the recently typed 5 commands,we have to say percentage hist 5 for the first question.
  
 
|-
 
|-
Line 340: Line 330:
 
|-
 
|-
 
|12:02
 
|12:02
| Then third answer is percentage hist 5 space 10 will display the recently typed commands from 5 to 10 inclusive of the history command.
+
|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.
  
 
|-
 
|-

Latest revision as of 17:57, 20 February 2017

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,

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.

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