<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="https://script.spoken-tutorial.org/skins/common/feed.css?303"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC3%2FLoops%2FEnglish</id>
		<title>Python/C3/Loops/English - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://script.spoken-tutorial.org/index.php?action=history&amp;feed=atom&amp;title=Python%2FC3%2FLoops%2FEnglish"/>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C3/Loops/English&amp;action=history"/>
		<updated>2026-05-15T07:38:50Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.23.17</generator>

	<entry>
		<id>https://script.spoken-tutorial.org/index.php?title=Python/C3/Loops/English&amp;diff=497&amp;oldid=prev</id>
		<title>Chandrika: Created page with '{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello Friends and Welcome to the tutorial on '…'</title>
		<link rel="alternate" type="text/html" href="https://script.spoken-tutorial.org/index.php?title=Python/C3/Loops/English&amp;diff=497&amp;oldid=prev"/>
				<updated>2012-11-29T06:17:00Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;#039;{| border=1 !Visual Cue !Narration |- | Show Slide 1   Containing title, name of the production team along with the logo of MHRD  | Hello Friends and Welcome to the tutorial on &amp;#039;…&amp;#039;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{| border=1&lt;br /&gt;
!Visual Cue&lt;br /&gt;
!Narration&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 1 &lt;br /&gt;
&lt;br /&gt;
Containing title, name of the production team along with the logo of MHRD &lt;br /&gt;
| Hello Friends and Welcome to the tutorial on 'loops' in Python.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 2 &lt;br /&gt;
&lt;br /&gt;
Learning objectives &lt;br /&gt;
| At the end of this tutorial, you will be able to,&lt;br /&gt;
&lt;br /&gt;
# use the &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop&lt;br /&gt;
# use the &amp;lt;tt&amp;gt;while&amp;lt;/tt&amp;gt; loop&lt;br /&gt;
# Use &amp;lt;tt&amp;gt;break&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;continue&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;pass&amp;lt;/tt&amp;gt; statements to play around with loops.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 3 &lt;br /&gt;
&lt;br /&gt;
Pre-requisite slide &lt;br /&gt;
| Before beginning this tutorial,we would suggest you to complete the tutorial on &amp;quot;Getting started with for&amp;quot; and &amp;quot;Conditionals&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Switch to the ipython terminal &lt;br /&gt;
&lt;br /&gt;
 ipython&lt;br /&gt;
| Let us start our ipython interpreter.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  i = 1&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;nowiki&amp;gt;while i&amp;lt;10:&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
     print i*i&lt;br /&gt;
     i += 2&lt;br /&gt;
| We shall first begin with the &amp;lt;tt&amp;gt;while&amp;lt;/tt&amp;gt; loop. The &amp;lt;tt&amp;gt;while&amp;lt;/tt&amp;gt; loop is used for repeated execution as long as a condition is &amp;lt;tt&amp;gt;True&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let us print the squares of all the odd numbers less than 10, using the &amp;lt;tt&amp;gt;while&amp;lt;/tt&amp;gt; loop.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| This loop prints the squares of the odd numbers below 10.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;tt&amp;gt;while&amp;lt;/tt&amp;gt; loop, repeatedly checks if the condition is true and executes the block of code within the loop, if it is. As with any other block in Python, the code within the &amp;lt;tt&amp;gt;while&amp;lt;/tt&amp;gt; block is indented to the right by 4 spaces.&lt;br /&gt;
&lt;br /&gt;
Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 4 &lt;br /&gt;
&lt;br /&gt;
Assignment 1 &lt;br /&gt;
| '''Write a &amp;lt;tt&amp;gt;while&amp;lt;/tt&amp;gt; loop to print the squares of all the even'''&lt;br /&gt;
&lt;br /&gt;
numbers below 10.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 i = 2&lt;br /&gt;
 &lt;br /&gt;
 &amp;lt;nowiki&amp;gt;while i&amp;lt;10:&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
     print i*i&lt;br /&gt;
     i += 2&lt;br /&gt;
| Switch to the terminal for solution.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  for n in range(1, 10, 2):&lt;br /&gt;
     print n*n&lt;br /&gt;
| Let us now solve the same problem of printing the squares of all odd numbers less than 10, using the &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop. As we know, the &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop iterates over a list or any other sequential data type. So, we use the &amp;lt;tt&amp;gt;range&amp;lt;/tt&amp;gt; function to get a list of odd numbers below 10, and then iterate over it and print the required stuff.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| We can see that we got the same output as before. Note that the lines of code are less.&lt;br /&gt;
&lt;br /&gt;
Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 5 &lt;br /&gt;
&lt;br /&gt;
Assignment 2 &lt;br /&gt;
| '''Write a &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop to print the squares of all the even'''&lt;br /&gt;
&lt;br /&gt;
numbers below 10.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 for n in range(2, 10, 2):&lt;br /&gt;
     print n*n&lt;br /&gt;
| Switch to the terminal for solution.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  for n in range(2, 10, 2):&lt;br /&gt;
     pass&lt;br /&gt;
| Let us now look at how to use the keywords, &amp;lt;tt&amp;gt;pass&amp;lt;/tt&amp;gt;, &amp;lt;tt&amp;gt;break&amp;lt;/tt&amp;gt; and &amp;lt;tt&amp;gt;continue&amp;lt;/tt&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As we already know, &amp;lt;tt&amp;gt;pass&amp;lt;/tt&amp;gt; is just a syntactic filler. It is used for the sake of completion of blocks, that do not have any code within them.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  i = 1&lt;br /&gt;
 &lt;br /&gt;
 while True:&lt;br /&gt;
     print i*i&lt;br /&gt;
     i += 2&lt;br /&gt;
     &amp;lt;nowiki&amp;gt;if i&amp;lt;10:&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
         break&lt;br /&gt;
| &amp;lt;tt&amp;gt;break&amp;lt;/tt&amp;gt; is used to break out of the innermost loop. The &amp;lt;tt&amp;gt;while&amp;lt;/tt&amp;gt; loop to print the squares of all the odd numbers below 10, can be modified using the &amp;lt;tt&amp;gt;break&amp;lt;/tt&amp;gt; statement, as follows&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
|  for n in range(1, 10, 2):&lt;br /&gt;
     if n%3 == 0:&lt;br /&gt;
         continue&lt;br /&gt;
     print n*n&lt;br /&gt;
| &amp;lt;tt&amp;gt;continue&amp;lt;/tt&amp;gt; is used to skip execution of the rest of the loop on this iteration and continue to the end of this iteration.&lt;br /&gt;
&lt;br /&gt;
Say, we wish to print the squares of all the odd numbers below 10, which are not multiples of 3, we would modify the &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop as follows.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| &lt;br /&gt;
| Pause the video here, try out the following exercise and resume the video.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 6 &lt;br /&gt;
&lt;br /&gt;
Assignment 3 &lt;br /&gt;
| Using the &amp;lt;tt&amp;gt;continue&amp;lt;/tt&amp;gt; keyword modify the &amp;lt;tt&amp;gt;for&amp;lt;/tt&amp;gt; loop, with the &amp;lt;tt&amp;gt;range(2, 10, 2)&amp;lt;/tt&amp;gt;, to print the squares of even numbers below 10, which are multiples of 4. (Do not modify the range function call.)&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Continue from paused state Switch to the terminal &lt;br /&gt;
&lt;br /&gt;
 for n in range(2, 10, 2):&lt;br /&gt;
     if n%4:&lt;br /&gt;
         continue&lt;br /&gt;
     print n*n&lt;br /&gt;
| Switch to the terminal for solution.&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 7&lt;br /&gt;
&lt;br /&gt;
Summary slide &lt;br /&gt;
| This brings us to the end of this tutorial. In this tutorial, we have learnt to,&lt;br /&gt;
&lt;br /&gt;
# Iterate over a sequence using ``for'' and ``while'' loops.&amp;lt;br/&amp;gt; Inline literal start-string without end-string.&amp;lt;br/&amp;gt; Inline literal start-string without end-string.&lt;br /&gt;
# Break out of loops using ``break'' statement.&amp;lt;br/&amp;gt; Inline literal start-string without end-string.&lt;br /&gt;
# Skip iterations using ``continue'' statement.&amp;lt;br/&amp;gt; Inline literal start-string without end-string.&lt;br /&gt;
# Use the ``pass'' statement in a loop.&amp;lt;br/&amp;gt; Inline literal start-string without end-string.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 8 &lt;br /&gt;
&lt;br /&gt;
Self assessment questions slide &lt;br /&gt;
| Here are some self assessment questions for you to solve&lt;br /&gt;
&lt;br /&gt;
# Given &amp;lt;tt&amp;gt;range(1,4)&amp;lt;/tt&amp;gt;&amp;lt;nowiki&amp;gt;; Write a code to print only the number 1.&amp;lt;/nowiki&amp;gt;&lt;br /&gt;
# Which statement do you use to skip iterations. - break - pass - continue&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 9&lt;br /&gt;
&lt;br /&gt;
Solution of self assessment questions on slide &lt;br /&gt;
| And the answers,&lt;br /&gt;
&lt;br /&gt;
# We can use the break statement in a for loop as,&lt;br /&gt;
&lt;br /&gt;
'''::'''&lt;br /&gt;
&lt;br /&gt;
'''for i in range(1, 4):'''&lt;br /&gt;
&lt;br /&gt;
print i break&lt;br /&gt;
&lt;br /&gt;
# In order to skip iterations,we make use of the &amp;lt;tt&amp;gt;continue&amp;lt;/tt&amp;gt; statement.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
|-&lt;br /&gt;
| Show Slide 10 &lt;br /&gt;
&lt;br /&gt;
Acknowledgment slide &lt;br /&gt;
| Hope you have enjoyed this tutorial and found it useful. Thank you!&lt;br /&gt;
&lt;br /&gt;
|}&lt;/div&gt;</summary>
		<author><name>Chandrika</name></author>	</entry>

	</feed>