Fundamentals 7 min read

Understanding and Using For Loops in Python

This article explains the purpose, syntax, and practical usage of Python's for loop, covering iteration over lists, strings, ranges, conditional filtering, loop control statements, list comprehensions, and real‑world examples with complete code snippets.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Understanding and Using For Loops in Python

For loops are an essential programming construct, and this article introduces how to use them correctly in Python.

Unlike while loops, Python's for loop provides deterministic control flow and is ideal for iterating over any sequence such as strings, lists, tuples, dictionaries, or sets.

The basic syntax is:

for new_variable in parent_variable:
    execute some statements

Examples demonstrate iterating over a list of Linux distributions and printing each item:

release = ["linuxmi", "ubuntu", "centos", "debian", "redhat"]
for i in release:
    print(i)

You can filter items inside the loop, for instance printing only elements that contain the letter "a":

release = ["linuxmi", "ubuntu", "centos", "debian", "redhat"]
for i in release:
    if "a" in i:
        print(i)

Python for loops also support an else clause that runs when the loop completes without a break:

b = [2, 3, 5]
for i in b:
    print(i)
else:
    print("循环结束")

Control statements such as break and continue can alter the loop flow:

b = [2, 3, 5]
for i in b:
    if i > 3:
        break
    print(i)
b = [2, 3, 5]
for i in b:
    if i > 3:
        continue
    print(i)

Using the built‑in range function, you can generate numeric sequences. The following prints numbers 1‑100, then only odd numbers, and finally a simple multiplication table for each odd number:

for x in range(1, 101):
    print(x)

for x in range(1, 101):
    if x % 2 == 1:
        print(x)

for x in range(1, 101):
    if x % 2 == 1:
        print(x, "x", 2, "=", x * 2)

For loops work with strings as well. The example below iterates over each character in a string and prints it:

a = "linuxmi"
for i in a:
    print(i)

You can count characters (including spaces) in a string using count inside a loop:

a = ["welcome linuxmi.com"]
for i in a:
    print(i.count(''))

List comprehensions provide a concise way to achieve the same results. The following creates a list of character counts:

a = ["welcome linuxmi.com"]
c = [b.count('') for b in a]
print(c)

Similarly, you can filter dictionaries in a list. The code below prints the names of unhealthy buffalo entries:

array = [{"name":"buffalo1", "healthy":"Yes"},
         {"name":"buffalo3", "healthy":"No"},
         {"name":"buffalo4", "healthy":"Yes"},
         {"name":"buffalo5", "healthy":"Yes"},
         {"name":"buffalo8", "healthy":"No"},
         {"name":"buffalo7", "healthy":"No"},
         {"name":"buffalo9", "healthy":"No"}]
for buffalos in array:
    if buffalos["healthy"] == "No":
        print("Quarantine", buffalos["name"])

The article concludes by encouraging readers to ask questions in the comments.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

TutorialLoopsfor loop
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.