Coding/Learn/python/for loops
Python Beginner Course
01.Introduction to Python
02.Your First Python Program
03.Syntax & Indentation
04.Comments & Documentation
05.Variables
06.Core Data Types
07.Numbers & Arithmetic Operators
08.Text & Strings in Depth
09.Boolean Logic
10.Type Conversion & Casting
11.Interactive User Input
12.Assignment & Shorthand Operators
13.Comparison Operators
14.Logical Operators (and, or, not)
15.If Statements & Conditional Execution
16.If-Else Branching (Dual Paths)
17.If-Elif-Else (Multi-Path Decisions)
18.While Loops & Iteration
19.For Loops & Sequence Traversal
20.The range() Function
21.Lists & Mutable Sequences
22.Tuples & Immutable Data
23.Sets & Unique Collections
24.Dictionaries & Key-Value Stores
25.List Indexing & Slicing
26.Essential List Methods
27.Essential String Methods
28.Functions & Modular Code
29.Function Parameters & Arguments
30.Return Values & Data Flow
31.Exception Handling (try / except)
32.Capstone Mini-Projects
Course Certificate0%

Complete all lessons to earn

LearnFor Loops & Sequence Traversal
19 / 32 0%
Lesson 19 of 32
7 mins read

For Loops & Sequence Traversal

PreviousNext Lesson
In Plain English

A for loop is the civilized way to visit every single item in a collection. Python picks up item 1, gives it to your code, then picks up item 2, and stops automatically when the collection is finished. No index counters, no infinite loops, just pure productivity.

The Mental Model

"Imagine dealing playing cards to players around a table. You take the top card, hand it to the first player, take the next card, hand it to the second player, and stop when everyone has their card."

Deep Dive: How It Works

In Python, for loops are "for-each" loops. Instead of managing a numeric index (like in old C or Java), you iterate directly over the elements themselves.

The syntax "for item in collection:" automatically assigns each element to the temporary variable "item" for that iteration.

For loops work seamlessly on any iterable data type in Python: lists, tuples, sets, dictionaries, and even strings (iterating character by character).

Syntax Blueprint

for item in collection:
    # 4 spaces of indentation
    process(item)

Python extracts each element from collection into the variable item sequentially.

PointCore Rules to Remember

Point star
Item-by-Item Magic: Iterates through each element directly without index management.
Point star
Works on All Iterables: Loop over lists, strings, tuples, dictionaries, and ranges effortlessly.
Point star
Impossible to Infinite Loop: For loops naturally terminate when the collection has no more items.

Common Beginner Traps & How to Fix Them

Mistake:Modifying a list while iterating over it

Why it happens: Deleting items from a list while looping over it skips elements because indices shift.

How to fix: Iterate over a copy of the list (e.g. for item in items.copy():) or use a list comprehension.

Pro Tip: Need both the index AND the item? Use enumerate(items): for index, item in enumerate(items):.

Live Interactive Example

Hit Run Code to see it live
Iterating Through a List of Tasks
Python 3.12
1
2
3
4
5
6
Output Console
Click "Run Code" to view the rendered output.
How it works: Python assigned each string in tasks to the task variable one by one and executed the print statement.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Loop over programming languages

  • PointGiven langs = ["Python", "JavaScript", "Rust"], write a for loop to print each language.
1
2
3
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates