Skip to main content
March 23, 2026Noble Desktop/2 min read

Using For Loops in Python

Master Python iteration with practical for loop fundamentals

Learning Objective

This tutorial covers for loops in Python, also known as definite loops because we know exactly how many times they will execute.

For Loop vs While Loop Overview

FeatureFor LoopWhile Loop
TypeDefinite loopIndefinite loop
Execution countKnown in advanceDepends on condition
Best use caseIterating through sequencesConditional repetition
Recommended: Use for loops when you need to iterate through a known sequence of items

Video Transcription

Hi, my name is Art and I teach Python at Noble Desktop. In this video, I'll explain how to use for loops in Python—one of the most fundamental programming concepts you'll encounter in data science, automation, and web development.

Python offers two primary loop types: for loops and while loops. For loops are definite loops because we know exactly how many iterations they'll perform, making them ideal for processing collections like lists, dictionaries, or database records. While loops, by contrast, continue running based on a condition and are better suited for scenarios where the iteration count is unknown.

Let me demonstrate for loops with a practical example. Suppose you're building a system that sends personalized greetings to a list of users—a common task in customer relationship management or email marketing applications.

I'll create a list container with several names: John, Mark, and Mary. Our goal is to generate individual greetings: "hello Mark," "hello John," and "hello Mary." Rather than writing separate print statements for each name (imagine doing this for thousands of users), we'll iterate through the list programmatically.

The for loop syntax follows a consistent pattern. We begin with the keyword 'for', followed by a variable name of our choosing—in this case, 'name'. Next comes the keyword 'in', then our list, followed by a colon. The indented block beneath contains the code that executes for each iteration. Here's the structure: `for name in friends: print("hello", name)`

When executed, this code produces "hello Mark," "hello Mary," "hello John." The for loop automatically handles the iteration mechanics: it traverses the sequence, assigns each item to our 'name' variable, and executes the indented code block. Behind the scenes, Python sequentially assigns 'John', 'Mark', and 'Mary' to the 'name' variable, demonstrating the elegant simplicity of Python's iteration protocol.

This fundamental pattern scales beautifully—whether you're processing three names or three million customer records, the syntax remains identical. For loops form the backbone of data processing, web scraping, file manipulation, and countless other professional Python applications.

That's the essence of for loops in Python. In my next video, I'll demonstrate while loops and help you understand when to choose definite iteration over conditional looping in your projects.

How to Write a For Loop

1

Start with the keyword 'for'

Every for loop must begin with the for keyword to tell Python you're creating a loop structure.

2

Create a variable name

Choose a descriptive variable name that will hold each item as the loop iterates through the sequence.

3

Use 'in' followed by your sequence

Specify the list, string, or other iterable object you want to loop through using the 'in' keyword.

4

Add colon and indentation

End the for statement with a colon and indent the code block that should execute for each iteration.

Key For Loop Concepts

Iteration Variable

The variable after 'for' holds each item from the sequence one at a time. In the example, 'name' holds 'John', then 'Mark', then 'Mary' in sequence.

Automatic Assignment

Python automatically assigns each item from the list to your variable. You don't need to manually access list indexes or manage the loop counter.

Sequential Processing

The loop processes items in order from the sequence. Each iteration executes the indented code block with the current item value.

It kinda goes through that sequence of items, grabs an item and assign it to 'name'
Art explains how Python automatically handles the iteration process behind the scenes

For Loop Best Practices

0/3

Key Takeaways

1For loops in Python are definite loops because you know exactly how many times they will execute
2Every for loop starts with the 'for' keyword followed by a variable name and the 'in' keyword
3The loop variable automatically gets assigned each item from the sequence during iteration
4Python handles the iteration process automatically without requiring manual index management
5For loops are ideal for processing sequences like lists, strings, and other iterable objects
6Proper indentation is required for the code block that executes during each loop iteration
7While loops differ from for loops as they depend on conditions rather than predetermined sequences
8For loops provide a clean and readable way to perform repetitive tasks on collections of data

RELATED ARTICLES