Python-3.4.3/C3/Loops/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue
Narration
Show Slide title Welcome to the spoken tutorial on Loops.
Show Slide

Objectives


In this tutorial, we will learn to use,
  • for loop
  • while loop
  • break, continue and pass statements in the loops
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3 and
  • IPython 5.1.0
Show Slide

Pre-requisite


To practice this tutorial, you should know how to
  • use conditional statements

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

Show Slide


while loop


Syntax:

while <condition>:

#True block statements

#update condition variable

#statements after while loop

Let us begin the tutorial with while loop.


The while loop is used to execute a set of statements as long as a condition is true.


When the condition becomes false, program control passes to the line immediately after the loop.


The code inside the while loop should be indented four spaces to the right.

Open the terminal Let us start ipython.


Open the terminal.

Type ipython3


Press Enter

Type ipython3 and press Enter.


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

Type,

i = 1

while i<10:

print (i*i)

i += 2

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


Type,

i is equal to 1

while i less than 10 colon

print inside brackets i asterisk i

i plus equal to 2

Highlight while i<10


Highlight

print (i*i)

i += 2

Here the while loop repeatedly checks the condition.


If it is True, it executes the block of code within the loop.


Then press the Enter key twice to get the output.


<<PAUSE>>

Type:


for n in range(1, 10, 2):

print (n*n)


Highlight for


Highlight range

Next let us now solve the same problem using for loop.


Type,

for n in range inside brackets 1, 10, 2 colon

print inside brackets n asterisk n


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 iterate over it.


Then we print the square of n.

Press Enter twice


Highlight both the outputs

Press Enter twice.


We can see that we got the same output as before.


Note that now the lines of code are less.

Pause the video here.


Try this exercise and then resume the video.

Show Slide Assignment 1 Write a while loop to print the squares of all the even numbers below 10.
Switch back to the terminal for the solution.
Type, i = 2


while i<10:

print (i*i)

i += 2


Enter twice


Highlight the output

Type, i is equal to 2


while i less than 10 colon

print inside brackets i asterisk i

i plus equal to 2


Press the Enter key twice.


We got the required output.

Pause the video here.


Try this exercise and then resume the video.

Show Slide

Assignment 2

Write a for loop to print the squares of all the even numbers below 10.
Switch back to the terminal for the solution.
Type, for n in range(2, 10, 2):

print (n*n)


Enter twice


Highlight the output

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.


<<PAUSE>>

Type

for n in range(2, 10, 2):

pass


Highlight pass


Enter twice


Next let us see at how to use the break, pass and continue keywords.


Now type this code to understand the pass statement.


As we already know, pass is just a placeholder.


It is used for the sake of completion of blocks, that do not have any code within them.


Nothing gets printed when we execute this code.


Press Enter twice.


As we expected nothing gets printed.

Type

for letter in 'python':

if letter == 'h':

break

print ('Current Letter :', letter)


Highlight break


Enter twice

Next let us understand about the break statement.


break is used to break the innermost loop.


Type the code as shown.


Here, it will iterate each letter in the word python.


And it breaks the loop when h is the iteration value.


Press Enter twice to see the output.

for n in range(1, 10, 2):

if n % 3 == 0:

continue

print (n*n)


Press Enter twice


Out

Next let us understand about the continue statement.


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.


It will print the squares of all the odd numbers below 10 which are not multiples of 3.


Press Enter twice.


We got the required output.

Show Slide

Summary


This brings us to the end of this tutorial.


Let us summarise.


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
Show Slide

Evaluation


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?
Show Slide

Solution of self assessment questions


And the answers,
  1. We can use the break statement in the for loop as shown here
  1. In order to skip iterations, we can use the continue statement.
Show Slide Forum Please post your timed queries in this forum.
Show Slide Fossee Forum Please post your general queries on Python in this forum.
Show Slide Textbook Companion FOSSEE team coordinates the TBC project.
Show Slide

Acknowledgment


Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.

For more details, visit this website.

Previous slide This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Priyacst