Python/C3/Getting-started-with-for/English-timed
From Script | Spoken-Tutorial
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,
|
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! |