Python-3.4.3/C3/Loops/English
| |
|
| 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 look 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. |