Difference between revisions of "Python/C3/Loops/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
PoojaMoolya (Talk | contribs) |
||
Line 140: | Line 140: | ||
|05:10 | |05:10 | ||
|So we got a syntax error because 'if' is not inside the while loop | |So we got a syntax error because 'if' is not inside the while loop | ||
− | So type while True colon | + | |
− | print i multiply by i | + | So type while True colon print i multiply by i, i += 2 |
− | i += 2 | + | |
− | if i less than 10 colon | + | if i less than 10 colon break Make sure that 'if' is inside the while loop. |
− | break | + | |
− | Make sure that 'if' is inside the while loop. | + | |
|- | |- | ||
Line 181: | Line 179: | ||
|07:02 | |07:02 | ||
|Type for n in range within bracket 2 comma 10 comma 2 colon | |Type for n in range within bracket 2 comma 10 comma 2 colon | ||
− | if n modulo 4 colon | + | |
− | continue | + | if n modulo 4 colon continue print n multiply by n |
− | print n multiply by n | + | |
|- | |- | ||
Line 195: | Line 192: | ||
|- | |- | ||
| 07:38 | | 07:38 | ||
− | |Break out of loops using | + | |Break out of loops using ''break'' statement. |
|- | |- | ||
| 07:42 | | 07:42 | ||
− | |Skip iterations using | + | |Skip iterations using ''continue'' statement. |
|- | |- | ||
| 07:45 | | 07:45 | ||
− | |Use the | + | |Use the ''pass'' statement in a loop. |
|- | |- |
Latest revision as of 12:25, 27 March 2017
Time | Narration |
00:01 | Hello Friends and Welcome to the tutorial on 'loops' in Python. |
00:05 | At the end of this tutorial, you will be able to,
use the for loop use the while loop Use break, continue and pass statements to play around with loops. |
00:17 | Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with for" and "Conditionals". |
00:24 | Let us start our ipython interpreter. |
00:28 | Type ipython in the terminal |
00:32 | We shall first begin with the while loop. |
00:34 | The while loop is used for repeated execution as long as a condition is True . |
00:39 | Let us print the squares of all the odd numbers less than 10, using the while loop. |
00:45 | Type i = 1 while i less than 10 colon print i multiply by i, i += 2 |
01:19 | This loop prints the squares of the odd numbers below 10. |
01:23 | The while loop, repeatedly checks if the condition is true and executes the block of code within the loop, if it is. |
01:30 | As with any other block in Python, the code within the while block is indented to the right by 4 spaces. |
01:41 | Pause the video here, try out the following exercise and resume the video. |
01:48 | Write a while loop to print the squares of all the even numbers below 10. |
01:55 | Switch to the terminal for solution. |
01:58 | Type i = 2, while i less than 10 colon print i multiply by i i += 2 |
02:27 | Let us now solve the same problem of printing the squares of all odd numbers less than 10, using the for loop. |
02:34 | As we know, the for loop iterates over a list or any other sequential data type. |
02:40 | So, we use the range function to get a list of odd numbers below 10, and then iterate over it and print the required stuff. |
02:48 | Type for n in range within bracket 1 comma 10 comma 2 colon. print n multiply by n |
03:07 | We can see that we got the same output as before. |
03:10 | Note that the lines of code are less. |
03:13 | Pause the video here, try out the following exercise and resume the video. |
03:19 | Write a for loop to print the squares of all the even numbers below 10. |
03:24 | Switch to the terminal |
03:26 | Type for n in range within bracket 2 comma 10 comma 2 colon. print n multiply by n |
03:46 | Let us now look at how to use the keywords, pass , break and continue . |
03:52 | As we already know, pass is just a syntactic filler. |
03:56 | It is used for the sake of completion of blocks, that do not have any code within them. |
04:02 | Type for n in range within bracket 2 comma 10 comma 2 colon pass And Enter. |
04:20 | break is used to break out of the innermost loop. |
04:24 | The while loop to print the squares of all the odd numbers below 10, can be modified using the break statement, as follows |
04:31 | Type = 1,while True colon print i multiply by i i += 2 if i is less than 10 colon break |
05:10 | So we got a syntax error because 'if' is not inside the while loop
So type while True colon print i multiply by i, i += 2 if i less than 10 colon break Make sure that 'if' is inside the while loop. |
05:42 | continue is used to skip execution of the rest of the loop on this iteration and continue to the end of this iteration. |
05:50 | So, we wish to print the squares of all the odd numbers below 10, which are not multiples of 3, we would modify the for loop as follows. |
06:03 | So, in terminal, Type for n in range within bracket 1 comma 10 comma 2 colon
if n modulo 3 == 0 colon continue print n multiply by n |
06:36 | Now Pause the video here, try out the following exercise and resume the video. |
06:41 | Using the continue keyword modify the for loop, with the range(2 comma 10 comma 2) , to print the squares of even numbers below 10, which are multiples of 4. |
06:53 | (Do not modify the range function call.) |
06:59 | Now Switch to the terminal for solution. |
07:02 | Type for n in range within bracket 2 comma 10 comma 2 colon
if n modulo 4 colon continue print n multiply by n |
07:30 | This brings us to the end of this tutorial. |
07:33 | In this tutorial, we have learnt to, Iterate over a sequence using ``for and ``while loops. |
07:38 | Break out of loops using break statement. |
07:42 | Skip iterations using continue statement. |
07:45 | Use the pass statement in a loop. |
07:49 | Here are some self assessment questions for you to solve |
07:52 | Given range(1 comma 4) ; Write a code to print only the number 1. |
08:01 | Second one is, Which statement do you use to skip iterations. - break - pass - continue |
08:10 | And the answers, |
08:12 | First one, We can use the break statement in a for loop as, colon colon |
08:21 | for i in range within bracket 1 comma 4 colon |
08:27 | Then print i break |
08:30 | And second one, In order to skip iterations,we make use of the continue statement. |
08:37 | Hope you have enjoyed this tutorial and found it useful. |