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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with '{| border=1 !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, # u…')
 
Line 10: Line 10:
 
| At the end of this tutorial, you will be able to,
 
| At the end of this tutorial, you will be able to,
  
# use the <tt>for</tt> loop
+
# use the for loop
# use the <tt>while</tt> loop
+
# use the while loop
# Use <tt>break</tt>, <tt>continue</tt> and <tt>pass</tt> statements to play around with loops.
+
# Use break, continue and pass statements to play around with loops.
  
 
|-
 
|-
Line 28: Line 28:
 
|-
 
|-
 
|  0:32
 
|  0:32
| We shall first begin with the <tt>while</tt> loop.  
+
| We shall first begin with the while loop.  
  
 
|-
 
|-
 
|  0:34
 
|  0:34
| The <tt>while</tt> loop is used for repeated execution as long as a condition is <tt>True</tt>.
+
| The while loop is used for repeated execution as long as a condition is   True .
  
 
|-
 
|-
 
|  0:39
 
|  0:39
| Let us print the squares of all the odd numbers less than 10, using the <tt>while</tt> loop.
+
| Let us print the squares of all the odd numbers less than 10, using the while loop.
  
 
|-
 
|-
Line 52: Line 52:
 
|-
 
|-
 
|  1:23
 
|  1:23
| The <tt>while</tt> loop, repeatedly checks if the condition is true and executes the block of code within the loop, if it is.  
+
| The while loop, repeatedly checks if the condition is true and executes the block of code within the loop, if it is.  
  
 
|-
 
|-
 
|  1:30
 
|  1:30
| As with any other block in Python, the code within the <tt>while</tt> block is indented to the right by 4 spaces.
+
| As with any other block in Python, the code within the while block is indented to the right by 4 spaces.
  
 
|-
 
|-
Line 64: Line 64:
 
|-
 
|-
 
| 1:48
 
| 1:48
| '''Write a <tt>while</tt> loop to print the squares of all the even''' numbers below 10.
+
| '''Write a while loop to print the squares of all the even''' numbers below 10.
  
 
|-
 
|-
Line 80: Line 80:
 
|-
 
|-
 
| 2:27
 
| 2:27
| Let us now solve the same problem of printing the squares of all odd numbers less than 10, using the <tt>for</tt> loop.  
+
| Let us now solve the same problem of printing the squares of all odd numbers less than 10, using the for loop.  
  
 
|-
 
|-
 
|  2:34
 
|  2:34
| As we know, the <tt>for</tt> loop iterates over a list or any other sequential data type.  
+
| As we know, the   for loop iterates over a list or any other sequential data type.  
  
 
|-
 
|-
 
|  2:40
 
|  2:40
| So, we use the <tt>range</tt> function to get a list of odd numbers below 10, and then iterate over it and print the required stuff.
+
| 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.
  
 
|-
 
|-
Line 109: Line 109:
 
|-
 
|-
 
| 3:19
 
| 3:19
| '''Write a <tt>for</tt> loop to print the squares of all the even''' numbers below 10.
+
| '''Write a for loop to print the squares of all the even''' numbers below 10.
  
 
|-
 
|-
Line 122: Line 122:
 
|-
 
|-
 
| 3:46
 
| 3:46
| Let us now look at how to use the keywords, <tt>pass</tt>, <tt>break</tt> and <tt>continue</tt>.
+
| Let us now look at how to use the keywords, pass , break and continue .
  
 
|-
 
|-
 
|  3:52
 
|  3:52
| As we already know, <tt>pass</tt> is just a syntactic filler.  
+
| As we already know, pass</tt > is just a syntactic filler.  
  
 
|-
 
|-
Line 142: Line 142:
 
|-
 
|-
 
| 4:20
 
| 4:20
| <tt>break</tt> is used to break out of the innermost loop.  
+
| break is used to break out of the innermost loop.  
  
 
|-
 
|-
 
|  4:24
 
|  4:24
| The <tt>while</tt> loop to print the squares of all the odd numbers below 10, can be modified using the <tt>break</tt> statement, as follows
+
| The   while loop to print the squares of all the odd numbers below 10, can be modified using the break statement, as follows
  
 
|-
 
|-
Line 169: Line 169:
 
|-
 
|-
 
| 5:42
 
| 5:42
| <tt>continue</tt> is used to skip execution of the rest of the loop on this iteration and continue to the end of this iteration.
+
| continue is used to skip execution of the rest of the loop on this iteration and continue to the end of this iteration.
  
 
|-
 
|-
Line 189: Line 189:
 
|-
 
|-
 
| 6:41
 
| 6:41
| Using the <tt>continue</tt> keyword modify the <tt>for</tt> loop, with the <tt>range(2 comma 10 comma 2)</tt>, to print the squares of even numbers below 10, which are multiples of 4.
+
| 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.
  
 
|-
 
|-
Line 232: Line 232:
 
|-
 
|-
 
|  7:52
 
|  7:52
| 1. Given <tt>range(1 comma 4)</tt><nowiki>; Write a code to print only the number 1.</nowiki>
+
| 1. Given range(1 comma 4) ; Write a code to print only the number 1.  
  
 
|-
 
|-
Line 257: Line 257:
 
|-
 
|-
 
|  8:30
 
|  8:30
|  And second one, In order to skip iterations,we make use of the <tt>continue</tt> statement.
+
|  And second one, In order to skip iterations,we make use of the continue statement.
  
 
|-
 
|-

Revision as of 15:05, 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 for solution.
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</tt > 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