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

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with " {| style="border-spacing:0;" | style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:n...")
 
 
(One intermediate revision by one other user not shown)
Line 12: Line 12:
  
 
Objectives  
 
Objectives  
 
  
  
Line 20: Line 19:
 
* '''while loop'''
 
* '''while loop'''
 
* '''break, continue '''and''' pass statements''' in the '''loops'''
 
* '''break, continue '''and''' pass statements''' in the '''loops'''
 
 
  
 
|-
 
|-
Line 32: Line 29:
 
* '''Python 3.4.3 '''and
 
* '''Python 3.4.3 '''and
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 46: Line 41:
 
* use''' conditional statements'''
 
* use''' conditional statements'''
  
If not, see the relevant Python tutorials on this website.
+
If not, see the relevant '''Python''' tutorials on this website.
  
 
|-
 
|-
Line 64: Line 59:
  
 
'''<nowiki>#statements after while loop</nowiki>'''
 
'''<nowiki>#statements after while loop</nowiki>'''
 
 
 
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Let us begin the tutorial with '''while loop'''.  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| 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.'''
+
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.
+
When the condition becomes '''false''', program control passes to the line immediately after the '''loop'''.
  
  
Line 105: Line 97:
  
 
'''i += 2'''
 
'''i += 2'''
 
 
 
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Let us '''print''' the squares of all the odd numbers less than 10, using the '''while loop'''.
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Let us '''print''' the squares of all the odd numbers less than 10, using the '''while loop'''.
  
Line 133: Line 122:
  
  
If it is '''True''', it '''executes''' the '''block''' of '''code '''within the loop.  
+
If it is '''True''', it '''executes''' the '''block''' of '''code '''within the '''loop'''.  
  
  
Then press the '''Enter''' twice to get the output.
+
Then press the '''Enter''' key twice to get the output.
  
  
Line 198: Line 187:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"|  
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"|  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch back to the terminal for the solution.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch back to the '''terminal''' for the solution.
  
 
|-
 
|-
Line 245: Line 234:
 
|-
 
|-
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"|  
 
| style="background-color:#ffffff;border-top:0.5pt solid #000001;border-bottom:0.5pt solid #000001;border-left:0.5pt solid #000001;border-right:none;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"|  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch back to the terminal for the solution.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Switch back to the '''terminal''' for the solution.
  
 
|-
 
|-
Line 287: Line 276:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next let us look at how to use the''' break, pass '''and '''continue''' keywords.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next let us see at how to use the''' break, pass '''and '''continue''' keywords.
  
  
Now type this code to understand the '''pass''' statement.
+
Now type this code to understand the '''pass statement.'''
  
  
Line 323: Line 312:
  
 
'''Enter''' twice
 
'''Enter''' twice
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next let us understand about the '''break''' statement.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next let us understand about the '''break statement.'''
  
  
Line 354: Line 343:
  
 
Out
 
Out
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next let us understand about the '''continue''' statement.
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.095cm;padding-right:0.191cm;"| Next let us understand about the '''continue statement.'''
  
  
Line 360: Line 349:
  
  
The''' continue''' statement rejects all the remaining statements in the current iteration of the loop.
+
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.  
+
And it moves the control back to the top of the '''loop'''.  
  
  
Line 393: Line 382:
 
* '''Break''' out of '''loops''' using '''break statement'''
 
* '''Break''' out of '''loops''' using '''break statement'''
 
* Skip '''iterations''' using '''continue statement'''
 
* Skip '''iterations''' using '''continue statement'''
* Use the '''pass statement''' in a'' '''''loop'''
+
* Use the '''pass statement''' in a '''loop'''
 
+
 
+
  
 
|-
 
|-
Line 408: Line 395:
  
 
# Given '''range(1,4)''', write a '''code''' to '''print''' only the number 1.
 
# Given '''range(1,4)''', write a '''code''' to '''print''' only the number 1.
# Which '''statement''' do you use to skip '''iterations'''.
+
# Which '''statement''' do you use to skip '''iterations'''?
 
+
 
+
  
 
|-
 
|-
Line 424: Line 409:
  
 
# In order to skip '''iterations''', we can use the '''continue statement'''.
 
# In order to skip '''iterations''', we can use the '''continue statement'''.
 
 
  
 
|-
 
|-

Latest revision as of 07:47, 8 June 2018

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