Narration 1 00:00:01 --> 00:00:04 Hello Friends and Welcome to the tutorial on 'loops' in Python. 2 00:00:05 --> 00:00:16 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.

3 00:00:17 --> 00:00:23 Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with for" and "Conditionals". 4 00:00:24 --> 00:00:27 Let us start our ipython interpreter. 5 00:00:28 --> 00:00:31 Type ipython in the terminal 6 00:00:32 --> 00:00:33 We shall first begin with the while loop. 7 00:00:34 --> 00:00:38 The while loop is used for repeated execution as long as a condition is True . 8 00:00:39 --> 00:00:44 Let us print the squares of all the odd numbers less than 10, using the while loop. 9 00:00:45 --> 00:01:18 Type i = 1 while i less than 10 colon print i multiply by i, i += 2 10 00:01:19 --> 00:01:22 This loop prints the squares of the odd numbers below 10. 11 00:01:23 --> 00:01:29 The while loop, repeatedly checks if the condition is true and executes the block of code within the loop, if it is. 12 00:01:30 --> 00:01:40 As with any other block in Python, the code within the while block is indented to the right by 4 spaces. 13 00:01:41 --> 00:01:47 Pause the video here, try out the following exercise and resume the video. 14 00:01:48 --> 00:01:54 Write a while loop to print the squares of all the even numbers below 10. 15 00:01:55 --> 00:01:57 Switch to the terminal for solution. 16 00:01:58 --> 00:02:26 Type i = 2, while i less than 10 colon print i multiply by i i += 2 17 00:02:27 --> 00:02:33 Let us now solve the same problem of printing the squares of all odd numbers less than 10, using the for loop. 18 00:02:34 --> 00:02:39 As we know, the for loop iterates over a list or any other sequential data type. 19 00:02:40 --> 00:02:47 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. 20 00:02:48 --> 00:03:06 Type for n in range within bracket 1 comma 10 comma 2 colon. print n multiply by n 21 00:03:07 --> 00:03:09 We can see that we got the same output as before. 22 00:03:10 --> 00:03:12 Note that the lines of code are less. 23 00:03:13 --> 00:03:18 Pause the video here, try out the following exercise and resume the video. 24 00:03:19 --> 00:03:23 Write a for loop to print the squares of all the even numbers below 10. 25 00:03:24 --> 00:03:25 Switch to the terminal 26 00:03:26 --> 00:03:45 Type for n in range within bracket 2 comma 10 comma 2 colon. print n multiply by n 27 00:03:46 --> 00:03:51 Let us now look at how to use the keywords, pass , break and continue . 28 00:03:52 --> 00:03:55 As we already know, pass is just a syntactic filler. 29 00:03:56 --> 00:04:01 It is used for the sake of completion of blocks, that do not have any code within them. 30 00:04:02 --> 00:04:19 Type for n in range within bracket 2 comma 10 comma 2 colon pass And Enter. 31 00:04:20 --> 00:04:23 break is used to break out of the innermost loop. 32 00:04:24 --> 00:04:30 The while loop to print the squares of all the odd numbers below 10, can be modified using the break statement, as follows 33 00:04:31 --> 00:05:09 Type = 1,while True colon print i multiply by i i += 2 if i is less than 10 colon break 34 00:05:10 --> 00:05:41 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.

35 00:05:42 --> 00:05:49 continue is used to skip execution of the rest of the loop on this iteration and continue to the end of this iteration. 36 00:05:50 --> 00:06:02 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. 37 00:06:03 --> 00:06:35 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

38 00:06:36 --> 00:06:40 Now Pause the video here, try out the following exercise and resume the video. 39 00:06:41 --> 00:06:52 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. 40 00:06:53 --> 00:06:58 (Do not modify the range function call.) 41 00:06:59 --> 00:07:01 Now Switch to the terminal for solution. 42 00:07:02 --> 00:07:29 Type for n in range within bracket 2 comma 10 comma 2 colon

if n modulo 4 colon continue print n multiply by n

43 00:07:30 --> 00:07:32 This brings us to the end of this tutorial. 44 00:07:33 --> 00:07:37 In this tutorial, we have learnt to, Iterate over a sequence using ``for and ``while loops. 45 00:07:38 --> 00:07:41 Break out of loops using break statement. 46 00:07:42 --> 00:07:44 Skip iterations using continue statement. 47 00:07:45 --> 00:07:48 Use the pass statement in a loop. 48 00:07:49 --> 00:07:51 Here are some self assessment questions for you to solve 49 00:07:52 --> 00:08:00 Given range(1 comma 4) ; Write a code to print only the number 1. 50 00:08:01 --> 00:08:09 Second one is, Which statement do you use to skip iterations. - break - pass - continue 51 00:08:10 --> 00:08:11 And the answers, 52 00:08:12 --> 00:08:20 First one, We can use the break statement in a for loop as, colon colon 53 00:08:21 --> 00:08:26 for i in range within bracket 1 comma 4 colon 54 00:08:27 --> 00:08:29 Then print i break 55 00:08:30 --> 00:08:36 And second one, In order to skip iterations,we make use of the continue statement. 56 00:08:37 --> 00:08:42 Hope you have enjoyed this tutorial and found it useful.