Difference between revisions of "Python/C3/Loops/English-timed"

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 113: Line 113:
 
|-
 
|-
 
| 3:24
 
| 3:24
| Switch to the terminal for solution.
+
| Switch to the terminal
  
 
|-
 
|-
Line 126: Line 126:
 
|-
 
|-
 
|  3:52
 
|  3:52
| As we already know, pass</tt > is just a syntactic filler.  
+
| As we already know, pass is just a syntactic filler.  
  
 
|-
 
|-
Line 173: Line 173:
 
|-
 
|-
 
|  5:50
 
|  5: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 <tt>for</tt> loop as follows.
+
| 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.
  
 
|-
 
|-

Revision as of 15:19, 22 March 2013

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

while True colon

   print i multiply by i
   i += 2
   if i is less than 10 colon
   break
5: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.

5:42 continue is used to skip execution of the rest of the loop on this iteration and continue to the end of this iteration.
5: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.
6: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
6:36 Now Pause the video here, try out the following exercise and resume the video.
6: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.
6:53 (Do not modify the range function call.)
6:59 Now Switch to the terminal for solution.
7: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
7:30 This brings us to the end of this tutorial.
7:33 In this tutorial, we have learnt to, 1. Iterate over a sequence using ``for and ``while loops.
7:38 2. Break out of loops using ``break statement.
7:42 3. Skip iterations using ``continue statement.
7:45 4. Use the ``pass statement in a loop.
7:49 Here are some self assessment questions for you to solve
7:52 1. Given range(1 comma 4) ; Write a code to print only the number 1.
8:01 2.Second one is, Which statement do you use to skip iterations. - break - pass - continue
8:10 And the answers,
8:12 1. First one, We can use the break statement in a for loop as,

colon colon

8:21 for i in range within bracket 1 comma 4 colon
8:27 Then print i break
8:30 And second one, In order to skip iterations,we make use of the continue statement.
8:37 Hope you have enjoyed this tutorial and found it useful.

Contributors and Content Editors

Gaurav, Kavita salve, Minal, PoojaMoolya, Sneha