Difference between revisions of "Python-3.4.3/C2/Additional-features-of-IPython/English"
(Created page with "'''Additional-features-of-IPython - English''' Title: Additional-features-of-'''IPython''' – English Author: '''Usha Viswanathan, Thirumalesh H S''' Keywords: '''Video t...") |
|||
Line 27: | Line 27: | ||
# Retrieve your''' IPython''' '''history'''. | # Retrieve your''' IPython''' '''history'''. | ||
# View a part of the '''history'''. | # View a part of the '''history'''. | ||
− | # Save a part of | + | # Save a part of a '''history''' to a file. |
# Run a script from within''' IPython'''. | # Run a script from within''' IPython'''. | ||
Line 83: | Line 83: | ||
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''''' | + | | 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.''' |
|- | |- | ||
Line 92: | Line 92: | ||
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 | + | | 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 114: | Line 114: | ||
− | | 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 | + | | 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'''.''' | ||
|- | |- | ||
Line 138: | Line 140: | ||
Type | Type | ||
− | '''xlabel | + | '''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.''' |
− | ''' | + | '''Title inside the brackets inside inverted commas inside dollar sign x and xsin(x) '''press '''Enter.''' |
− | We see the labelled '''plot'''. | + | We can now see the labelled '''plot'''. |
|- | |- | ||
Line 181: | Line 183: | ||
− | Type '''%history 5 '''and press''' Enter'''. This displays the fifth '''command''' which we typed. | + | Type '''%history space 5 '''and press''' Enter'''. This displays the fifth '''command''' which we typed. |
|- | |- | ||
Line 219: | Line 221: | ||
− | We can see, '''%history | + | We can see, '''%history hypen n 4 hypen 6''' displays the '''commands''' from 4 to 6. |
− | Here ''' | + | Here hypen '''n '''is optional '''argument''' which prints line numbers. |
Line 227: | Line 229: | ||
− | Now type'' '''''%history | + | Now type'' '''''%history space 5hypen 10''' and press''' Enter.''' |
|- | |- | ||
Line 264: | Line 266: | ||
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 plot_script.py 2 4 | + | '''%save space plot_script.py 2 space 4 hypen 7'''. press''' Enter''' |
|- | |- | ||
Line 272: | Line 274: | ||
| 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 '''%save command''' is the name of the file in which the commands are saved. | ||
− | The second argument gives the | + | The second argument gives the numbers of the commands that are being saved separated by spaces. |
|- | |- | ||
Line 295: | Line 297: | ||
− | Type'' '''''%run | + | Type'' '''''%run space hypen i space plot_script.py''' and press''' Enter.''' |
− | Here, ''' | + | Here, '''hypen i''' parameter runs the code written in a text editor. |
Line 354: | Line 356: | ||
− | We first look at the '''history''' using''' %history | + | We first look at the '''history''' using''' %history hypen n''' command. |
− | Type'' '''''%history | + | Type'' '''''%history hypen n''' and press''' Enter.''' |
|- | |- | ||
Line 365: | Line 367: | ||
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;"| | + | | style="background-color:#ffffff;border:1pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0125in;padding-right:0.075in;"| Let us clear the plot window |
− | Type '''clf()''' | + | Type '''clf() and '''press Enter |
|- | |- | ||
Line 381: | Line 383: | ||
− | | 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''' | + | | 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''' |
We need lines 2, 4 to 7 and 16 | We need lines 2, 4 to 7 and 16 | ||
Line 387: | Line 389: | ||
Type | Type | ||
− | '''%save show_included.py 2 4 | + | '''%save show_included.py 2 space 4 hypen 7 space 16''' and press''' Enter''' |
|- | |- | ||
Line 401: | Line 403: | ||
− | '''%run | + | '''%run hypen i show_included.py''' |
Line 417: | Line 419: | ||
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 ‘ | + | | 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, |
'''%run show_included.py''' | '''%run show_included.py''' |
Revision as of 16:11, 15 February 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,
|
|
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,
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisites |
To practice this tutorial, you should know how to
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.
|
[IPython console]
%pylab and press Enter. |
Let us initialise the pylab package.
|
[IPython Terminal]
press Enter |
To start plotting, type x equals to linspace inside the brackets minus 2*pi comma 2*pi comma 100 then press Enter. |
[IPython Terminal]
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".
|
[IPython Terminal]
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]
|
Next let us add title and the labels for both x and y axes.
xlabel inside the brackets inside inverted commas inside dollar sign x press Enter.
|
[IPython Terminal]
Type %history and press Enter |
The history of typed commands can be retrieved by %history command.
|
%history itself is a command and is displayed as the most recent command.
| |
[IPython Terminal]
%history 5 press Enter |
If we want to see what was the fifth command, pass 5 as an argument to %history command.
|
Show Slide
Assignment 1 |
Now pause the video here, try out the following exercise and resume the video.
|
Switch back to terminal. | |
[IPython terminal]
Example >> %history -n 4-6 Now quit history doc and type %history 5-10 and press Enter |
Let us look at the solution.
Here hypen n is optional argument which prints line numbers.
|
To save the history, we use %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.
|
[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.
|
[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.
%save space plot_script.py 2 space 4 hypen 7. press Enter |
[IPython Terminal]
Highlight the typed command on the terminal. |
The first argument in %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]
|
Let us now open the file plot_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.
|
[IPython Terminal]
Type show() and press Enter
|
The script runs but we do not see the plot.
we are not in the interactive mode.
|
Show Slide
Assignment 2
|
Pause the video here, try out the following exercise and resume the video.
|
[IPython Terminal]
%history -n press Enter
|
Let us look at the solution.
Type %history hypen n and press Enter. |
[IPython Terminal]
press Enter |
Let us clear the plot window
|
[IPython Terminal]
Type %save show_included.py 1 3-7 12 15 Press Enter
|
Now let us save the script using the command %save
We need lines 2, 4 to 7 and 16 Type %save show_included.py 2 space 4 hypen 7 space 16 and press Enter |
[IPython Terminal]
Type
Press Enter |
To run the script type
|
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
|
Go to the previous command. Modify it by removing ‘hypen i’ in the command to,
%run show_included.py
|
Show Slide
Summary |
This brings us to the end of this tutorial. In this tutorial,we have learnt to,
|
Show Slide
Summary |
# Save the required lines of code in required order using '%save' command.
|
Show Slide
Self assessment 1 |
* How do you save the command lines 2 3 4 5 7 9 10 11
|
Show Slide
Self assessment 2 |
* Which is the command to run the script?
|
Show Slide
Solution for the self assessment questions |
And the answers,
|
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 |
Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
Previous slide | This is _________ from IIT Bombay (or FOSSEE, if you wish) signing off.
Thank you. |