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