Difference between revisions of "Python-3.4.3/C3/Loops/English"
(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...") |
Nancyvarkey (Talk | contribs) |
||
(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 | + | | 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''' | + | 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''' | + | | 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''' | + | | 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''' | + | 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 | + | * 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
|
|
Show Slide title | Welcome to the spoken tutorial on Loops. |
Show Slide
Objectives
|
In this tutorial, we will learn to use,
|
Show Slide
System Specifications |
To record this tutorial, I am using
|
Show Slide
Pre-requisite
|
To practice this tutorial, you should know how to
If not, see the relevant Python tutorials on this website. |
Show Slide
while <condition>: #True block statements #update condition variable #statements after while loop |
Let us begin the tutorial with while loop.
|
Open the terminal | Let us start ipython.
|
Type ipython3
|
Type ipython3 and press Enter.
|
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.
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
print (i*i) i += 2 |
Here the while loop repeatedly checks the condition.
|
Type:
print (n*n)
|
Next let us now solve the same problem using for loop.
for n in range inside brackets 1, 10, 2 colon print inside brackets n asterisk n
|
Press Enter twice
|
Press Enter twice.
|
Pause the video here.
| |
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
print (i*i) i += 2
|
Type, i is equal to 2
print inside brackets i asterisk i i plus equal to 2
|
Pause the video here.
| |
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)
|
Type,
for n in range inside brackets 2, 10, 2 colon print inside brackets n asterisk n
|
Type
for n in range(2, 10, 2): pass
|
Next let us see at how to use the break, pass and continue keywords.
|
Type
for letter in 'python': if letter == 'h': break print ('Current Letter :', letter)
|
Next let us understand about the break statement.
|
for n in range(1, 10, 2):
if n % 3 == 0: continue print (n*n)
|
Next let us understand about the continue statement.
|
Show Slide
Summary
|
This brings us to the end of this tutorial.
|
Show Slide
Evaluation
|
Here are some self assessment questions for you to solve
|
Show Slide
Solution of self assessment questions
|
And the answers,
|
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. |