<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Vedantgk on Medium]]></title>
        <description><![CDATA[Stories by Vedantgk on Medium]]></description>
        <link>https://medium.com/@vedantgk2004?source=rss-739922959aa5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*RUJc0zMopRahU-qy</url>
            <title>Stories by Vedantgk on Medium</title>
            <link>https://medium.com/@vedantgk2004?source=rss-739922959aa5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 23:12:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@vedantgk2004/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[The Story of Loops in Python: Doing More with Less Code]]></title>
            <link>https://medium.com/@vedantgk2004/the-story-of-loops-in-python-doing-more-with-less-code-8ee13e57ee95?source=rss-739922959aa5------2</link>
            <guid isPermaLink="false">https://medium.com/p/8ee13e57ee95</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Vedantgk]]></dc:creator>
            <pubDate>Tue, 11 Aug 2026 15:53:04 GMT</pubDate>
            <atom:updated>2026-08-11T15:53:04.568Z</atom:updated>
            <content:encoded><![CDATA[<h3>Introduction</h3><p>Imagine you are asked to print your name 100 times.</p><p>You could write the print() statement 100 times, but that would be boring, lengthy, and inefficient. Instead, Python gives us a powerful feature called a <strong>loop</strong>.</p><p>A loop allows us to repeat a particular task multiple times without writing the same code again and again.</p><p>When I started learning Python, loops felt a little confusing at first. But after understanding the basic idea, I realized that loops are actually very simple. They are like telling Python:</p><blockquote><em>“Keep doing this task until I tell you to stop.”</em></blockquote><p>In this blog, let’s understand Python loops with simple examples and real-life situations.</p><h3>What is a Loop?</h3><p>A <strong>loop</strong> is a programming structure that repeatedly executes a block of code.</p><p>For example, suppose we want to print numbers from 1 to 5.</p><p>Without a loop, we would write:</p><pre>print(1)<br>print(2)<br>print(3)<br>print(4)<br>print(5)</pre><p>This works, but imagine printing numbers from 1 to 1000!</p><p>Instead, we can use a loop:</p><pre>for i in range(1, 6):<br>    print(i)</pre><h3>Output:</h3><pre>1<br>2<br>3<br>4<br>5</pre><p>The loop saves us from writing unnecessary code.</p><h3>Types of Loops in Python</h3><p>Python mainly provides two types of loops:</p><ol><li><strong>for loop</strong></li><li><strong>while loop</strong></li></ol><p>Let’s understand both with examples.</p><h3>1. The for Loop</h3><p>The for loop is used when we want to repeat something for a specific number of times or go through the elements of a sequence.</p><p>For example, suppose I have a list of fruits:</p><pre>fruits = [&quot;Apple&quot;, &quot;Banana&quot;, &quot;Mango&quot;, &quot;Orange&quot;]</pre><pre>for fruit in fruits:<br>    print(fruit)</pre><h3>Output:</h3><pre>Apple<br>Banana<br>Mango<br>Orange</pre><p>Here, Python takes one fruit at a time from the list and prints it.</p><p>The for loop is very useful when working with lists, strings, ranges, and other collections of data.</p><h3>Understanding range()</h3><p>The range() function is commonly used with a for loop.</p><p>For example:</p><pre>for i in range(1, 6):<br>    print(i)</pre><p>The output is:</p><pre>1<br>2<br>3<br>4<br>5</pre><p>Notice that 6 is not printed.</p><p>This is because range(1, 6) starts from 1 and stops <strong>before 6</strong>.</p><p>We can also use:</p><pre>for i in range(5):<br>    print(i)</pre><p>Output:</p><pre>0<br>1<br>2<br>3<br>4</pre><p>Python starts counting from 0 when the starting value is not provided.</p><h3>2. The while Loop</h3><p>The second important loop in Python is the while loop.</p><p>A while loop continues running <strong>as long as a particular condition is true</strong>.</p><p>For example:</p><pre>number = 1</pre><pre>while number &lt;= 5:<br>    print(number)<br>    number = number + 1</pre><h3>Output:</h3><pre>1<br>2<br>3<br>4<br>5</pre><p>Here, the loop checks the condition:</p><pre>number &lt;= 5</pre><p>As long as the condition is true, the loop continues.</p><p>When number becomes 6, the condition becomes false and the loop stops.</p><h3>A Real-Life Example of a while Loop</h3><p>Imagine you are studying for an exam.</p><p>You tell yourself:</p><blockquote><em>“I will study until my syllabus is completed.”</em></blockquote><p>This is similar to a while loop.</p><pre>chapters_completed = 0</pre><pre>while chapters_completed &lt; 5:<br>    print(&quot;Study the chapter&quot;)<br>    chapters_completed += 1</pre><p>The program keeps repeating the task until five chapters are completed.</p><p>This is one reason why loops are useful: they can represent repetitive tasks that happen in real life.</p><h3>break Statement</h3><p>Sometimes we want to stop a loop before it naturally finishes.</p><p>For this, Python provides the break statement.</p><p>Example:</p><pre>for i in range(1, 11):<br>    if i == 6:<br>        break<br>    print(i)</pre><h3>Output:</h3><pre>1<br>2<br>3<br>4<br>5</pre><p>When i becomes 6, the break statement stops the loop completely.</p><p>It is like saying:</p><blockquote><em>“Stop! I don’t want to continue.”</em></blockquote><h3>continue Statement</h3><p>The continue statement works differently.</p><p>It skips the current iteration and moves to the next one.</p><p>For example:</p><pre>for i in range(1, 6):<br>    if i == 3:<br>        continue<br>    print(i)</pre><h3>Output:</h3><pre>1<br>2<br>4<br>5</pre><p>When Python reaches 3, it skips printing it and continues with the next number.</p><p>So we can remember it like this:</p><ul><li>break → <strong>Stop the loop</strong></li><li>continue → <strong>Skip the current iteration</strong></li></ul><h3>Nested Loops</h3><p>A loop inside another loop is called a <strong>nested loop</strong>.</p><p>It may sound complicated, but the idea is simple.</p><p>For example:</p><pre>for i in range(1, 4):<br>    for j in range(1, 4):<br>        print(i, j)</pre><p>Here, the second loop runs for every iteration of the first loop.</p><p>Nested loops are commonly used for patterns, tables, matrices, and many programming problems.</p><p>For example, we can create a simple multiplication table:</p><pre>for i in range(1, 6):<br>    print(&quot;2 x&quot;, i, &quot;=&quot;, 2 * i)</pre><h3>Output:</h3><pre>2 x 1 = 2<br>2 x 2 = 4<br>2 x 3 = 6<br>2 x 4 = 8<br>2 x 5 = 10</pre><h3>Why Are Loops Important?</h3><p>Loops are important because they make programs:</p><h3>1. Shorter</h3><p>Instead of writing the same statement many times, we can use a loop.</p><h3>2. Faster to Write</h3><p>A few lines of code can perform a task hundreds or thousands of times.</p><h3>3. Easier to Maintain</h3><p>If we need to change the repeated task, we can change the code inside the loop instead of changing many separate statements.</p><h3>4. Useful for Large Data</h3><p>Loops are frequently used to process lists, files, databases, and other large amounts of information.</p><h3>A Small Python Project Using Loops</h3><p>Let’s create a simple program that calculates the sum of numbers from 1 to 10.</p><pre>total = 0</pre><pre>for i in range(1, 11):<br>    total = total + i</pre><pre>print(&quot;Sum =&quot;, total)</pre><h3>Output:</h3><pre>Sum = 55</pre><p>The loop adds each number to total.</p><p>This small example shows how loops can be used to solve real programming problems.</p><h3>My Understanding of Loops</h3><p>When I first saw loops, I thought they were difficult because the program seemed to repeat itself automatically.</p><p>But after practicing, I understood that a loop is simply a way of giving Python a repeated instruction.</p><p>For example:</p><blockquote><em>“Print these numbers.”</em></blockquote><blockquote><em>“Check these students.”</em></blockquote><blockquote><em>“Calculate these values.”</em></blockquote><blockquote><em>“Keep asking until the correct answer is entered.”</em></blockquote><p>All of these situations can involve loops.</p><p>The important thing is to understand <strong>when the loop should start, what it should do, and when it should stop</strong>.</p><h3>Conclusion</h3><p>Loops are one of the most important concepts in Python programming. They help us repeat tasks without writing the same code again and again.</p><p>Python provides two main loops: the <strong>for loop</strong> and the <strong>while loop</strong>. Along with them, statements such as <strong>break</strong> and <strong>continue</strong> give us more control over how loops work.</p><p>At first, loops may seem confusing, but with simple examples and regular practice, they become easy to understand.</p><p><strong><em>Don’t repeat your code when Python can repeat it for you.</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8ee13e57ee95" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>