Difference between revisions of "Python/C3/Getting-started-with-for/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 1: Line 1:
 
{| border=1
 
{| border=1
!Timing
+
|'''Time'''
!Narration
+
|'''Narration'''
 
|-
 
|-
| 0:00
+
| 00:00
 
| Hello and welcome to the tutorial on ''Getting started with ``for`'' loop`.
 
| Hello and welcome to the tutorial on ''Getting started with ``for`'' loop`.
  
 
|-
 
|-
| 0:07
+
| 00:07
 
| At the end of this tutorial, you will be able to,
 
| At the end of this tutorial, you will be able to,
  
Line 15: Line 15:
 
# Write blocks in python interpreter
 
# Write blocks in python interpreter
 
# Write blocks in ipython interpreter.
 
# Write blocks in ipython interpreter.
 
  
 
|-
 
|-
| 0:25
+
| 00:25
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with Lists".
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with Lists".
  
 
|-
 
|-
|0:31
+
|00:31
 
|In Python white space is significant, and the blocks are visually separated.  
 
|In Python white space is significant, and the blocks are visually separated.  
  
 
|-
 
|-
|0:38
+
|00:38
 
|The best practice is to indent the code using four spaces.
 
|The best practice is to indent the code using four spaces.
  
 
|-
 
|-
|0:41
+
|00:41
 
|As you can see in the slide, "Block B" is an inner block, indented by 4 spaces.
 
|As you can see in the slide, "Block B" is an inner block, indented by 4 spaces.
  
 
|-
 
|-
|0:48
+
|00:48
 
| After "Block B" the next statement in "Block A" starts from the same indentation level of other "Block A" Statements.
 
| After "Block B" the next statement in "Block A" starts from the same indentation level of other "Block A" Statements.
  
 
|-
 
|-
|0:58
+
|00:58
 
| Start the ipython interpreter using ipython hyphen pylab.
 
| Start the ipython interpreter using ipython hyphen pylab.
  
 
|-
 
|-
| 1:08
+
| 01:08
 
| Now let us move straight into  for  loop.
 
| Now let us move straight into  for  loop.
  
 
|-
 
|-
| 1:11
+
| 01:11
 
| Write a for loop which iterates through a list of numbers and find the square root of each number. numbers are 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916
 
| Write a for loop which iterates through a list of numbers and find the square root of each number. numbers are 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916
  
 
|-
 
|-
| 1:37
+
| 01:37
 
| For the problem, first we need to create a list  of numbers and then iterate over the list and find the square root of each element in it.
 
| For the problem, first we need to create a list  of numbers and then iterate over the list and find the square root of each element in it.
  
 
|-
 
|-
| 1:45
+
| 01:45
 
| And let us create a script, rather than typing it out in the interpreter itself.
 
| And let us create a script, rather than typing it out in the interpreter itself.
  
 
|-
 
|-
| 1:50
+
| 01:50
 
| Open your text editor and type the following code shown on the slide.
 
| Open your text editor and type the following code shown on the slide.
  
 
|-
 
|-
| 1:56
+
| 01:56
 
| Now switch to your terminal and run the script .
 
| Now switch to your terminal and run the script .
  
 
|-
 
|-
|2:05
+
|02:05
 
|In the terminal run the script as  percentage run space hyphen i space list underscore roots dot py.  
 
|In the terminal run the script as  percentage run space hyphen i space list underscore roots dot py.  
  
 
|-
 
|-
2:24
+
02:24
 
| So that way easy!
 
| So that way easy!
  
 
|-
 
|-
| 2:27
+
| 02:27
 
| All what we did was iterate over the list element by element and then use the element for calculation.
 
| All what we did was iterate over the list element by element and then use the element for calculation.
  
 
|-
 
|-
| 2:36
+
| 02:36
 
| Note that here we used two variables,the variable numbers , which is a list,and the other variable each, which is the element of list under consideration in each cycle of the  for  loop.
 
| Note that here we used two variables,the variable numbers , which is a list,and the other variable each, which is the element of list under consideration in each cycle of the  for  loop.
  
 
|-
 
|-
|2:50
+
|02:50
 
| The variable names can be chosen by you.
 
| The variable names can be chosen by you.
  
 
|-
 
|-
| 2:52
+
| 02:52
 
| Note that the lines after  for  statement, is indented using four spaces.
 
| Note that the lines after  for  statement, is indented using four spaces.
  
 
|-
 
|-
| 3:01
+
| 03:01
 
| It means that line is a part of the for loop.  
 
| It means that line is a part of the for loop.  
  
 
|-
 
|-
|3:05
+
|03:05
 
|And it is a block of code, although it is only a single statement in the block.
 
|And it is a block of code, although it is only a single statement in the block.
  
 
|-
 
|-
|3:10
+
|03:10
 
| Also, the fourth line or the immediate line after the  for  block is not indented.
 
| Also, the fourth line or the immediate line after the  for  block is not indented.
  
 
|-
 
|-
| 3:18
+
| 03:18
 
| It means that it is not a part of the  for  loop and the lines after that don't fall in the scope of the  for  loop.  
 
| It means that it is not a part of the  for  loop and the lines after that don't fall in the scope of the  for  loop.  
  
 
|-
 
|-
|3:29
+
|03:29
 
|Thus each block is separated by the indentation level and that marks the importance of white-spaces in Python.
 
|Thus each block is separated by the indentation level and that marks the importance of white-spaces in Python.
  
 
|-
 
|-
| 3:35
+
| 03:35
 
| Print the square root of numbers in the list.
 
| Print the square root of numbers in the list.
  
 
|-
 
|-
|3:38
+
|03:38
 
| And this time let us do it right in the IPython interpreter.
 
| And this time let us do it right in the IPython interpreter.
  
 
|-
 
|-
|3:47
+
|03:47
 
| So let us create a list.
 
| So let us create a list.
  
 
|-
 
|-
| 3:53
+
| 03:53
 
| You will notice that, as soon as you press the enter key after for statement, the prompt changes to four dots and the cursor is not right after the four dots but there are four spaces from the dots.
 
| You will notice that, as soon as you press the enter key after for statement, the prompt changes to four dots and the cursor is not right after the four dots but there are four spaces from the dots.
  
 
|-
 
|-
|4:07
+
|04:07
 
|numbers is equal to within square brackets 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916
 
|numbers is equal to within square brackets 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916
 
Then type for each in numbers colon
 
Then type for each in numbers colon
  
 
|-
 
|-
|4:37
+
|04:37
 
|So you can see the four dots.
 
|So you can see the four dots.
  
 
|-
 
|-
| 4:43
+
| 04:43
 
| Please note that IPython automatically indents the block.
 
| Please note that IPython automatically indents the block.
  
 
|-
 
|-
|4:48
+
|04:48
 
| The four dots tell you that you are inside a block.
 
| The four dots tell you that you are inside a block.
  
 
|-
 
|-
4:53
+
04:53
 
| Now type the rest of the  for  loop,
 
| Now type the rest of the  for  loop,
  
 
|-
 
|-
|4:56
+
|04:56
 
|So type print  within double quotes Square root of, each, then print within double quotes is comma sqrt in brackets each.
 
|So type print  within double quotes Square root of, each, then print within double quotes is comma sqrt in brackets each.
  
 
|-
 
|-
|5:29
+
|05:29
 
| Now we have finished the statements in the block, and still the interpreter is showing four dots, this means that you are still inside the block.
 
| Now we have finished the statements in the block, and still the interpreter is showing four dots, this means that you are still inside the block.
  
 
|-
 
|-
|5:41
+
|05:41
 
| To exit from the block press the return key or the enter key twice without entering anything else.
 
| To exit from the block press the return key or the enter key twice without entering anything else.
  
 
|-
 
|-
| 5:50
+
| 05:50
 
| It printed the square root of each number in the list, which was executed in the  for  loop.
 
| It printed the square root of each number in the list, which was executed in the  for  loop.
  
 
|-
 
|-
| 5:57
+
| 05:57
 
| Find the cube of all the numbers from one to ten.
 
| Find the cube of all the numbers from one to ten.
  
 
|-
 
|-
|6:01
+
|06:01
 
| But this time let us try it in the vanilla version of Python interpreter.
 
| But this time let us try it in the vanilla version of Python interpreter.
  
 
|-
 
|-
| 6:07
+
| 06:07
 
| Start the vanilla version of Python interpreter by issuing the command  python in your terminal.
 
| Start the vanilla version of Python interpreter by issuing the command  python in your terminal.
  
 
|-
 
|-
|6:21
+
|06:21
 
|So type for i in range within brackets 1,11 colon and hit enter
 
|So type for i in range within brackets 1,11 colon and hit enter
  
 
|-
 
|-
| 6:38
+
| 06:38
 
| press enter once, and we will see that this time it shows four dots, but the cursor is close to the dots, so we have to indent the block.
 
| press enter once, and we will see that this time it shows four dots, but the cursor is close to the dots, so we have to indent the block.
  
 
|-
 
|-
6:52
+
06:52
 
| The vanilla version of Python interpreter does not indent the code automatically.
 
| The vanilla version of Python interpreter does not indent the code automatically.
  
 
|-
 
|-
| 6:58
+
| 06:58
 
| So enter four spaces there and then type the following
 
| So enter four spaces there and then type the following
  
 
|-
 
|-
|7:04
+
|07:04
 
|Then type print i comma within double quotes cube is comma i star star 3. Hit Enter.
 
|Then type print i comma within double quotes cube is comma i star star 3. Hit Enter.
  
 
|-
 
|-
| 7:21
+
| 07:21
 
| Now when we hit Enter, we still see the four dots.  
 
| Now when we hit Enter, we still see the four dots.  
  
 
|-
 
|-
|7:26
+
|07:26
 
|To get out of the block, hit enter once again.
 
|To get out of the block, hit enter once again.
  
 
|-
 
|-
| 7:32
+
| 07:32
 
| Okay!  
 
| Okay!  
  
 
|-
 
|-
|7:33
+
|07:33
 
|so the main thing we learnt here is how to use the Python interpreter and the IPython interpreter to specify blocks.  
 
|so the main thing we learnt here is how to use the Python interpreter and the IPython interpreter to specify blocks.  
  
 
|-
 
|-
|7:43
+
|07:43
 
|But while we were generating the multiplication table we used something new,  range  function.
 
|But while we were generating the multiplication table we used something new,  range  function.
  
 
|-
 
|-
|7:50
+
|07:50
 
|range function is an inbuilt function in Python which can be used to generate a  list  of integers from a starting number to an ending number.  
 
|range function is an inbuilt function in Python which can be used to generate a  list  of integers from a starting number to an ending number.  
  
 
|-
 
|-
|7:57
+
|07:57
 
|Note that the ending number that you specify will not be included in the list  
 
|Note that the ending number that you specify will not be included in the list  
  
 
|-
 
|-
8:03
+
08:03
 
| Print all the odd numbers from 1 to 50. Let us do it in our IPython interpreter for ease of use.
 
| Print all the odd numbers from 1 to 50. Let us do it in our IPython interpreter for ease of use.
  
 
|-
 
|-
| 8:18
+
| 08:18
 
| The problem can be solved by just using the  range()  function.
 
| The problem can be solved by just using the  range()  function.
  
 
|-
 
|-
|8:22
+
|08:22
 
|It can be solved as through command line you can type ipython and hit Enter.
 
|It can be solved as through command line you can type ipython and hit Enter.
  
 
|-
 
|-
8:28
+
08:28
 
| This time we passed three parameters to  range()  function unlike the previous case where we passed only two parameters.  
 
| This time we passed three parameters to  range()  function unlike the previous case where we passed only two parameters.  
  
 
|-
 
|-
|8:37
+
|08:37
 
|The first two parameters are same in both the cases.  
 
|The first two parameters are same in both the cases.  
  
 
|-
 
|-
|8:40
+
|08:40
 
|The first parameter is the starting number of the sequence and the second parameter is the end of the range.
 
|The first parameter is the starting number of the sequence and the second parameter is the end of the range.
  
 
|-
 
|-
|8:45
+
|08:45
 
| Note that the sequence does not include the ending number.
 
| Note that the sequence does not include the ending number.
  
 
|-
 
|-
|8:48
+
|08:48
 
| The third parameter is for stepping through the sequence.
 
| The third parameter is for stepping through the sequence.
  
 
|-
 
|-
| 8:53
+
| 08:53
 
| Here we gave two which means we are skipping every alternate element.
 
| Here we gave two which means we are skipping every alternate element.
  
 
|-
 
|-
|9:03
+
|09:03
 
|So type print range within brackets 1 comma 51 comma 2  
 
|So type print range within brackets 1 comma 51 comma 2  
  
 
|-
 
|-
| 9:07
+
| 09:07
 
| This brings us to the end of this tutorial.  
 
| This brings us to the end of this tutorial.  
  
 
|-
 
|-
|9:11
+
|09:11
 
|In this tutorial,we learnt to,
 
|In this tutorial,we learnt to,
  
 
|-
 
|-
|9:12
+
|09:12
 
| 1. create blocks in python using for  loop
 
| 1. create blocks in python using for  loop
  
 
|-
 
|-
|9:15
+
|09:15
 
| 2. indent the blocks of code
 
| 2. indent the blocks of code
  
 
|-
 
|-
|9:17
+
|09:17
 
| 3. iterate over a list using  for  loop
 
| 3. iterate over a list using  for  loop
  
 
|-
 
|-
|9:21
+
|09:21
 
| use the range() function
 
| use the range() function
  
 
|-
 
|-
|9:24
+
|09:24
 
| Here are some self assessment questions for you to solve
 
| Here are some self assessment questions for you to solve
  
 
|-
 
|-
|9:27
+
|09:27
 
| 1. Indentation is not mandatory in Python
 
| 1. Indentation is not mandatory in Python
  
 
|-
 
|-
|9:30
+
|09:30
 
| Is it true or false?
 
| Is it true or false?
  
 
|-
 
|-
|9:33
+
|09:33
 
| 2. Write a code using  for  loop to print the product of all natural numbers from 1 to 20.
 
| 2. Write a code using  for  loop to print the product of all natural numbers from 1 to 20.
  
 
|-
 
|-
|9:39
+
|09:39
 
| 3. What will be the output of-
 
| 3. What will be the output of-
  
Line 309: Line 308:
  
 
|-
 
|-
| 9:47
+
| 09:47
 
| Now, the answers,
 
| Now, the answers,
  
 
|-
 
|-
|9:48
+
|09:48
 
| 1. False. Indentation is essential in python.
 
| 1. False. Indentation is essential in python.
  
 
|-
 
|-
|9:53
+
|09:53
 
| 2. We use the  for loop in the following manner.
 
| 2. We use the  for loop in the following manner.
  
 
|-
 
|-
|9:56
+
|09:56
 
| y is equal to 1 for x in range(1,21):
 
| y is equal to 1 for x in range(1,21):
  

Revision as of 13:02, 10 July 2014

Time Narration
00:00 Hello and welcome to the tutorial on Getting started with ``for` loop`.
00:07 At the end of this tutorial, you will be able to,
  1. Write blocks of code in python using indentation.
  2. Use the for loop.
  3. Use range() function.
  4. Write blocks in python interpreter
  5. Write blocks in ipython interpreter.
00:25 Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with Lists".
00:31 In Python white space is significant, and the blocks are visually separated.
00:38 The best practice is to indent the code using four spaces.
00:41 As you can see in the slide, "Block B" is an inner block, indented by 4 spaces.
00:48 After "Block B" the next statement in "Block A" starts from the same indentation level of other "Block A" Statements.
00:58 Start the ipython interpreter using ipython hyphen pylab.
01:08 Now let us move straight into for loop.
01:11 Write a for loop which iterates through a list of numbers and find the square root of each number. numbers are 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916
01:37 For the problem, first we need to create a list of numbers and then iterate over the list and find the square root of each element in it.
01:45 And let us create a script, rather than typing it out in the interpreter itself.
01:50 Open your text editor and type the following code shown on the slide.
01:56 Now switch to your terminal and run the script .
02:05 In the terminal run the script as percentage run space hyphen i space list underscore roots dot py.
02:24 So that way easy!
02:27 All what we did was iterate over the list element by element and then use the element for calculation.
02:36 Note that here we used two variables,the variable numbers , which is a list,and the other variable each, which is the element of list under consideration in each cycle of the for loop.
02:50 The variable names can be chosen by you.
02:52 Note that the lines after for statement, is indented using four spaces.
03:01 It means that line is a part of the for loop.
03:05 And it is a block of code, although it is only a single statement in the block.
03:10 Also, the fourth line or the immediate line after the for block is not indented.
03:18 It means that it is not a part of the for loop and the lines after that don't fall in the scope of the for loop.
03:29 Thus each block is separated by the indentation level and that marks the importance of white-spaces in Python.
03:35 Print the square root of numbers in the list.
03:38 And this time let us do it right in the IPython interpreter.
03:47 So let us create a list.
03:53 You will notice that, as soon as you press the enter key after for statement, the prompt changes to four dots and the cursor is not right after the four dots but there are four spaces from the dots.
04:07 numbers is equal to within square brackets 1369, 7225, 3364, 7056, 5625, 729, 7056, 576, 2916

Then type for each in numbers colon

04:37 So you can see the four dots.
04:43 Please note that IPython automatically indents the block.
04:48 The four dots tell you that you are inside a block.
04:53 Now type the rest of the for loop,
04:56 So type print within double quotes Square root of, each, then print within double quotes is comma sqrt in brackets each.
05:29 Now we have finished the statements in the block, and still the interpreter is showing four dots, this means that you are still inside the block.
05:41 To exit from the block press the return key or the enter key twice without entering anything else.
05:50 It printed the square root of each number in the list, which was executed in the for loop.
05:57 Find the cube of all the numbers from one to ten.
06:01 But this time let us try it in the vanilla version of Python interpreter.
06:07 Start the vanilla version of Python interpreter by issuing the command python in your terminal.
06:21 So type for i in range within brackets 1,11 colon and hit enter
06:38 press enter once, and we will see that this time it shows four dots, but the cursor is close to the dots, so we have to indent the block.
06:52 The vanilla version of Python interpreter does not indent the code automatically.
06:58 So enter four spaces there and then type the following
07:04 Then type print i comma within double quotes cube is comma i star star 3. Hit Enter.
07:21 Now when we hit Enter, we still see the four dots.
07:26 To get out of the block, hit enter once again.
07:32 Okay!
07:33 so the main thing we learnt here is how to use the Python interpreter and the IPython interpreter to specify blocks.
07:43 But while we were generating the multiplication table we used something new, range function.
07:50 range function is an inbuilt function in Python which can be used to generate a list of integers from a starting number to an ending number.
07:57 Note that the ending number that you specify will not be included in the list
08:03 Print all the odd numbers from 1 to 50. Let us do it in our IPython interpreter for ease of use.
08:18 The problem can be solved by just using the range() function.
08:22 It can be solved as through command line you can type ipython and hit Enter.
08:28 This time we passed three parameters to range() function unlike the previous case where we passed only two parameters.
08:37 The first two parameters are same in both the cases.
08:40 The first parameter is the starting number of the sequence and the second parameter is the end of the range.
08:45 Note that the sequence does not include the ending number.
08:48 The third parameter is for stepping through the sequence.
08:53 Here we gave two which means we are skipping every alternate element.
09:03 So type print range within brackets 1 comma 51 comma 2
09:07 This brings us to the end of this tutorial.
09:11 In this tutorial,we learnt to,
09:12 1. create blocks in python using for loop
09:15 2. indent the blocks of code
09:17 3. iterate over a list using for loop
09:21 use the range() function
09:24 Here are some self assessment questions for you to solve
09:27 1. Indentation is not mandatory in Python
09:30 Is it true or false?
09:33 2. Write a code using for loop to print the product of all natural numbers from 1 to 20.
09:39 3. What will be the output of-
range within brackets 1,5
09:47 Now, the answers,
09:48 1. False. Indentation is essential in python.
09:53 2. We use the for loop in the following manner.
09:56 y is equal to 1 for x in range(1,21):
10:00 y star is equal to x
10:03 then we have to print y
10:04 3.range within brackets 1,5 will produce a list of integers from 1 to 4. that is 1,2,3,4 in square brackets.
10:17 Hope you have enjoyed this tutorial and found it useful.
10:20 Thank you!

Contributors and Content Editors

Gaurav, Minal, PoojaMoolya, Sneha