<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Hưng Đặng Quốc</title>
    <description>The latest articles on DEV Community by Hưng Đặng Quốc (@hng_ngquc_55eb161eca).</description>
    <link>https://dev.to/hng_ngquc_55eb161eca</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3866777%2F4e2f5095-fb86-4b85-82ab-ccf8112179c9.png</url>
      <title>DEV Community: Hưng Đặng Quốc</title>
      <link>https://dev.to/hng_ngquc_55eb161eca</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hng_ngquc_55eb161eca"/>
    <language>en</language>
    <item>
      <title>Understanding Python Loops: A Beginner-Friendly Guide</title>
      <dc:creator>Hưng Đặng Quốc</dc:creator>
      <pubDate>Wed, 08 Apr 2026 03:05:38 +0000</pubDate>
      <link>https://dev.to/hng_ngquc_55eb161eca/understanding-python-loops-a-beginner-friendly-guide-3idn</link>
      <guid>https://dev.to/hng_ngquc_55eb161eca/understanding-python-loops-a-beginner-friendly-guide-3idn</guid>
      <description>&lt;p&gt;Welcome to the world of Python programming! If you're just starting out, you might quickly realize that writing the exact same line of code over and over again is not only tedious, but it also makes your code messy and prone to errors.&lt;/p&gt;

&lt;p&gt;Enter &lt;strong&gt;loops&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Loops are fundamental concepts in programming that allow you to automate repetitive tasks efficiently. In Python, there are two main types of loops you will use every day: the &lt;strong&gt;for&lt;/strong&gt; loop and the &lt;strong&gt;while&lt;/strong&gt; loop. Let's break them down so you can start writing cleaner, smarter code.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. The &lt;strong&gt;for&lt;/strong&gt; Loop: Iterating Over a Collection
&lt;/h2&gt;

&lt;p&gt;Think of a for loop like a DJ playing through a setlist. The DJ looks at the list, plays the first song, moves to the second, and continues until the list is finished. In Python, a for loop iterates over a sequence (like a list, a string, or a range of numbers) and executes a block of code for each item in that sequence.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: Looping through a List&lt;/strong&gt;&lt;br&gt;
Let's say you have a list of your favorite fruits and you want to print each one.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq95coqq03dsu3uwowvko.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fq95coqq03dsu3uwowvko.png" alt=" " width="630" height="142"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F14uurw0d5y187q4ivilm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F14uurw0d5y187q4ivilm.png" alt=" " width="623" height="263"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt; Python takes the first item ("apple"), assigns it to the temporary variable &lt;em&gt;fruit&lt;/em&gt;, and runs the &lt;em&gt;print&lt;/em&gt; statement. Then it moves to "banana", and so on, until the list is exhausted.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using range() with for Loops&lt;/strong&gt;&lt;br&gt;
Often, you don't have a list, but you just want to run a block of code a specific number of times. The built-in range() function is perfect for this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F39aak4kpwhh0og9o2cu1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F39aak4kpwhh0og9o2cu1.png" alt=" " width="544" height="116"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pro Tip:&lt;/strong&gt; In Python, counting almost always starts at &lt;em&gt;0&lt;/em&gt;. So, &lt;em&gt;range(5)&lt;/em&gt; gives you 5 numbers: 0, 1, 2, 3, and 4.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. The &lt;strong&gt;while&lt;/strong&gt; Loop: Running Until a Condition Changes
&lt;/h2&gt;

&lt;p&gt;If a &lt;strong&gt;for&lt;/strong&gt; loop is a DJ with a setlist, a &lt;strong&gt;while&lt;/strong&gt; loop is a bouncer at a club. The bouncer will keep letting people in while the club is under maximum capacity. Once that condition becomes false (the club is full), the action stops.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;while&lt;/strong&gt; loop repeatedly executes a target statement as long as a given condition is &lt;strong&gt;True&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example: A Simple Countdown&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4lx1jvo45xlsa5ccby4w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4lx1jvo45xlsa5ccby4w.png" alt=" " width="661" height="215"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft1tk5cmgjf6sjgirwk5i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft1tk5cmgjf6sjgirwk5i.png" alt=" " width="685" height="368"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt; Python checks if &lt;em&gt;countdown &amp;gt; 0&lt;/em&gt;. If it is, it runs the code block. &lt;strong&gt;Crucially&lt;/strong&gt;, we subtract &lt;em&gt;1&lt;/em&gt; from the countdown inside the loop. If we didn't do this, the condition would always be true, creating an &lt;strong&gt;infinite loop&lt;/strong&gt; (a program that runs forever until it crashes). Always make sure your &lt;em&gt;while&lt;/em&gt; loop has a way to eventually end!&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Loop Control Statements: &lt;strong&gt;break&lt;/strong&gt; and &lt;strong&gt;continue&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Sometimes you need a little more control over your loops. Python provides a couple of handy keywords for this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;break:&lt;/em&gt; Exits the loop entirely, no matter what.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;continue:&lt;/em&gt; Skips the rest of the current iteration and jumps straight to the next one.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example using &lt;em&gt;break&lt;/em&gt;&lt;/strong&gt;:&lt;br&gt;
Imagine you are searching for a specific book in a stack. Once you find it, you don't need to check the rest of the books.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv643y5klwssebr7jm2r9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv643y5klwssebr7jm2r9.png" alt=" " width="648" height="221"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example using &lt;em&gt;continue&lt;/em&gt;&lt;/strong&gt;:&lt;br&gt;
Imagine you want to print numbers from 1 to 5, but you hate the number 3 and want to skip it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frhj6h0kggk8r6rq7kpw9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frhj6h0kggk8r6rq7kpw9.png" alt=" " width="460" height="255"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrapping Up
&lt;/h2&gt;

&lt;p&gt;Loops are a programmer's best friend. They save time, reduce human error, and keep your scripts lightweight.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To recap:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use a &lt;em&gt;for&lt;/em&gt; &lt;strong&gt;loop&lt;/strong&gt; when you know beforehand how many times you need to loop (e.g., going through a list of items).&lt;/li&gt;
&lt;li&gt;Use a &lt;em&gt;while&lt;/em&gt; &lt;strong&gt;loop&lt;/strong&gt; when you want to keep looping until a specific condition changes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The best way to master loops is to practice! Try opening your Python editor and writing a loop that counts even numbers, or one that spells out your name letter by letter. Happy coding!&lt;/p&gt;

</description>
      <category>programming</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>python</category>
    </item>
  </channel>
</rss>
