Difference between revisions of "Python-3.4.3/C2/Getting-started-with-IPython/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Python/C2/Getting-started-with-IPython/English)
 
(2 intermediate revisions by 2 users not shown)
Line 28: Line 28:
 
# invoke the '''IPython interpreter'''.  
 
# invoke the '''IPython interpreter'''.  
 
# quit the '''IPython interpreter'''.  
 
# quit the '''IPython interpreter'''.  
# navigate the '''IPython''' session history.  
+
# navigate the '''IPython session''' history.  
 
# use '''tab-completion''' within '''IPython'''.  
 
# use '''tab-completion''' within '''IPython'''.  
# look-up documentation in '''IPython'''.  
+
# look up documentation in '''IPython'''.  
 
# interrupt incomplete or incorrect '''commands'''.  
 
# interrupt incomplete or incorrect '''commands'''.  
 
 
  
 
|-
 
|-
Line 44: Line 42:
 
* '''Python 3.5.2'''
 
* '''Python 3.5.2'''
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 53: Line 49:
 
| style="border:0.5pt solid #000000;padding:0.0417in;"| What is '''IPython'''?
 
| style="border:0.5pt solid #000000;padding:0.0417in;"| What is '''IPython'''?
  
* '''IPython''' is an enhanced '''interactive''' '''Python interpreter.'''  
+
* '''IPython''' is an enhanced '''interactive Python interpreter.'''  
 
* It provides features like '''tab-completion''', and easier access to help.  
 
* It provides features like '''tab-completion''', and easier access to help.  
 
 
  
 
|-
 
|-
Line 70: Line 64:
 
Type '''ipython3''' at the '''prompt''' and press '''Enter'''.
 
Type '''ipython3''' at the '''prompt''' and press '''Enter'''.
  
If '''IPython''' is not installed, please refer to the instructions sheet.
+
If '''IPython''' is not installed, please refer to the '''Instruction sheet'''.
  
 
|-
 
|-
Line 84: Line 78:
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| [IPython Terminal]
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| [IPython Terminal]
  
Point to '''In[1]''': prompt with mouse
 
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| Some additional helpful information is printed by '''IPython.'''
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| Some additional helpful information is printed by '''IPython.'''
  
 +
|-
 +
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| Point to '''In[1]''': prompt with mouse
  
After this, we get a prompt with '''In[1]''':
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| After this, we get a prompt with '''i n bracket 1''':
 
+
 
+
 
+
  
 
|-
 
|-
Line 97: Line 89:
  
 
Type '''Ctrl+D'''
 
Type '''Ctrl+D'''
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Now, Let us see how we can quit the '''IPython interpreter'''.  
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Now, let us see how we can quit the '''IPython interpreter'''.  
  
  
Line 144: Line 136:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| [IPython Terminal]
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| [IPython Terminal]
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Now, Let us learn how to use the interpreter.  
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Now, let us learn how to use the interpreter.  
  
  
Line 161: Line 153:
  
  
We press '''Enter''' to execute the '''python''' '''command'''. Please do so after typing every '''command'''.
+
We press '''Enter''' to execute the '''python command'''. Please do so after typing every '''command'''.
  
  
Line 186: Line 178:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| IPython Terminal
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| IPython Terminal
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Let us see how we can navigate to previous commands in '''IPython'''.
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Let us see how we can navigate to previous commands in '''IPython'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| [IPython Terminal]
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| [IPython Terminal]
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| For example, say, we want to execute '''print (1 plus 2).'''
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| For example, say, we want to execute '''print open parenthesis 1 plus 2 close parenthesis.'''
  
  
Instead of typing the whole '''command''', we can recall the '''command''' '''1 plus 2 '''we typed earlier.
+
Instead of typing the whole '''command''', we can recall the '''command 1 plus 2 '''we typed earlier.
  
 
|-
 
|-
Line 206: Line 198:
 
Use left arrow to navigate to start of line  
 
Use left arrow to navigate to start of line  
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Now use the left-arrow key to navigate to the beginning of the line.
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Now use the left-arrow key to navigate to the beginning of the line.
 
 
 
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| [IPython Terminal]
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| [IPython Terminal]
  
Type '''print open parenthesis close parenthesis '''and press '''Enter'''.  
+
Type '''print ('''>> press '''space) '''and press '''Enter'''.  
  
  
  
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Type the word '''print ('''and press '''space key) '''on the keyboard.  
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Type the word '''print open parenthesis close parenthesis '''on the keyboard.  
  
  
 
We have changed the '''command''' to '''print (1 plus 2). '''Now press '''Enter'''.  
 
We have changed the '''command''' to '''print (1 plus 2). '''Now press '''Enter'''.  
 
 
 
  
 
|-
 
|-
Line 252: Line 238:
  
  
As with any programming language, '''asterix '''is used for multiplication operation.
+
As with any programming language, '''asterix '''is used for '''multiplication operator'''.
  
 
|-
 
|-
Line 258: Line 244:
  
 
Highlight output
 
Highlight output
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Observe the output on the console.
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Observe the output on the '''console'''.
  
 
<<PAUSE>>
 
<<PAUSE>>
Line 264: Line 250:
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| [IPython Terminal]
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| [IPython Terminal]
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Now, Let us see, what is '''tab-completion'''.  
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Now, let us see, what is '''tab-completion'''.  
  
  
Line 276: Line 262:
  
  
For this we just type '''pri''' at the prompt and press the '''tab''' '''key'''.  
+
For this we just type '''pri''' at the '''prompt''' and press the '''tab key'''.  
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| [IPython Terminal]
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| [IPython Terminal]
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| As you can see on the console that '''IPython''' has autocompleted the '''command''' '''pri''' to '''print'''.
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| As you can see on the '''console''' that '''IPython''' has autocompleted the '''command''' '''pri''' to '''print'''.
  
  
Line 302: Line 288:
  
  
Therefore, it just lists out all the possible '''tab-completion''' of '''p'''.  
+
Therefore, it just lists out all the possible '''tab-completions''' of '''p'''.  
  
 
|-
 
|-
Line 308: Line 294:
  
 
Exercise 1
 
Exercise 1
| style="border:0.5pt solid #000000;padding:0.0417in;"| Now Let us try out an exercise.  
+
| style="border:0.5pt solid #000000;padding:0.0417in;"| Now let us try out an exercise.  
  
  
Line 315: Line 301:
 
# find out the '''commands''' starting with "'''ab'''".
 
# find out the '''commands''' starting with "'''ab'''".
 
# list out the '''commands''' starting with "'''a'''".
 
# list out the '''commands''' starting with "'''a'''".
 
 
  
 
|-
 
|-
Line 325: Line 309:
  
 
'''a'''<tab>
 
'''a'''<tab>
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| '''ab''' tab autocompletes to '''abs'''.
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| '''ab tab''' autocompletes to '''a b s  abs'''.
 +
  
 
+
'''a tab''' displays a list of all the '''commands''' starting with '''a'''.  
'''a'''<tab> displays a list of all the '''command'''s starting with '''a'''.  
+
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| [IPython Terminal]
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| [IPython Terminal]
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Now, Let us see what the '''function abs '''is used for.  
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Now, let us see what the '''function abs '''is used for.  
  
  
Line 342: Line 326:
  
  
'''IPython interpreter '''will show the documentation for the '''function'''.
+
The '''IPython interpreter '''will show the documentation for the '''function'''.  
 
+
 
+
Let us see the documentation of the function '''abs'''.
+
  
 
|-
 
|-
Line 352: Line 333:
 
Type '''abs'''? >> press '''Enter'''
 
Type '''abs'''? >> press '''Enter'''
  
 
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| From the displayed information, it says '''abs''' accepts a number as input and returns it's absolute value.  
 
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Type '''abs'''? and press '''Enter'''. From the displayed information, it says '''abs''' accepts a number as input and returns it's absolute value.  
+
  
 
|-
 
|-
Line 362: Line 341:
  
 
'''abs(19) '''
 
'''abs(19) '''
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Let us see few examples,
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Let us see a few examples.
  
  
On the '''console''', type '''abs(-19)''' and then '''abs(19)'''.
+
On the '''console''', type '''a b s minus 19''' and then '''a b s 19'''.
  
  
Line 374: Line 353:
  
 
'''abs(-10.5) '''
 
'''abs(-10.5) '''
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Now Let us try it for decimal numbers.
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| Now let us try it for decimal numbers.
  
  
Let us try '''abs(-10.5)'''.
+
Let us try '''a b s minus 10.5'''.
  
  
Line 405: Line 384:
  
  
You can look up the documentation of the '''function round''' by typing '''round''' '''question mark'''.  
+
You can look up the documentation of the '''function round''' by typing '''round question mark'''.  
  
  
It says here that the '''function round''', '''rounds''' a number to a given '''precision'''.  
+
It says here that the '''function round, rounds''' a number to a given '''precision'''.  
  
 
|-
 
|-
Line 414: Line 393:
  
 
Highlight the syntax of '''ndigits '''
 
Highlight the syntax of '''ndigits '''
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| '''ndigits '''is the precision value for '''round function'''.''' '''
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| '''ndigits '''is the '''precision''' value for '''round function'''.
  
  
Line 436: Line 415:
 
Check the output of  
 
Check the output of  
  
'''round(2.48) round(2.48, 1)round(2.484) round(2.484, 2) '''
+
'''round 2.48 round 2.48 comma 1 round 2.484 round 2.484 comma 2 '''
  
 
|-
 
|-
Line 444: Line 423:
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| We get  
 
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| We get  
  
 
+
'''round 2.48 is equal to 2.0'''
2.0
+
'''round 2.48 comma 1 is 2.5'''
 
+
'''round 2.484 is 2.0'''
2.5
+
'''round 2.484 comma 2 is 2.48 '''
 
+
2.0  
+
 
+
2.48  
+
  
  
Line 469: Line 444:
  
  
Type '''round(2.484''' and press '''Enter''', without closing the parenthesis.  
+
Type '''round open parenthesis 2.484''' and press '''Enter''', without closing the '''parenthesis'''.  
 
+
 
+
 
+
  
 
|-
 
|-
Line 484: Line 456:
  
  
This '''prompt '''is the continuation '''prompt '''of '''IPython'''.  
+
This '''prompt '''is the '''continuation prompt '''of '''IPython'''.  
  
  
It appears when, the previous line is incomplete.  
+
It appears when the previous line is incomplete.  
  
 
|-
 
|-
Line 494: Line 466:
  
  
We get the expected output, that is 2.0
+
We get the expected output, that is 2.
  
 
|-
 
|-
Line 507: Line 479:
  
  
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| What if we type an incorrect '''command '''and end up with the continuation '''prompt'''?
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding-top:0in;padding-bottom:0.0417in;padding-left:0.0417in;padding-right:0.0417in;"| What if we type an incorrect '''command '''and end up with the '''continuation prompt'''?
  
  
Line 516: Line 488:
  
  
Do not close the paranthesis and press''' Enter. '''
+
Do not close the '''parenthesis''' and press''' Enter. '''
  
  
We gave alpha-numeric value ''''1a'''' as input.  
+
We gave alpha-numeric value ''''1 a'''' as input.  
  
  
Press''' Ctrl+C''' to iterrupt the execution.
+
Press''' Ctrl+C''' to interrupt the execution.
  
 
|-
 
|-
Line 533: Line 505:
 
Try out the following exercise and resume the video.  
 
Try out the following exercise and resume the video.  
  
# Type '''round(2.484,''' and press '''Enter'''.  
+
# Type '''round 2.484,''' without closing the '''parenthesis''' and press '''Enter'''.  
 
# Then cancel the '''command''' using '''Ctrl+C'''.  
 
# Then cancel the '''command''' using '''Ctrl+C'''.  
# Type the '''command''', '''round(2.484, 2) '''
+
# Type the '''command round 2.484 comma 2 '''
 
+
 
+
  
 
|-
 
|-
Line 562: Line 532:
 
In this tutorial,we have learnt to,  
 
In this tutorial,we have learnt to,  
  
# invoke the '''IPython interpreter '''by typing ip'''ython in the terminal'''.  
+
# invoke the '''IPython interpreter '''by typing '''ipython''' in the '''terminal'''.  
 
# quit the '''IPython interpreter '''by using '''Ctrl+D'''.  
 
# quit the '''IPython interpreter '''by using '''Ctrl+D'''.  
# navigate '''IPython''' session history by using the arrow keys.  
+
# navigate the '''IPython session''' history by using the arrow keys.  
 
# use the '''tab-completion''' to work faster.  
 
# use the '''tab-completion''' to work faster.  
 
# see the documentation of '''functions '''using '''question mark'''.  
 
# see the documentation of '''functions '''using '''question mark'''.  
 
# interrupt commands using '''Ctrl+C''' when we make an error.  
 
# interrupt commands using '''Ctrl+C''' when we make an error.  
 
 
  
 
|-
 
|-
Line 578: Line 546:
  
  
| style="border:0.5pt solid #000000;padding:0.0417in;"| Here are some self assessment questions for you to solve  
+
| style="border:0.5pt solid #000000;padding:0.0417in;"| Here are some self assessment questions for you to solve.
  
  
# '''IPython''' '''is a programming language similar to Python.'''True or False <br/>  
+
# '''IPython is a programming language similar to Python.'''True or False <br/>  
 
+
 
# Which key combination quits '''IPython'''?  
 
# Which key combination quits '''IPython'''?  
 
+
#* '''Ctrl + C'''
*
+
#* '''Ctrl + D'''  
** '''Ctrl + C'''
+
#* '''Alt + C'''
** '''Ctrl + D'''  
+
#* '''Alt + D''' <br/>
** '''Alt + C'''
+
# Which character is used at the end of a '''command''', in '''IPython''' to display the documentation?
** '''Alt + D''' <br/>  
+
#* '''under score '''  
 
+
#* '''question mark '''  
 
+
#* '''exclamation mark '''  
3. Which character is used at the end of a '''command''', in '''IPython''' to display the documentation?
+
#* '''ampersand ''' <br/>  
 
+
*
+
** '''under score (_)'''  
+
** '''question mark (?)'''  
+
** '''exclamation mark (!)'''  
+
** '''ampersand (&)''' <br/>  
+
 
+
 
+
 
+
  
 
|-
 
|-
Line 612: Line 570:
 
| style="border:0.5pt solid #000000;padding:0.0417in;"| And the answers are-  
 
| style="border:0.5pt solid #000000;padding:0.0417in;"| And the answers are-  
  
 
+
# '''False. IPython''' is not a new programming language. It is just an enhanced interactive '''Python interpreter'''.  
# '''False. IPython''' is not a new programming language. It is just an enhanced interactive '''Python''' '''interpreter'''.  
+
 
# We use '''Ctrl + D''' to quit '''IPython interpreter'''.  
 
# We use '''Ctrl + D''' to quit '''IPython interpreter'''.  
# We use '''? '''at the end of the command to display its documentation.  
+
# We use '''? '''at the end of the '''command''' to display its documentation.  
 
+
 
+
  
 
|-
 
|-
Line 659: Line 614:
  
 
Forum to answer questions
 
Forum to answer questions
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| Do you have any general / technical questions on Python?
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| Do you have any general / technical questions in '''Python'''?
  
Please visit the FOSSEE forum and post your question.
+
Please visit the '''FOSSEE forum''' and post your question.
  
 
|-
 
|-
Line 667: Line 622:
  
 
Textbook Companion  
 
Textbook Companion  
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| The '''FOSSEE '''team coordinates coding of solved examples of popular books.  
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| The '''FOSSEE '''team coordinates coding of several solved examples of popular books.  
  
 
We give honorarium and certificates for those who do this.  
 
We give honorarium and certificates for those who do this.  
Line 683: Line 638:
  
 
Thank You
 
Thank You
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| This is '''__________''' from IIT Bombay signing off. Thanks for watching
+
| style="border-top:none;border-bottom:0.5pt solid #000000;border-left:0.5pt solid #000000;border-right:0.5pt solid #000000;padding:0.0417in;"| This is Prabhu from IIT Bombay signing off. Thanks for watching
  
 
|}
 
|}

Latest revision as of 07:37, 10 February 2017

Python/C2/Getting-started-with-IPython/English

Title of script: Getting started with IPython

Author: Puneeth, Jovina, Thirumalesh H S

Keywords: Python, IPython


Visual Cue
Narration
Show Slide

[Slide with MHRD logo]

Hello Friends. Welcome to the spoken tutorial on "getting started with IPython".
Show Slide

Objectives


At the end of this tutorial, you will be able to,
  1. invoke the IPython interpreter.
  2. quit the IPython interpreter.
  3. navigate the IPython session history.
  4. use tab-completion within IPython.
  5. look up documentation in IPython.
  6. interrupt incomplete or incorrect commands.
Show Slide

System Specifications

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

What is IPython

What is IPython?
  • IPython is an enhanced interactive Python interpreter.
  • It provides features like tab-completion, and easier access to help.
[Terminal]

Type ipython >> press Enter

Let us first see how to start the IPython interpreter.


First open the terminal by pressing Ctrl+Alt+T keys simultaneously on the keyboard.


Type ipython3 at the prompt and press Enter.

If IPython is not installed, please refer to the Instruction sheet.

[IPython Terminal]

Point to the version information with mouse.

If IPython is installed, the IPython interpreter is loaded by running the ipython command in the terminal


The versions of Python and IPython that are installed, are shown on the terminal.

[IPython Terminal] Some additional helpful information is printed by IPython.
Point to In[1]: prompt with mouse After this, we get a prompt with i n bracket 1:
[IPython Terminal]

Type Ctrl+D

Now, let us see how we can quit the IPython interpreter.


Press Ctrl+D keys.

]IPython Terminal]


A prompt will appear to confirm if we really want to exit.


Type y for yes and quit IPython.


Note that y is given in square brackets so it is default.

We could also press Enter and it will exit.


Else type n for no if you don't want to quit IPython.

[IPython Terminal]


Type y >> press Enter.


Let us type y.


We have quit IPython interpreter and are back at the terminal prompt.

[Terminal]

Type ipython >> press Enter

Let us start it again.


Type ipython3 in the terminal and press Enter.

[IPython Terminal] Now, let us learn how to use the interpreter.


Let us start with the simplest operation - addition.

[IPython Terminal]

1+2

Point at the Out[1] prompt


Type 1 plus 2 at the IPython prompt and press Enter.


We press Enter to execute the python command. Please do so after typing every command.


IPython promptly displays the output as 3.


Notice that the output is shown with an Out square brackets 1 indication.

[IPython Terminal]

5 – 3

7 * 4

Let us now try a few more operations such as,

5 minus 3,

7 multiplied by 4,


Each time we press Enter and see the output on the IPython console window.

IPython Terminal Let us see how we can navigate to previous commands in IPython
[IPython Terminal] For example, say, we want to execute print open parenthesis 1 plus 2 close parenthesis.


Instead of typing the whole command, we can recall the command 1 plus 2 we typed earlier.

[IPython Terminal]

Use the up arrow key to go back to the command 1+2.

Use the up arrow key to go back to the command 1 plus 2.
[IPython Terminal]

Use left arrow to navigate to start of line

Now use the left-arrow key to navigate to the beginning of the line.
[IPython Terminal]

Type print (>> press space) and press Enter.


Type the word print open parenthesis close parenthesis on the keyboard.


We have changed the command to print (1 plus 2). Now press Enter.

[IPython Terminal]

Highlight the result line.


Point at the Output

The interpreter prints the result as 3.


Notice that this time, the indication Out square brackets is not displayed.

[IPython Terminal]

Press up-arrow key

Now let us execute print 10 multiplied by 2.

We use the up arrow key to navigate to the previous command print (1 plus 2).

[IPython Terminal]

Change print (1+2) to print (10*2)

Now replace 1 plus 2 with 10 multiplied by 2 and press Enter.


As with any programming language, asterix is used for multiplication operator.

[IPython Terminal]

Highlight output

Observe the output on the console.

<<PAUSE>>

[IPython Terminal] Now, let us see, what is tab-completion.


Let us consider an example.

[IPython Terminal]

pri<tab>

Suppose we want to use the function print.


For this we just type pri at the prompt and press the tab key.

[IPython Terminal] As you can see on the console that IPython has autocompleted the command pri to print.


This feature of IPython is called the tab-completion.

[IPython Terminal]

p<tab>

Let us see some more possibilities of tab completion.


Just type p and then press the tab.

[IPython Terminal] In this case, we see that IPython does not complete the command.


This is because, there are more than one command beginning with p.


Therefore, it just lists out all the possible tab-completions of p.

Show Slide:

Exercise 1

Now let us try out an exercise.


Pause the video, solve the problem and resume the video.

  1. find out the commands starting with "ab".
  2. list out the commands starting with "a".
[IPython Terminal]


ab<tab>

a<tab>

ab tab autocompletes to a b s abs.


a tab displays a list of all the commands starting with a.

[IPython Terminal] Now, let us see what the function abs is used for.


We will use the help feature of IPython to find out this.

[IPython Terminal] To see the documentation of a function, type the function name followed by a question mark.


The IPython interpreter will show the documentation for the function.

[IPython Terminal]

Type abs? >> press Enter

From the displayed information, it says abs accepts a number as input and returns it's absolute value.
[Ipython Terminal]

abs(-19)

abs(19)

Let us see a few examples.


On the console, type a b s minus 19 and then a b s 19.


We get 19, as expected, in both the cases.

[Ipython Terminal]

abs(-10.5)

Now let us try it for decimal numbers.


Let us try a b s minus 10.5.


We get 10.5 as the result.

Show Slide

Exercise 2

round?

Pause the video here.

Try out the following exercise and resume the video.


Look-up the documentation of round and see how to use it.

[IPython Terminal]

Type round?


Highlight the definition of round

Switch to the console for the solution.


You can look up the documentation of the function round by typing round question mark.


It says here that the function round, rounds a number to a given precision.

[IPython Terminal]

Highlight the syntax of ndigits

ndigits is the precision value for round function.


Notice, there are extra square brackets around ndigits.


This means that ndigits is optional and 0 is the default value.


Optional parameters are shown in square brackets in Python documentation.

Show Slide

Exercise 3

Pause the video here.


Try out the following exercise and resume the video.

Check the output of

round 2.48 round 2.48 comma 1 round 2.484 round 2.484 comma 2

Show Slide

Solution 3

We get

round 2.48 is equal to 2.0 round 2.48 comma 1 is 2.5 round 2.484 is 2.0 round 2.484 comma 2 is 2.48


which are what we expect.

[IPython Terminal]


round(2.484

Press Enter

Let us now see how to correct typing errors, which we could make while typing on the console.


Let us make a typing error deliberately.


Type round open parenthesis 2.484 and press Enter, without closing the parenthesis.

[IPython Terminal]

Point at the prompt with three dots


We get a prompt with dots.


This prompt is the continuation prompt of IPython.


It appears when the previous line is incomplete.

Type ) >> press Enter Now complete the command with close parenthesis and press Enter.


We get the expected output, that is 2.

[IPython Terminal]


round(1a >> press Enter


press Ctrl+C


What if we type an incorrect command and end up with the continuation prompt?


In such case, we can press Ctrl+C keys, to interrupt the command and get back to the IPython prompt.


E.g, round takes only numbers as input. Type round(1a


Do not close the parenthesis and press Enter.


We gave alpha-numeric value '1 a' as input.


Press Ctrl+C to interrupt the execution.

Show Slide

Exercise 4

Pause the video here.


Try out the following exercise and resume the video.

  1. Type round 2.484, without closing the parenthesis and press Enter.
  2. Then cancel the command using Ctrl+C.
  3. Type the command round 2.484 comma 2
[IPython Terminal]

round(2.484

Ctrl+C

round(2.484, 2)

The output on your console should look like this.
Show Slide


Summary


This brings us to the end of this tutorial.


In this tutorial,we have learnt to,

  1. invoke the IPython interpreter by typing ipython in the terminal.
  2. quit the IPython interpreter by using Ctrl+D.
  3. navigate the IPython session history by using the arrow keys.
  4. use the tab-completion to work faster.
  5. see the documentation of functions using question mark.
  6. interrupt commands using Ctrl+C when we make an error.
Show Slide

Assignment


Here are some self assessment questions for you to solve.


  1. IPython is a programming language similar to Python.True or False
  2. Which key combination quits IPython?
    • Ctrl + C
    • Ctrl + D
    • Alt + C
    • Alt + D
  3. Which character is used at the end of a command, in IPython to display the documentation?
    • under score
    • question mark
    • exclamation mark
    • ampersand
Show Slide

Solutions


And the answers are-
  1. False. IPython is not a new programming language. It is just an enhanced interactive Python interpreter.
  2. We use Ctrl + D to quit IPython interpreter.
  3. We use ? at the end of the command to display its documentation.
Show Slide

About the Spoken Tutorial Project


This video summarises the Spoken Tutorial project.


If you do not have good bandwidth, you may download and watch it.

Show Slide

Spoken tutorial workshops

We conduct workshops using Spoken Tutorials.

Give Certificates.

Please contact us.

Show Slide

Forum to answer questions

Do you have questions in THIS Spoken Tutorial?

Choose the minute and second where you have the question.

Explain your question briefly.

Someone from the FOSSEE team will answer them.

Please visit this site.

Show Slide

Forum to answer questions

Do you have any general / technical questions in Python?

Please visit the FOSSEE forum and post your question.

Show Slide

Textbook Companion

The FOSSEE team coordinates coding of several solved examples of popular books.

We give honorarium and certificates for those who do this.

For more details, please visit this site.

Show Slide

Acknowledgement

The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India
Show Slide

Thank You

This is Prabhu from IIT Bombay signing off. Thanks for watching

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, PoojaMoolya, Vineeta