Python/C2/Additional-features-of-IPython/English-timed

From Script | Spoken-Tutorial
Revision as of 10:52, 30 October 2013 by Krupali (Talk | contribs)

Jump to: navigation, search
Timing Narration
0:00 Hello friends and welcome to the tutorial on "Additional Features of IPython".
0:06 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 your history to a file.
  4. Run a script from within ipython.


0:19 Before beginning this tutorial,we would suggest you to complete the tutorial on "Embellishing a plot".
0:24 Let us start ipython with pylab loaded, by typing ipython space hyphen pylab on the terminal.
0:33 We shall first make a plot and then view the history and save it, for that we can type x = linspace within brackets minus 2 into pi comma 2 into pi comma 100

plot within bracket x comma xsinx cosx and hit enter


1:00 We got an error saying "xsinx is not defined".
1:04 This is because xsin(x) is actually x star sin(x), for that we have to type on the terminal

plot within bracket x comma x star sin(x)

plot x, sin(x) xlabel within bracket in double quotes x then ylabel within bracket in double quotes dollar sign f(x) again a dollar sign then title within bracket in double quotes x and xsin(x) then again title within bracket in double quotes dollar sign x and xsin(x) and again a dollar sign


2:15 We now have the plot.
2:18 Let us look at the commands that we have typed in.
2:21 The history can be retrieved by using modulo hist command.
2:28 Type modulo that is percentage sign then hist and hit enter
2:37 As you can see, it displays a list of recent commands that we typed.
2:41 Every command has a number in front, to specify in which order and when it was typed.
2:48 So, Please note that there is a percentage sign before the hist command.
2:52 This implies that percentage hist is a command that is specific to IPython only and not to any other version of python.
3:00 These other type of commands are called as magic commands.
3:04 Also note that, the =%hist= itself is a command and is displayed as the most recent command.
3:15 We should note that anything we type in is stored as history, irrespective of whether it is command or an error or IPython magic command.
3:27 If we want only the recent 5 commands to be displayed, we pass the number as an argument to =percentage hist= command.
3:35 Hence percentage hist 5 displays the recent 5 commands, inclusive of the =percentage hist= command.
3:46 The default number is 40.
3:48 So to try this we can type in the terminal percentage hist space 5 and hit enter.
3:58 Pause the video here, try out the following exercise and resume the video.
4:02 Read through the documentation of %hist and find out how to list all the commands between 5 and 10.
4:11 As we can see from =percentage hist= documentation,percentage hist 5 10 displays the commands from 5 to 10
4:22 Now that we have the history, we will try it on the command prompt so type percentage hist 5 space 10 and hit enter.
4:35 we would like to save the required line of code from history.
4:39 This is possible by using the =percentage save= command.
4:47 Before we do that, let us first look at history and identify what lines of code we require. So type percentage hist and hit enter.
4:58 The first command is linspace.
5:00 But second command is a command that gave us an error.
5:06 Hence we do not need second command.
5:09 The commands from third to sixth are required.
5:12 The seventh command although is correct, we do not need it since we are setting the title correctly in the eighth command.
5:20 So we need first third to sixth and the eighth command for our program.
5:26 Hence you can type in the syntax of percentage save slash home slash fossee slash plot underscore script dot py space 1 space 3 hyphen 6 space 8
5:46 The command saves the first line of code and then third to sixth followed by the eighth lines of code into the specified file.
5:55 The first argument to percentage save is the path of file to save the commands and the arguments there after are the commands to be saved in the given order.
6:08 Pause the video here, try out the following exercise and resume the video.
6:11 Change the label on y-axis to "y" and save the lines of code accordingly.
6:17 we use the command =ylabel= on interpreter
6:21 ylabel within brackets in double quotes y and hit enter
6:29 and then you can do percentage save command
6:36 That is you can type percentage save slash home slash fossee slash example underscore plot dot py space 1 space 3 hyphen 6 space 10
6:51 Now that we have the required lines of code in a file, let us learn how to run the file as a python script.
6:58 We use the IPython magic command =percentage run= to do this.
7:03 Type percentage run space hyphen i space slash home slash fossee slash plot underscore script dot py
7:18 The script runs but we do not see the plot. This happens because when we are running a script, we are not in the interactive mode anymore.
7:27 Hence to view the plot type show then brackets on your terminal
7:38 So now Pause the video here, try out the following exercise and resume the video.
7:41 Use percentage hist and percentage save and create a script that has the function show() in it.
7:49 Run the script to produce the plot and display the name.
7:56 So you can type percentage hist space 20 on the terminal
8:06 We first look at the history using this command only,
8:09 Then save the script using the command percentage save space slash home slash fossee slash show underscore included dot py space 1 space 3 hyphen 6 space 8 , 14 and 17
8:37 then on the next line you can type

percentage run hyphen i slash home slash fossee slash show underscore included dot py, then type show function to get the plot.

9:03 We get the desired plot now.
9:07 The reason for including a hyphen i after run is to tell the interpreter that if any name is not found in script, search for it in the interpreter.
9:17 Hence all these sin, plot, pi and show which are not available in script, are taken from the interpreter and used to run the script.
9:32 Pause the video here, try out the following exercise and resume the video.
9:36 Run the script without using the hyphen i option.
9:42 Do you find any difference?
9:47 So you can type percentage run slash home slash fossee slash included hypen included dot py
10:04 We see that it raises Name Error saying that the name linspace is not found.
10:12 This brings us to the end of this tutorial.
10:14 In this tutorial,we have learnt to, Retrieve the history using =percentage hist= command.
10:21 then View only a part of history by passing an argument to 'percentage hist' command.
10:25 then Save the required lines of code in required order using 'percentage save' command.
10:30 then Use 'percentage run hyphen i' command to run the saved script.


10;39 Here are some self assessment questions for you to solve
10:43 5. How do you retrieve the recent 5 commands
10:46 And there are four options percentage hist,percentage hist minus 5,percentage hist 5,percentage hist 5 to 10
10:57 How do you save the lines 2 3 4 5 7 9 10 11 and the options are percentage save filepath 2 hyphen 5 7 9 hyphen 11,percentage save filepath 2 hyphen 11,percentage save filepath percentage save 2 hyphen 5 7 9 10 11
11:24 And the final question is What will the command percentage hist 5 10 display.
11:30 The recently typed commands from 5 to 10 inclusive of the history command
11:34 The recently typed commands from 5 to 10 excluding the history command
11:38 So now the answers,
11:41 In order to retrieve the recently typed 5 commands,we say percentage hist 5 for the first question.
11:49 So second question answer is percentage save file path 2 hyphen 5 space 7 space 9 hyphen 11 is the correct option to the specified lines of codes.
12:02 Then third answer is percentage hist 5 space 10 will display the recently typed commands from 5 to 10 inclusive of the history command.
12:14 This brings us to the end of this tutorial
12:17 So we Hope you have enjoyed and found it useful.Thank you.

Contributors and Content Editors

Gaurav, Kavita salve, Krupali, Minal, PoojaMoolya, Sneha