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

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(5 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{| border=1
 
{| border=1
!Visual Cue
+
|'''Time'''
!Narration
+
|'''Narration'''
 +
 
 
|-
 
|-
| 0:01
+
| 00:01
 
| Hello Friends and Welcome to the tutorial on 'loops' in Python.
 
| Hello Friends and Welcome to the tutorial on 'loops' in Python.
  
 
|-
 
|-
| 0:05
+
| 00:05
 
| At the end of this tutorial, you will be able to,
 
| At the end of this tutorial, you will be able to,
 
+
use the  '''for''' loop
# use the  for loop
+
use the '''while''' loop
# use the while loop
+
Use '''break''', '''continue''' and '''pass''' statements to play around with loops.
# Use break, continue and pass statements to play around with loops.
+
  
 
|-
 
|-
| 0:17
+
| 00:17
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with for" and "Conditionals".
 
| Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with for" and "Conditionals".
  
 
|-
 
|-
| 0:24
+
| 00:24
 
| Let us start our ipython interpreter.
 
| Let us start our ipython interpreter.
  
 
|-
 
|-
|0:28
+
|00:28
 
|Type ipython in the terminal
 
|Type ipython in the terminal
  
 
|-
 
|-
0:32
+
00:32
 
| We shall first begin with the while  loop.  
 
| We shall first begin with the while  loop.  
  
 
|-
 
|-
0:34
+
00:34
 
| The while  loop is used for repeated execution as long as a condition is  True .
 
| The while  loop is used for repeated execution as long as a condition is  True .
  
 
|-
 
|-
0:39
+
00:39
 
| Let us print the squares of all the odd numbers less than 10, using the  while  loop.
 
| Let us print the squares of all the odd numbers less than 10, using the  while  loop.
  
 
|-
 
|-
|0:45
+
|00:45
|Type i = 1
+
|Type i = 1 while i less than 10 colon print i multiply by i, i += 2
 
+
while i less than 10 colon
+
    print i multiply by i
+
    i += 2
+
  
 
|-
 
|-
| 1:19
+
| 01:19
 
| This loop prints the squares of the odd numbers below 10.
 
| This loop prints the squares of the odd numbers below 10.
  
 
|-
 
|-
1:23
+
01:23
 
| The while  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
+
01:30
 
| As with any other block in Python, the code within the  while  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.
  
 
|-
 
|-
1:41
+
01:41
 
| Pause the video here, try out the following exercise and resume the video.
 
| Pause the video here, try out the following exercise and resume the video.
  
 
|-
 
|-
| 1:48
+
| 01:48
 
| '''Write a  while  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.
  
 
|-
 
|-
| 1:55
+
| 01:55
 
| Switch to the terminal for solution.
 
| Switch to the terminal for solution.
  
 
|-
 
|-
|1:58
+
|01:58
|Type i = 2
+
|Type i = 2, while i less than 10 colon print i multiply by i i += 2
 
+
  while i less than 10 colon
+
    print i multiply by i
+
    i += 2
+
  
 
|-
 
|-
| 2:27
+
| 02:27
 
| Let us now solve the same problem of printing the squares of all odd numbers less than 10, using the  for  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
+
02:34
 
| As we know, the  for  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
+
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.
 
| 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
+
|02:48
|Type for n in range within bracket 1 comma 10 comma 2 colon
+
|Type for n in range within bracket 1 comma 10 comma 2 colon. print n multiply by n
      print n multiply by n
+
  
 
|-
 
|-
| 3:07
+
| 03:07
 
| We can see that we got the same output as before.  
 
| We can see that we got the same output as before.  
  
 
|-
 
|-
3:10
+
03:10
 
| Note that the lines of code are less.
 
| Note that the lines of code are less.
  
 
|-
 
|-
3:13
+
03:13
 
| Pause the video here, try out the following exercise and resume the video.
 
| Pause the video here, try out the following exercise and resume the video.
  
 
|-
 
|-
| 3:19
+
| 03:19
 
| '''Write a  for 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.
  
 
|-
 
|-
| 3:24
+
| 03:24
| Switch to the terminal for solution.
+
| Switch to the terminal
  
 
|-
 
|-
| 3:26
+
| 03:26
|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. print n multiply by n
        print n multiply by n
+
  
 
|-
 
|-
| 3:46
+
| 03:46
 
| Let us now look at how to use the keywords,  pass ,  break  and  continue .
 
| Let us now look at how to use the keywords,  pass ,  break  and  continue .
  
 
|-
 
|-
3:52
+
03:52
| As we already know, pass</tt > is just a syntactic filler.  
+
| As we already know, pass is just a syntactic filler.  
  
 
|-
 
|-
3:56
+
03:56
 
| It is used for the sake of completion of blocks, that do not have any code within them.
 
| It is used for the sake of completion of blocks, that do not have any code within them.
  
 
|-
 
|-
|4:02
+
|04: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 pass And Enter.
 
+
    pass
+
 
+
    And Enter.
+
  
 
|-
 
|-
| 4:20
+
| 04:20
 
|  break  is used to break out of the innermost loop.  
 
|  break  is used to break out of the innermost loop.  
  
 
|-
 
|-
4:24
+
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
 
| 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
+
|04:31
|Type = 1
+
|Type = 1,while True colon print i multiply by i   i += 2   if i is less than 10 colon break
 
+
while True colon
+
    print i multiply by i
+
    i += 2
+
    if i is less than 10 colon
+
    break
+
 
|-
 
|-
|5: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.
+
  
 
|-
 
|-
| 5:42
+
| 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.
 
|  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
+
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 <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.
  
 
|-
 
|-
|6:03
+
|06:03
 
|So, in terminal, Type for n in range within bracket 1 comma 10 comma 2 colon
 
|So, in terminal, Type for n in range within bracket 1 comma 10 comma 2 colon
 
+
if n modulo 3 == 0 colon
    if n modulo 3 == 0 colon
+
continue
        continue
+
print n multiply by n
    print n multiply by n
+
  
 
|-
 
|-
| 6:36
+
| 06:36
 
| Now Pause the video here, try out the following exercise and resume the video.
 
| Now Pause the video here, try out the following exercise and resume the video.
  
 
|-
 
|-
| 6:41
+
| 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.
 
| 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
+
06:53
| (Do not modify the range function call.)
+
| (Do not modify the range function call.)
  
 
|-
 
|-
| 6:59
+
| 06:59
 
| Now  Switch to the terminal for solution.
 
| Now  Switch to the terminal for solution.
  
 
|-
 
|-
|7: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
+
  
 
|-
 
|-
| 7:30
+
| 07:30
 
| This brings us to the end of this tutorial.
 
| This brings us to the end of this tutorial.
  
 
|-
 
|-
7:33
+
07:33
|  In this tutorial, we have learnt to, 1. Iterate over a sequence using ``for'' and ``while'' loops.
+
|  In this tutorial, we have learnt to, Iterate over a sequence using ``for'' and ``while'' loops.
 
    
 
    
 
|-
 
|-
7:38
+
07:38
| 2. Break out of loops using ``break'' statement.
+
|Break out of loops using ''break'' statement.
  
 
|-
 
|-
7:42
+
07:42
| 3. Skip iterations using ``continue'' statement.
+
|Skip iterations using ''continue'' statement.
  
 
|-
 
|-
7:45
+
07:45
| 4. Use the ``pass'' statement in a loop.
+
|Use the ''pass'' statement in a loop.
  
 
|-
 
|-
| 7:49
+
| 07:49
 
| Here are some self assessment questions for you to solve
 
| Here are some self assessment questions for you to solve
  
 
|-
 
|-
7:52
+
07:52
| 1. Given  range(1 comma 4) ; Write a code to print only the number 1.  
+
|Given  range(1 comma 4) ; Write a code to print only the number 1.  
  
 
|-
 
|-
8:01
+
08:01
| 2.Second one is, Which statement do you use to skip iterations. - break - pass - continue
+
|Second one is, Which statement do you use to skip iterations. - break - pass - continue
  
 
|-
 
|-
| 8:10
+
| 08:10
 
| And the answers,
 
| And the answers,
  
 
|-
 
|-
8:12
+
08:12
| 1. First one, We can use the break statement in a for loop as,
+
| First one, We can use the break statement in a for loop as,''' colon colon'''
''' colon colon'''
+
  
 
|-
 
|-
8:21
+
08:21
 
| '''for i in range within bracket 1 comma 4 colon'''
 
| '''for i in range within bracket 1 comma 4 colon'''
  
 
|-
 
|-
8:27
+
08:27
 
| Then print i break
 
| Then print i break
 
   
 
   
 
|-
 
|-
8:30
+
08:30
 
|  And second one, In order to skip iterations,we make use of the continue  statement.
 
|  And second one, In order to skip iterations,we make use of the continue  statement.
  
 
|-
 
|-
| 8:37
+
| 08:37
 
| Hope you have enjoyed this tutorial and found it useful.  
 
| Hope you have enjoyed this tutorial and found it useful.  
  
 
|}
 
|}

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.

Contributors and Content Editors

Gaurav, Kavita salve, Minal, PoojaMoolya, Sneha