diff options
Diffstat (limited to 'additional_features_of_ipython/script.rst')
-rw-r--r-- | additional_features_of_ipython/script.rst | 131 |
1 files changed, 76 insertions, 55 deletions
diff --git a/additional_features_of_ipython/script.rst b/additional_features_of_ipython/script.rst index 087f43f..f178e88 100644 --- a/additional_features_of_ipython/script.rst +++ b/additional_features_of_ipython/script.rst @@ -14,6 +14,7 @@ .. 1. Embellishing Plots + .. Author : Nishanth Amuluru Internal Reviewer : Amit External Reviewer : @@ -30,7 +31,8 @@ team along with the logo of MHRD }}} .. R1 -Hello friends and welcome to the tutorial on "Additional Features of IPython". +Hello friends and welcome to the tutorial on +"Additional Features of IPython". .. L2 @@ -47,10 +49,16 @@ At the end of this tutorial, you will be able to, .. R3 -Let us start ipython with pylab loaded, by typing ipython -pylab on the terminal. +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. .. L3 +{{{ Show slide with pre-requisite }}} + {{{ shift to terminal and type ipython -pylab }}} :: @@ -85,8 +93,9 @@ xsin(x) is actually x * sin(x) .. R6 -We now have the plot. Let us look at the commands that we have typed in. The -history can be retreived by using =%hist= command.Type %hist in your terminal. +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. .. L6 @@ -96,24 +105,28 @@ history can be retreived by using =%hist= command.Type %hist in your terminal. .. R7 -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. +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. +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 -recent 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. +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. .. L7 .. R8 -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. +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. .. L8 @@ -130,7 +143,7 @@ The default number is 40. .. L9 -{{{ Show slide with question 1 }}} +{{{ Show slide with exercise 1 }}} .. L10 @@ -142,14 +155,16 @@ The default number is 40. .. R10 -As we can see from =%hist= documentation,%hist 5 10 displays the commands from 5 to 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. +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. .. R11 -Before we do that, let us first look at history and identify what lines of code we require.Type %hist again. +Before we do that, let us first look at history and identify what +lines of code we require.Type %hist again. .. L11 @@ -163,10 +178,10 @@ Before we do that, let us first look at history and identify what lines of code .. R12 -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. +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. .. R13 @@ -187,11 +202,11 @@ Hence the syntax of =%save= will be .. R14 -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 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. +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. .. L15 @@ -203,11 +218,11 @@ arguments there after are the commands to be saved in the given order. 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. + Change the label on y-axis to "y" and save the lines of code accordingly. .. L16 -{{{ Show slide with question 2 }}} +{{{ Show slide with exercise 2 }}} .. L17 @@ -235,8 +250,8 @@ and then do .. R19 -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. We use the IPython magic command =%run= to do this. Type @@ -248,8 +263,8 @@ We use the IPython magic command =%run= to do this. Type .. R20 -The script runs but we do not see the plot. This happens because when we are running -a script and we are not in 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. Hence to view the plot type ``show()`` on your terminal @@ -261,10 +276,10 @@ Hence to view the plot type ``show()`` on your terminal .. R21 - 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. - 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. + 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> @@ -272,7 +287,7 @@ We first look at the history using .. L21 -{{{ Show slide with question 3 }}} +{{{ Show slide with exercise 3 }}} :: @@ -296,24 +311,24 @@ 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. +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. .. L23 .. R24 - 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. - Run the script without using the -i option. Do you find any difference? + Run the script without using the -i option. Do you find any difference? <Pause> .. L24 -{{{ Show slide with question 4 }}} +{{{ Show slide with exercise 4 }}} .. L25 @@ -331,16 +346,18 @@ We see that it raises NameError saying that the name linspace is not found. .. R26 -This brings us to the end of the tutorial.let's revise quickly what we have learnt today- +This brings us to the end of this tutorial.In this tutorial,we have learnt to, - 1. to retreive the history using =%hist= command. - #. to view only a part of history by passing an argument to %hist. - #. to save the required lines of code in required order using %save command. - #. to use %run -i command to run the saved script. + 1. Retrieve the history using =%hist= command. + #. View only a part of history by passing an argument to '%hist' + command. + #. Save the required lines of code in required order using '%save' + command. + #. Use '%run -i' command to run the saved script. .. L27 -{{Show self assessment questions slide}} +{{Show self assessment questionss slide}} .. R27 @@ -364,12 +381,14 @@ Here are some self assessment questions for you to solve 3. What will the command ``%hist 5 10`` display. - - The recently typed commands from 5 to 10 inclusive of the history command - - The recently typed commands from 5 to 10 excluding the history command + - The recently typed commands from 5 to 10 inclusive of the + history command + - The recently typed commands from 5 to 10 excluding the + history command .. L28 -(solution of self assessment questions on slide) +{{{solution of self assessment questions on slide}}} .. R28 @@ -377,9 +396,11 @@ And the answers, 1. In order to retrieve the recently typed 5 commands,we say ``%hist 5``. -2. ``%save filepath 2-5 7 9-11`` is the correct option to the specified lines of codes. +2. ``%save filepath 2-5 7 9-11`` is the correct option to the specified + lines of codes. -3. ``%hist 5 10`` will display the recently typed commands from 5 to 10 inclusive of the history command. +3. ``%hist 5 10`` will display the recently typed commands from 5 to 10 + inclusive of the history command. .. L29 |