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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(4 intermediate revisions by 2 users not shown)
Line 29: Line 29:
 
# Save a part of a '''history''' to a file.
 
# Save a part of a '''history''' to a file.
 
# Run a script from within''' IPython'''.
 
# Run a script from within''' IPython'''.
 
 
  
 
|-
 
|-
Line 41: Line 39:
 
* '''Python 3.4.3'''
 
* '''Python 3.4.3'''
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 71: Line 67:
  
 
'''%pylab '''and press '''Enter.'''
 
'''%pylab '''and press '''Enter.'''
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Let us initialise the pylab package.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Let us initialise the '''pylab package'''.
  
  
Type '''%pylab '''and press''' Enter'''
+
Type '''percentage pylab '''and press''' Enter'''
  
 
|-
 
|-
Line 83: Line 79:
  
 
press''' Enter'''
 
press''' Enter'''
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| To start plotting, type'' '''''x''''' '''''equals to''''' '''''linspace inside the brackets minus 2*pi comma 2*pi comma 100 '''then press''' Enter.'''
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| To start plotting, type '''x equals to linspace''' inside the '''brackets minus 2pi comma 2pi comma 100 '''then press''' Enter.'''
  
 
|-
 
|-
Line 92: Line 88:
  
 
press''' Enter'''
 
press''' Enter'''
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Next type'' '''''plot inside the brackets x comma xsin(x) '''and then press''' Enter'''
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Next type '''plot''' inside the '''brackets x comma xsin(x) '''and then press''' Enter'''
 
+
 
+
  
  
Line 100: Line 94:
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Highlight the message “'''xsin''' is not defined” on the''' terminal.'''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Highlight the message “'''xsin''' is not defined” on the''' terminal.'''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| We got an error saying "'''xsin''' is not defined".
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| We got an error saying "'''xsin''' is not defined".
 
  
 
This is because '''xsin(x) '''is actually '''x '''multiplied by '''sin(x)'''
 
This is because '''xsin(x) '''is actually '''x '''multiplied by '''sin(x)'''
Line 111: Line 104:
  
 
Press''' Enter'''
 
Press''' Enter'''
 
 
  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Here, multiplication sign is missing. So let us type
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Here, multiplication sign is missing. So let us type
  
'''plot inside the brackets x comma x multiplied by sin(x)''' and press Enter'''.'''
+
'''plot''' inside the '''brackets x comma x multiplied by sin(x)''' and press Enter'''.'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| [IPython Terminal]
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| [IPython Terminal]
 
  
 
Type
 
Type
Line 133: Line 123:
 
'''title("$x and xsin(x)$")'''
 
'''title("$x and xsin(x)$")'''
  
 
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Next let us add '''title''' and the '''labels''' for both '''x''' and '''y axes'''.
 
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Next let us add '''title''' and the '''labels''' for both x and y axes.
+
  
  
 
Type
 
Type
  
'''xlabel inside the brackets inside inverted commas inside dollar sign x '''press '''Enter.'''
+
'''xlabel''' inside the '''brackets''' inside '''inverted commas''' inside '''dollar sign x '''press '''Enter.'''
  
  
'''Ylabel inside the brackets inside inverted commas inside dollar sign f(x) '''press '''Enter.'''
+
'''ylabel''' inside the '''brackets''' inside '''inverted commas''' inside '''dollar sign f(x) '''
  
  
'''Title inside the brackets inside inverted commas inside dollar sign x and xsin(x) '''press '''Enter.'''
+
'''Title''' inside the '''brackets''' inside ''''inverted commas''' inside '''dollar sign x and xsin(x) '''
  
  
Line 154: Line 142:
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| [IPython Terminal]
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| [IPython Terminal]
  
Type
+
Type '''%history''' and press''' Enter'''
 +
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| The '''history''' of typed '''commands''' can be retrieved by''' percentage history command'''.
  
'''%history'''
+
Type '''percentage history''' and press''' Enter'''.
 
+
and press''' Enter'''
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| The '''history''' of typed commands can be retrieved by''' %history''' command.
+
 
+
 
+
Type'' '''''%history''' and press''' Enter'''.
+
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"|  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"|  
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| '''%history '''itself is a '''command''' and is displayed as the most recent '''command'''.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| '''percentage history '''itself is a '''command''' and is displayed as the most recent '''command'''.
  
 
+
Whatever we executed in the '''terminal''' is stored as history.
Whatever we executed in the terminal is stored as history.
+
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| [IPython Terminal]
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| [IPython Terminal]
  
 +
Type '''%history 5'''  and press''' Enter'''
 +
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| If we want to see what was the fifth '''command''', pass 5 as an '''argument''' to''' percentage history command'''.
  
Type
 
 
'''%history 5'''
 
 
press''' Enter'''
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| If we want to see what was the fifth '''command''', pass 5 as an argument to''' %history''' '''command'''.
 
  
 +
Type '''percentage history space 5 '''and press''' Enter'''.
  
Type '''%history space 5 '''and press''' Enter'''. This displays the fifth '''command''' which we typed.  
+
This displays the fifth '''command''' which we typed.  
  
 
|-
 
|-
Line 196: Line 175:
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"|  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"|  
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Switch back to terminal.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Switch back to '''terminal'''.
  
 
|-
 
|-
Line 215: Line 194:
  
  
Type '''%history? '''
+
Type '''percentage history question mark '''
  
  
Read through the information of''' %history '''command'''. '''
+
Read through the information of''' percentage history command'''.
  
  
We can see, '''%history hypen n 4 hypen 6''' displays the '''commands''' from 4 to 6.  
+
We can see, '''percentage history hypen n 4 hypen 6''' displays the '''commands''' from 4 to 6.  
  
Here hypen '''n '''is optional '''argument''' which prints line numbers.
+
Here '''hypen n '''is an optional '''argument''' which prints line numbers.
  
  
Line 229: Line 208:
  
  
Now type'' '''''%history space 5hypen 10''' and press''' Enter.'''
+
Now type '''percentage history space 5 hypen 10''' and press''' Enter.'''
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"|  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"|  
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| To save the '''history''', we use '''%save''' command.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| To save the '''history''', we use '''percentage save command'''.
  
 
|-
 
|-
Line 242: Line 221:
  
  
Type''' ''%''history''' and press''' Enter'''.
+
Type''' percentage history''' and press''' Enter'''.
  
 
|-
 
|-
Line 248: Line 227:
  
 
Highlight the commands on the terminal.
 
Highlight the commands on the terminal.
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| The second command is '''linspace'''. But the third '''command''' is a '''command''' that gave us an error.  
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| The second '''command''' is '''linspace'''.  
  
 +
But the third '''command''' is a '''command''' that gave us an error.
  
Hence we do not need it. The '''commands''' from fourth to seventh are required.  
+
 
 +
Hence we do not need it.  
 +
 
 +
The '''commands''' from fourth to seventh are required.  
  
 
|-
 
|-
Line 261: Line 244:
  
 
and press''' Enter'''
 
and press''' Enter'''
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| So we need the second command and then the fourth to seventh '''commands''' for our program.  
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| So we need the second '''command''' and then the fourth to seventh '''commands''' for our program.  
  
  
 
Let us save it in the present working directory. Hence the syntax will be
 
Let us save it in the present working directory. Hence the syntax will be
  
'''%save space plot_script.py 2 space 4 hypen 7'''. press''' Enter'''
+
'''percentage save space plot underscore script.py space 2 space 4 hypen 7'''. press''' Enter'''
  
 
|-
 
|-
Line 272: Line 255:
  
 
Highlight the typed command on the terminal.  
 
Highlight the typed command on the terminal.  
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| The first argument in '''%save command''' is the name of the file in which the commands are saved.  
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| The first '''argument''' in '''percentage save command''' is the name of the file in which the commands are saved.  
  
The second argument gives the numbers of the commands that are being saved separated by spaces.
+
The second '''argument''' gives the numbers of the commands that are being saved separated by spaces.
  
 
|-
 
|-
Line 281: Line 264:
  
 
Goto file '''plot_script.py. '''open and show it
 
Goto file '''plot_script.py. '''open and show it
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Let us now open the file '''plot_script.py''' and see the contents.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Let us now open the file '''plot underscore script.py''' and see the contents.
  
 
|-
 
|-
Line 294: Line 277:
  
  
We use the '''command''' '''%run''' to do this.  
+
We use the '''command percentage run''' to do this.  
  
  
Type'' '''''%run space hypen i space plot_script.py''' and press''' Enter.'''
+
Type '''percentage run space hypen i space plot underscore script.py''' and press''' Enter.'''
  
  
Line 303: Line 286:
  
  
The code is run within the current ipython session.
+
The code is run within the current '''ipython''' session.
  
  
It uses the variables defined interactively in the ipython session.  
+
It uses the '''variables''' defined interactively in the '''ipython''' session.  
  
 
|-
 
|-
Line 319: Line 302:
  
  
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| The script runs but we do not see the plot.  
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| The script runs but we do not see the '''plot'''.  
  
  
This is because when we are running a script,  
+
This is because when we are running a script, we are not in the interactive mode.
  
we are not in the interactive mode.
 
  
 
+
To view the '''plot''', type''' show()''' on your '''terminal'''. Press '''Enter.'''
To view the '''plot''', type''' show()''' on your terminal. Press '''Enter.'''
+
  
 
|-
 
|-
Line 339: Line 320:
  
  
Use''' %history''' and''' %save''' to create a script that has the function '''show()''' in it.  
+
Use '''percentage history''' and''' percentage save''' to create a script that has the function '''show()''' in it.  
  
  
Line 356: Line 337:
  
  
We first look at the '''history''' using''' %history hypen n''' command.
+
We first look at the '''history''' using''' percentage history hypen n''' command.
  
Type'' '''''%history hypen n''' and press''' Enter.'''
+
Type '''percentage history space hypen n''' and press''' Enter.'''
  
 
|-
 
|-
Line 370: Line 351:
  
  
Type '''clf() and '''press Enter
+
Type '''clf()''' and press '''Enter'''
  
 
|-
 
|-
Line 383: Line 364:
  
  
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Now let us save the '''script''' using the '''command''' '''%save'''
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Now let us save the '''script''' using the '''command''' '''percentage save'''
  
 
We need lines 2, 4 to 7 and 16
 
We need lines 2, 4 to 7 and 16
Line 389: Line 370:
 
Type
 
Type
  
'''%save show_included.py 2 space 4 hypen 7 space 16''' and press''' Enter'''
+
'''percentage save space show underscore included.py space 2 space 4 hypen 7 space 16''' and press''' Enter'''
  
 
|-
 
|-
Line 403: Line 384:
  
  
'''%run hypen i show_included.py'''
+
'''percentage run space hypen i space show underscore included.py'''
  
  
Line 419: Line 400:
  
 
and Press''' Enter'''
 
and Press''' Enter'''
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Go to the previous command. Modify it by removing ‘'''hypen i’''' in the command to,
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Go to the previous '''command'''.
  
'''%run show_included.py'''
+
Modify it by removing ‘'''hypen i’''' in the '''command''' to,
  
 +
'''percentage run space show underscore included.py'''
  
We see that it raises a '''NameError''' saying that the name '''linspace''' is not found
+
 
 +
We see that it raises a '''NameError''' saying that the name '''linspace''' is not defined.
  
  
 
This happens because we did not run the script interactively.
 
This happens because we did not run the script interactively.
 
 
 
  
 
|-
 
|-
Line 438: Line 418:
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| This brings us to the end of this tutorial. In this tutorial,we have learnt to,
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| This brings us to the end of this tutorial. In this tutorial,we have learnt to,
  
# Retrieve the '''history''' using '''%history''' command.
+
1. Retrieve the '''history''' using '''percentage history command.'''
 
+
2. View only a part of '''history''' by passing argument to''' percentage history command.'''
# View only a part of '''history''' by passing argument to''' %history''' command.
+
 
+
 
+
  
 
|-
 
|-
Line 448: Line 425:
  
 
Summary  
 
Summary  
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| # Save the required lines of code in required order using ''''%save'''' command.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"|
 
+
# Use ''''%run -i'''' command to run a saved script.
+
 
+
  
 +
3. Save the required lines of code in required order using '''percentage save command.'''
 +
4. Use ''''percentage run space hypen i'''' command to run a saved script.
  
 
|-
 
|-
Line 458: Line 434:
  
 
Self assessment 1
 
Self assessment 1
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| * How do you save the command lines 2 3 4 5 7 9 10 11
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| How do you save the command lines 2 3 4 5 7 9 10 11?
 
+
* '''percentage save <filename> 2-5 7 9 hyphen 11'''
*
+
* '''percentage save <filename> 2 hyphen 11'''
** '''%save <filename> 2-5 7 9-11'''
+
* '''percentage save <filename>'''
** '''%save <filename> 2-11'''
+
* '''percentage save 2 hyphen 5 7 9 hyphen 11'''
** '''%save <filename>'''
+
** '''%save 2-5 7 9-11'''
+
 
+
 
+
  
 
|-
 
|-
Line 472: Line 444:
  
 
Self assessment 2
 
Self assessment 2
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| * Which is the command to run the script?
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Which is the command to run the script?
 
+
* '''Percentage execute <script name>'''
*
+
* '''percentage run hyphen i <script name> '''
** '''%execute <script name>'''
+
* '''percentage run <script name> '''
** '''%run -i <script name> '''
+
* '''percentage execute hyphen i <script name>'''  
** '''%run <script name> '''
+
** '''%execute -i <script name>'''  
+
 
+
 
+
  
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Show Slide  
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Show Slide  
  
Solution for the self assessment questions
+
And the solutions are
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| And the answers,
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| And the answers,
  
# To save the commands 2 3 4 5 7 9 10 11, we issue the '''command %save <filename> 2-5 7 9-11'''
+
# To save the commands 2 3 4 5 7 9 10 11, we issue the '''command percentage save <filename> 2 hyphen 5 space 7 space 9 hyphen 11'''
 
+
# To run the script we use '''%run -i <scriptname>'''
+
  
 +
# To run the script we use '''percentage run space hyphen i <scriptname>'''
  
  
Line 524: Line 491:
 
|-
 
|-
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Previous slide
 
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Previous slide
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| This is _________ from IIT Bombay (or FOSSEE, if you wish) signing off.
+
| style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| This is Usha from IIT Bombay (or FOSSEE, if you wish) signing off.
  
 
Thank you.
 
Thank you.
  
 
|}
 
|}

Latest revision as of 23:23, 9 March 2017

Additional-features-of-IPython - English


Title: Additional-features-of-IPython – English

Author: Usha Viswanathan, Thirumalesh H S

Keywords: Video tutorial, IPython, running script, saving script,


Visual Cue
Narration
Show Slide

containing title, name of the production team along with the logo of MHRD

Hello friends and welcome to the Spoken Tutorial on Additional Features of IPython.
Show Slide

Objectives

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 a history to a file.
  4. Run a script from within IPython.
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 14.04 operating system
  • Python 3.4.3
  • IPython 5.1.0
Show Slide

Pre-requisites

To practice this tutorial, you should know how to
  • use Plots interactively
  • and embellish a plot

If not, see the pre-requisite Python tutorials on this website.

[Terminal]

ipython3


Let us now open the Terminal by pressing Ctrl+Alt+T keys simultaneously.


Now, type ipython3 and press Enter.

[IPython console]

%pylab and press Enter.

Let us initialise the pylab package.


Type percentage pylab and press Enter

[IPython Terminal]


x = linspace(-2*pi, 2*pi, 100)

press Enter

To start plotting, type x equals to linspace inside the brackets minus 2pi comma 2pi comma 100 then press Enter.
[IPython Terminal]


plot(x, xsin(x))

press Enter

Next type plot inside the brackets x comma xsin(x) and then press Enter


Highlight the message “xsin is not defined” on the terminal. We got an error saying "xsin is not defined".

This is because xsin(x) is actually x multiplied by sin(x)

[IPython Terminal]


plot(x, x*sin(x))

Press Enter

Here, multiplication sign is missing. So let us type

plot inside the brackets x comma x multiplied by sin(x) and press Enter.

[IPython Terminal]

Type


xlabel("$x$")


ylabel("$f(x)$")


title("$x and xsin(x)$")

Next let us add title and the labels for both x and y axes.


Type

xlabel inside the brackets inside inverted commas inside dollar sign x press Enter.


ylabel inside the brackets inside inverted commas inside dollar sign f(x)


Title inside the brackets inside 'inverted commas inside dollar sign x and xsin(x)


We can now see the labelled plot.

[IPython Terminal]

Type %history and press Enter

The history of typed commands can be retrieved by percentage history command.

Type percentage history and press Enter.

percentage history itself is a command and is displayed as the most recent command.

Whatever we executed in the terminal is stored as history.

[IPython Terminal]

Type %history 5 and press Enter

If we want to see what was the fifth command, pass 5 as an argument to percentage history command.


Type percentage history space 5 and press Enter.

This displays the fifth command which we typed.

Show Slide

Assignment 1

Now pause the video here, try out the following exercise and resume the video.


Find out how to list the recent commands between 5 and 10.

Switch back to terminal.
[IPython terminal]


In examples of history doc, highlight

Example >> %history -n 4-6

Now quit history doc and type

%history 5-10 and press Enter

Let us look at the solution.


Type clf()


Type percentage history question mark


Read through the information of percentage history command.


We can see, percentage history hypen n 4 hypen 6 displays the commands from 4 to 6.

Here hypen n is an optional argument which prints line numbers.


Type q to quit the documentation


Now type percentage history space 5 hypen 10 and press Enter.

To save the history, we use percentage save command.
[IPython Terminal]

Type %history and press Enter

Before we do that, let us first look at the history and identify which lines of code we require.


Type percentage history and press Enter.

[IPython Terminal]

Highlight the commands on the terminal.

The second command is linspace.

But the third command is a command that gave us an error.


Hence we do not need it.

The commands from fourth to seventh are required.

[IPython Terminal]

Type

%save plot_script.py 2 4-7

and press Enter

So we need the second command and then the fourth to seventh commands for our program.


Let us save it in the present working directory. Hence the syntax will be

percentage save space plot underscore script.py space 2 space 4 hypen 7. press Enter

[IPython Terminal]

Highlight the typed command on the terminal.

The first argument in percentage save command is the name of the file in which the commands are saved.

The second argument gives the numbers of the commands that are being saved separated by spaces.

[File Browser]


Goto file plot_script.py. open and show it

Let us now open the file plot underscore script.py and see the contents.
[IPython Terminal]

Type

%run -i plot_script.py

and press Enter

Let us learn how to run the file as a python script.


We use the command percentage run to do this.


Type percentage run space hypen i space plot underscore script.py and press Enter.


Here, hypen i parameter runs the code written in a text editor.


The code is run within the current ipython session.


It uses the variables defined interactively in the ipython session.

[IPython Terminal]

Type

show()

and press Enter


The script runs but we do not see the plot.


This is because when we are running a script, we are not in the interactive mode.


To view the plot, type show() on your terminal. Press Enter.

Show Slide

Assignment 2


Pause the video here, try out the following exercise and resume the video.


Use percentage history and percentage save to create a script that has the function show() in it.


Run the script to produce the plot and display the same.

[IPython Terminal]

%history -n

press Enter


Let us look at the solution.


We first look at the history using percentage history hypen n command.

Type percentage history space hypen n and press Enter.

[IPython Terminal]


Type clf()

press Enter

Let us clear the plot window


Type clf() and press Enter

[IPython Terminal]

Type

%save show_included.py 1 3-7 12 15

Press Enter


Now let us save the script using the command percentage save

We need lines 2, 4 to 7 and 16

Type

percentage save space show underscore included.py space 2 space 4 hypen 7 space 16 and press Enter

[IPython Terminal]

Type


%run -i show_included.py

Press Enter

To run the script type


percentage run space hypen i space show underscore included.py


Then press Enter

We get the desired plot.
[IPython Terminal]

Go to the previous command. Modify it by removing ‘-i’ in the command to, %run show_included.py


and Press Enter

Go to the previous command.

Modify it by removing ‘hypen i’ in the command to,

percentage run space show underscore included.py


We see that it raises a NameError saying that the name linspace is not defined.


This happens because we did not run the script interactively.

Show Slide

Summary

This brings us to the end of this tutorial. In this tutorial,we have learnt to,

1. Retrieve the history using percentage history command. 2. View only a part of history by passing argument to percentage history command.

Show Slide

Summary

3. Save the required lines of code in required order using percentage save command. 4. Use 'percentage run space hypen i' command to run a saved script.

Show Slide

Self assessment 1

How do you save the command lines 2 3 4 5 7 9 10 11?
  • percentage save <filename> 2-5 7 9 hyphen 11
  • percentage save <filename> 2 hyphen 11
  • percentage save <filename>
  • percentage save 2 hyphen 5 7 9 hyphen 11
Show Slide

Self assessment 2

Which is the command to run the script?
  • Percentage execute <script name>
  • percentage run hyphen i <script name>
  • percentage run <script name>
  • percentage execute hyphen i <script name>
Show Slide

And the solutions are

And the answers,
  1. To save the commands 2 3 4 5 7 9 10 11, we issue the command percentage save <filename> 2 hyphen 5 space 7 space 9 hyphen 11
  1. To run the script we use percentage run space hyphen i <scriptname>


Show Slide

Forum

Please post your timed queries in this forum.
Show Slide

Fossee Forum

Please post your general queries on Python in this forum.
Show Slide

Textbook Companion

FOSSEE team coordinates the TBC project.
Show Slide

Acknowledgment

http://spoken-tutorial.org

Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

Previous slide This is Usha from IIT Bombay (or FOSSEE, if you wish) signing off.

Thank you.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Vineeta