additional_ipython.rst
author Puneeth Chaganti <punchagan@fossee.in>
Thu, 07 Oct 2010 12:28:12 +0530
changeset 242 a33e942379d7
parent 212 c3172a51b555
permissions -rw-r--r--
Cleaned up script for getting started with files LO.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
212
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
     1
.. Author              : Nishanth
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
     2
   Internal Reviewer 1 : 
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
     3
   Internal Reviewer 2 : 
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
     4
   External Reviewer   :
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
     5
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
     6
.. Prerequisites: Embellinshing plots
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
     7
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
     8
Script
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
     9
======
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
    10
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
    11
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    12
Hello friends and welcome to the tutorial on Additional Features of IPython
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    13
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    14
{{{ Show the slide containing title }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    15
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    16
{{{ Show the slide containing the outline slide }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    17
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    18
In this tutorial, we shall look at additional features of IPython that help us
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    19
to retreive the commands that we type on the interpreter and then save them
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    20
into a file and run it.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    21
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    22
Let us start ipython with pylab loaded, by typing
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    23
::
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    24
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    25
    $ ipython -pylab
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    26
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    27
on the terminal
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    28
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    29
{{{ shit to terminal and type ipython -pylab }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    30
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    31
We shall first make a plot and then view the history and save it.
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    32
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    33
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    34
    x = linspace(-2*pi, 2*pi, 100)
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    35
    plot(x, xsinx(x))
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    36
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    37
xsin(x) is actually x * sin(x)
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    38
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    39
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    40
    plot(x, x*sin(x))
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    41
    plot(x, sin(x))
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    42
    xlabel("x")
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    43
    ylabel("$f(x)$")   
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    44
    title("x and xsin")
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    45
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    46
We now have the plot. Let us look at the commands that we have typed in. The
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    47
history can be retreived by using =%hist= command. Type
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    48
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    49
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    50
    %hist
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    51
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    52
As you can see, it displays a list of recent commands that we typed. Every
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    53
command has a number in front, to specify in which order and when it was typed.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    54
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    55
Please note that there is a % sign before the hist command. This implies that 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    56
%hist is a command that is specific to IPython and not available in vannila 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    57
Python interpreter. These type of commands are called as magic commands.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    58
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    59
Also note that, the =%hist= itself is a command and is displayed as the most
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    60
recent command. This implies that anything we type in is stored as history, 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    61
irrespective of whether it is command or an error or IPython magic command.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    62
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    63
If we want only the recent 5 to be displayed, we pass the number as an argument
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    64
to =%hist= command. Hence
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    65
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    66
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    67
    %hist 5 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    68
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    69
displays the recent 5 commands, inclusive of the =%hist= command.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    70
The default number is 40.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    71
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    72
{{{ Pause here and try out the following exercises }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    73
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    74
%% 1 %% Read through the %hist documenatation and find out how can we list all
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    75
        the commands between 5 and 10
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    76
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    77
{{{ continue from paused state }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    78
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    79
As we can see from =%hist= documentation,
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    80
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    81
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    82
    %hist 5 10
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    83
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    84
displays the commands from 5 to 10
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    85
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    86
Now that we have the history, we would like to save the required line of code
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    87
from history. This is possible by using the =%save= command.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    88
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    89
Before we do that, let us first look at history and identify what lines of code
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    90
we require.Type
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
    91
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    92
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    93
    %hist
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    94
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    95
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    96
{{{ point to the lines }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    97
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    98
The first command is linspace. But second command is a command that gave us an
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
    99
error. Hence we do not need seconf. The commands from third to sixth are 
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   100
required. The seventh command although is correct, we do not need it since we
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   101
are setting the title correctly in the eigthth command.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   102
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   103
So we need first third to sixth and the eigthth command for our program.
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   104
Hence the syntax of =%save= is
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   105
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   106
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   107
    %save /home/fossee/plot_script.py 1 3-6 8
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   108
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   109
{{{ point to the output of the command }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   110
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   111
The command saves first and then third to sixth and eighth lines of code into
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   112
the specified file.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   113
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   114
The first argument to %save is the path of file to save the commands and the
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   115
arguments there after are the commands to be saved in the given order.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   116
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   117
{{{ goto the file and open it and show it }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   118
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   119
{{{ Pause here and try out the following exercises }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   120
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   121
%% 2 %% change the label on y-axis to "y" and save the lines of code
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   122
        accordingly
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   123
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   124
{{{ continue from paused state }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   125
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   126
we use the command =ylabel= on interpreter as
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   127
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   128
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   129
    ylabel("y")
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   130
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   131
and then do
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   132
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   133
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   134
    %save /home/fossee/example_plot.py 1 3-6 10
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   135
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   136
Now that we have the required lines of code in a file, let us learn how to run
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   137
the file as a python script.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   138
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   139
We use the IPython magic command =%run= to do this. Type
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   140
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   141
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   142
   %run -i /home/fossee/plot_script.py
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   143
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   144
The script runs but we do not see the plot. This happens because we are running
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   145
a script and we are not in interactive mode anymore.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   146
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   147
Hence on your terminal type
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   148
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   149
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   150
    show()
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   151
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   152
to show the plot.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   153
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   154
{{{ Pause here and try out the following exercises }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   155
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   156
%% 3 %% Use %hist and %save and create a script that has show in it and run it
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   157
        to produce and show the plot.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   158
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   159
{{{ continue from paused state }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   160
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   161
We first look at the history using
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   162
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   163
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   164
    %hist 20
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   165
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   166
Then save the script using
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   167
::
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   168
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   169
    %save /home/fossee/show_included.py 1 3-6 8 10 13
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   170
    %run -i /home/fossee/show_included.py
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   171
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   172
We get the desired plot.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   173
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   174
The reason for including a -i after run is to tell the interpreter that if any
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   175
name is not found in script, search for it in the interpreter. Hence all these
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   176
sin, plot, pi and show which are not available in script, are taken from the
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   177
interpreter and used to run the script.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   178
212
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   179
{{{ Pause here and try out the following exercises }}}
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   180
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   181
%% 4 %% Run the script without using the -i option. Do you find any difference?
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   182
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   183
{{{ continue from paused state }}}
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   184
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   185
We see that it raises nameerror saying the name linspace is not found.
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   186
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   187
{{{ Show summary slide }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   188
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   189
This brings us to the end of the tutorial.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   190
we have looked at 
136
7f8b6a9fb61d added a newline before :: so that a colon does not appear in html
nishanth
parents: 131
diff changeset
   191
131
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   192
 * Retreiving history using =%hist= command
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   193
 * Vieweing only a part of history by passing an argument to %hist
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   194
 * saving the required lines of code in required order using %save
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   195
 * using %run -i command to run the saved script
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   196
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   197
{{{ Show the "sponsored by FOSSEE" slide }}}
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   198
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   199
#[Nishanth]: Will add this line after all of us fix on one.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   200
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   201
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   202
Hope you have enjoyed and found it useful.
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   203
Thankyou
ca42b9821019 initial commit of additional_ipython
nishanth
parents:
diff changeset
   204
 
212
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   205
Questions
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   206
=========
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   207
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   208
 1. How do you retrieve the recent 5 commands
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   209
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   210
    a. ``%hist``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   211
    #. ``%hist -5``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   212
    #. ``%hist 5``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   213
    #. ``%hist 5-10``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   214
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   215
    Answer: ``%hist 5``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   216
    
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   217
 2. If there were 20 commands typed and ``%hist`` is used. How many commands 
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   218
    will be displayed.
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   219
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   220
    a. 10
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   221
    #. 20
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   222
    #. 21
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   223
    #. 19
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   224
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   225
    Answer: 21
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   226
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   227
 3. is ``%hist`` considered as a command
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   228
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   229
    a. True
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   230
    #. False
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   231
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   232
    Answer: True
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   233
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   234
 4. how do you retreive the commands from 20 to 50 (inclusive of 20 and 50)
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   235
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   236
    a. ``%hist 20 50``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   237
    #. ``%hist 19 50``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   238
    #. ``%hist 19 51``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   239
    #. ``%hist 21 50``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   240
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   241
    Answer: ``%hist 20 50``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   242
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   243
 5. What does the ``%hist 2 5 7`` command do
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   244
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   245
    a. lists the second, fifth and seventh commands
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   246
    #. lists the commands from 2 to 5 and the seventh command
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   247
    #. raises an error
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   248
    #. lists the commands 2 to 7
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   249
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   250
    Answer: raises an error
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   251
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   252
 6. How many commands are displayed when lot of coomands were typed and 
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   253
    ``%hist`` is used.
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   254
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   255
    a. 20
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   256
    #. 10
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   257
    #. 50
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   258
    #. 40
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   259
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   260
    Answer: 40
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   261
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   262
 7. How do you save the lines 2 3 4 5 7 9 10 11
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   263
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   264
    a. ``%save filepath 2-5 7 9-11``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   265
    #. ``%save filepath 2-11``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   266
    #. ``%save filepath``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   267
    #. ``%save 2-5 7 9 10 11``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   268
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   269
    Answer: ``%save filepath 2-5 7 9-11``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   270
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   271
 8. You are working in /home/user. Where is the file saved when you do
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   272
    ``%save hello.py 1-3`` 
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   273
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   274
    a. /home/user/hello.py
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   275
    #. /hello.py
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   276
    #. /home/hello.py
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   277
    #. /home/user/ipython/hello.py
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   278
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   279
    Answer: /home/user/hello.py
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   280
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   281
 9. Which lines are saved by the command ``%save filepath 2-5 7 1`` and in
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   282
    which order
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   283
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   284
    a. 2 3 4 5 7 1
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   285
    #. 1 2 3 4 5 6 7
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   286
    #. 2 5 7 1
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   287
    #. 1 2 5 7
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   288
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   289
 10. What happens when ``%save filepath line_numbers`` is used and a file
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   290
     already exists in that path.
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   291
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   292
    a. It is overwritten
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   293
    #. The commands are added to the file
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   294
    #. It raises an error
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   295
    #. A prompt to confirm overwriting is displayed 
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   296
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   297
    Answer: A prompt to confirm overwriting is displayed 
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   298
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   299
 11. Read through the documentation of ``%hist`` and find its alternative name
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   300
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   301
    Answer: ``%history``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   302
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   303
 12. Are ``%run /home/user/saved.py`` and ``%run /home/user/saved`` the same
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   304
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   305
   a. Yes
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   306
   #. No
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   307
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   308
   Answer: Yes
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   309
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   310
 13. The file hello.py contains only one command ``x = x + 1``. What happens
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   311
     when you do ``%run hello.py``
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   312
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   313
    Answer: Raises a nameerror
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   314
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   315
  14. The file hello.py contains only one command ``x = x + 1``. If value of x
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   316
      is 5 and what does ``%run -i hello.py`` do.
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   317
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   318
    a. raises an error
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   319
    #. increments value of x by 1
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   320
    #. Does nothing
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   321
    
c3172a51b555 Added questions
Nishanth Amuluru <nishanth@fossee.in>
parents: 136
diff changeset
   322
    Answer: increments the value of x by 1