Python/C3/Loops/English

From Script | Spoken-Tutorial
Revision as of 11:47, 29 November 2012 by Chandrika (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Visual Cue Narration
Show Slide 1

Containing title, name of the production team along with the logo of MHRD

Hello Friends and Welcome to the tutorial on 'loops' in Python.
Show Slide 2

Learning objectives

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.


Show Slide 3

Pre-requisite slide

Before beginning this tutorial,we would suggest you to complete the tutorial on "Getting started with for" and "Conditionals".
Switch to the ipython terminal
ipython
Let us start our ipython interpreter.
i = 1
while i<10:
    print i*i
    i += 2
We shall first begin with the while loop. The while loop is used for repeated execution as long as a condition is True.

Let us print the squares of all the odd numbers less than 10, using the while loop.

This loop prints the squares of the odd numbers below 10.

The while loop, repeatedly checks if the condition is true and executes the block of code within the loop, if it is. As with any other block in Python, the code within the while block is indented to the right by 4 spaces.

Pause the video here, try out the following exercise and resume the video.

Show Slide 4

Assignment 1

Write a while loop to print the squares of all the even

numbers below 10.

Continue from paused state Switch to the terminal
i = 2

while i<10:
    print i*i
    i += 2
Switch to the terminal for solution.
for n in range(1, 10, 2):
    print n*n
Let us now solve the same problem of printing the squares of all odd numbers less than 10, using the for loop. As we know, the for loop iterates over a list or any other sequential data type. 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.
We can see that we got the same output as before. Note that the lines of code are less.

Pause the video here, try out the following exercise and resume the video.

Show Slide 5

Assignment 2

Write a for loop to print the squares of all the even

numbers below 10.

Continue from paused state Switch to the terminal
for n in range(2, 10, 2):
    print n*n
Switch to the terminal for solution.
for n in range(2, 10, 2):
    pass
Let us now look at how to use the keywords, pass, break and continue.

As we already know, pass is just a syntactic filler. It is used for the sake of completion of blocks, that do not have any code within them.

i = 1
while True:
    print i*i
    i += 2
    if i<10:
        break
break is used to break out of the innermost loop. The while loop to print the squares of all the odd numbers below 10, can be modified using the break statement, as follows
for n in range(1, 10, 2):
    if n%3 == 0:
        continue
    print n*n
continue is used to skip execution of the rest of the loop on this iteration and continue to the end of this iteration.

Say, 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.

Pause the video here, try out the following exercise and resume the video.
Show Slide 6

Assignment 3

Using the continue keyword modify the for loop, with the range(2, 10, 2), to print the squares of even numbers below 10, which are multiples of 4. (Do not modify the range function call.)
Continue from paused state Switch to the terminal
for n in range(2, 10, 2):
    if n%4:
        continue
    print n*n
Switch to the terminal for solution.
Show Slide 7

Summary slide

This brings us to the end of this tutorial. In this tutorial, we have learnt to,
  1. Iterate over a sequence using ``for and ``while loops.
    Inline literal start-string without end-string.
    Inline literal start-string without end-string.
  2. Break out of loops using ``break statement.
    Inline literal start-string without end-string.
  3. Skip iterations using ``continue statement.
    Inline literal start-string without end-string.
  4. Use the ``pass statement in a loop.
    Inline literal start-string without end-string.


Show Slide 8

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. Given range(1,4); Write a code to print only the number 1.
  2. Which statement do you use to skip iterations. - break - pass - continue


Show Slide 9

Solution of self assessment questions on slide

And the answers,
  1. We can use the break statement in a for loop as,

::

for i in range(1, 4):

print i break

  1. In order to skip iterations,we make use of the continue statement.


Show Slide 10

Acknowledgment slide

Hope you have enjoyed this tutorial and found it useful. Thank you!

Contributors and Content Editors

Chandrika