Python-3.4.3/C3/Loops/English-timed

From Script | Spoken-Tutorial
Revision as of 11:17, 11 June 2019 by Pratik kamble (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
Time Narration
00:01 Welcome to the spoken tutorial on Loops.
00:05 In this tutorial, we will learn to use,

for loop

while loop

break, continue and pass statements in the loops

00:18 To record this tutorial, I am using

Ubuntu Linux 16.04 operating system

Python 3.4.3 and

IPython 5.1.0

00:34 To practice this tutorial, you should know how to

use conditional statements

If not, see the relevant Python tutorials on this website.

00:45 Let us begin the tutorial with while loop.
00:49 The while loop is used to execute a set of statements as long as a condition is true.
00:56 When the condition becomes false, program control passes to the line immediately after the loop.
01:03 The code inside the while loop should be indented four spaces to the right.
01:09 Let us start ipython. Open the terminal.
01:14 Type ipython3 and press Enter.

From here onwards, remember to press the Enter key after typing every command on the terminal.

01:26 Let us print the squares of all the odd numbers less than 10, using the while loop.
01:32 Type, i is equal to 1 while i less than 10 colon print inside brackets i asterisk i i plus equal to 2
01:50 Here the while loop repeatedly checks the condition.
01:54 If it is True, it executes the block of code within the loop.
01:59 Then press the Enter key twice to get the output.
02:04 Next let us now solve the same problem using for loop.
02:09 Type, for n in range inside brackets 1, 10, 2 colon print inside brackets n asterisk n
02:24 As we know, the for loop iterates over a list or any other sequential data type.
02:30 So, we use the range function to get a list of odd numbers below 10 and iterate over it. Then we print the square of n.
02:40 Press Enter twice. We can see that we got the same output as before. Note that now the lines of code are less.
02:52 Pause the video here. Try this exercise and then resume the video.
02:58 Write a while loop to print the squares of all the even numbers below 10.
03:04 Switch back to the terminal for the solution.
03:08 Type, i is equal to 2
03:12 while i less than 10 colon print inside brackets i asterisk i i plus equal to 2
03:26 Press the Enter key twice. We got the required output.
03:32 Pause the video here. Try this exercise and then resume the video.
03:39 Write a for loop to print the squares of all the even numbers below 10.
03:45 Switch back to the terminal for the solution.
03:49 Type, for n in range inside brackets 2, 10, 2 colon print inside brackets n asterisk n

Press Enter twice. We got the required output.

04:09 Next let us see at how to use the break, pass and continue keywords.
04:15 Now type this code to understand the pass statement.
04:20 As we already know, pass is just a placeholder.
04:24 It is used for the sake of completion of blocks, that do not have any code within them.
04:31 Nothing gets printed when we execute this code. Press Enter twice. As we expected nothing gets printed.
04:42 Next let us understand about the break statement.
04:46 break is used to break the innermost loop. Type the code as shown.
04:53 Here, it will iterate each letter in the word python.
04:58 And it breaks the loop when h is the iteration value. Press Enter twice to see the output.
05:07 Next let us understand about the continue statement.
05:12 Type the code as shown. The continue statement rejects all the remaining statements in the current iteration of the loop.

And it moves the control back to the top of the loop.

05:27 It will print the squares of all the odd numbers below 10 which are not multiples of 3.
05:35 Press Enter twice. We got the required output.
05:41 This brings us to the end of this tutorial. Let us summarise.
05:47 In this tutorial, we have learnt to,

Iterate over a sequence using for and while loops

Break out of loops using break statement

Skip iterations using continue statement

Use the pass statement in a loop

06:05 Here are some self assessment questions for you to solve

Given range(1,4), write a code to print only the number 1.

Which statement do you use to skip iterations?

06:24 And the answers,

We can use the break statement in the for loop as shown here

In order to skip iterations, we can use the continue statement.

06:37 Please post your timed queries in this forum.
06:41 Please post your general queries on Python in this forum.
06:46 FOSSEE team coordinates the TBC project.
06:50 Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India. For more details, visit this website.
07:00 This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

Pratik kamble