Python for Loop
Tags: python, pythonforloop, pythonloopexample
Published: 2020-05-19 12:38:42

<hr />

<h1>#Python</h1>
<ul>
 	<li>When to use for loops?</li>
 	<li>How do for loop works in Python?</li>
 	<li>Nested for loops</li>
 	<li>Early Exits (Break)</li>
</ul>
<h2>When to use for loop in python</h2>
It's used when you have a block of statement/code that you want to repeat a certain number of times. <a href="https://onlinetutorials.tech/category/python-tutorials-from-onlinetutorials-tech/"><strong>Python</strong> </a>for statements iterates over members of a sequence in order, executing a block each time.
<h3><strong>For loop syntax:</strong></h3>
<blockquote>
<pre>for iterating_variable in sequence:
    statement(s)</pre>
</blockquote>
<h2>How do for loop works in python</h2>
Basically in for <a href="https://onlinetutorials.tech/python-for-loop-statements/"><strong>loops</strong> </a>we determine conditions (<em>statement expression to identify condition is completed or not</em>) then increase the expression as the per increment statement and proceed further. Actually any object in python with <strong>iterable method (</strong><em>It means data can be represented in list form</em><strong>)</strong> can be used for <a href="https://onlinetutorials.tech/python-for-loop-statements/">loop</a>.
<h2>Nested for loop in python</h2>
We we have any block of code executed N number of times, then we have another block of code withing above block which execute M number of times then it is called as nested for <a href="https://onlinetutorials.tech/python-for-loop-statements/">loop in python</a>.
<blockquote>
<pre>for x in range(1, 11):
<span id="line-2-2" class="anchor"></span>    for y in range(1, 11):
<span id="line-3-1" class="anchor"></span>        print('%d * %d = %d' % (x, y, x*y))
</pre>
</blockquote>
<h2>Early Exit (break)</h2>
It is like when we want to break, or we have to come out of <a href="https://onlinetutorials.tech/python-for-loop-statements/"><strong>loop</strong> </a>after meeting certain condition then we forcefully exit the loop.
<blockquote>
<pre>for x in range(3):
<span id="line-2-3" class="anchor"></span>    if x == 1:
<span id="line-3-2" class="anchor"></span>        break</pre>
</blockquote>
<h2>Let's see some interesting for loop example of python:</h2>
<h4>For..Else</h4>
<blockquote>
<pre>for x in range(5):
<span id="line-2-4" class="anchor"></span>    print(x)
<span id="line-3-3" class="anchor"></span>else:
<span id="line-4-1" class="anchor"></span>    print('Final x = %d' % (x))</pre>
</blockquote>
<h4>Strings as an iterable</h4>
<blockquote>
<pre>string = "Online Tutorials"
<span id="line-2-5" class="anchor"></span>for x in string:
<span id="line-3-4" class="anchor"></span>    print(x)</pre>
</blockquote>
<h4>Lists as an iterable</h4>
<blockquote>
<pre>collection = ['Online', 7, 'Tutorials']
<span id="line-2-6" class="anchor"></span>for x in collection:
<span id="line-3-5" class="anchor"></span>    print(x)</pre>
</blockquote>
<h4 class="line867">Loop over Lists of lists<span id="line-90" class="anchor"></span><span id="line-91" class="anchor"></span><span id="line-92" class="anchor"></span></h4>
<blockquote>
<pre><span id="line-1-7" class="anchor"></span>list_of_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
<span id="line-2-7" class="anchor"></span>for eachList in list_of_array :
<span id="line-3-6" class="anchor"></span>    for x in eachList :
<span id="line-4-2" class="anchor"></span>        print(x)</pre>
</blockquote>
<h4 class="line867">Creating your own custom iterable<span id="line-102" class="anchor"></span><span id="line-103" class="anchor"></span></h4>
<blockquote>
<pre><span id="line-1-8" class="anchor"></span>class Iterable(object):
<span id="line-2-8" class="anchor"></span>
<span id="line-3-7" class="anchor"></span>    def __init__(self, values):
<span id="line-4-3" class="anchor"></span>        self.values = values
<span id="line-5" class="anchor"></span>        self.location = 0
<span id="line-6" class="anchor"></span>
<span id="line-7" class="anchor"></span>    def __iter__(self):
<span id="line-8" class="anchor"></span>        return self
<span id="line-9" class="anchor"></span>
<span id="line-10" class="anchor"></span>    def next(self):
<span id="line-11" class="anchor"></span>        if self.location == len(self.values):
<span id="line-12" class="anchor"></span>            raise StopIteration
<span id="line-13" class="anchor"></span>        value = self.values[self.location]
<span id="line-14" class="anchor"></span>        self.location += 1
<span id="line-15" class="anchor"></span>        return value</pre>
</blockquote>
<h4 class="line867">Custom range generator using yield<span id="line-123" class="anchor"></span><span id="line-124" class="anchor"></span></h4>
<blockquote>
<pre><span id="line-1-9" class="anchor"></span>for x in my_custom_range(1, 20, 1):
<span id="line-7-1" class="anchor"></span>    print(x)</pre>
<pre>myCustomlist = ['a', 'b', 'c', 'd']
<span id="line-2-10" class="anchor"></span>for i in range(len(myCustomlist )):
<span id="line-3-9" class="anchor"></span>    # perform operation with myCustomlist[i]</pre>
</blockquote>
<strong>Reference</strong>
<ul>
 	<li><a href="https://onlinetutorials.tech/python-for-loop-statements/" rel="dofollow">OnlineTutorials.tech</a></li>
 	
</ul>
Visit us for more tutorials:
<a href="https://onlinetutorials.tech/category/java-tutorials-online-tutorials/" target="_blank" rel="dofollow">Java Tutorial</a> 
<a href="https://onlinetutorials.tech/category/python-tutorials-from-onlinetutorials-tech/" target="_blank" rel="dofollow">Python Tutorial</a> 
<a href="https://onlinetutorials.tech/category/rabbitmq-tutorial/" target="_blank" rel="dofollow">RabbitMQ Tutorial</a> 
<a href="https://onlinetutorials.tech/category/spring-boot/" target="_blank" rel="dofollow">Spring boot Tutorial</a> 
<hr />