Lesson 18 of 55
7 mins readPython Postfix Loops (Modifier Loops)
In Plain English
Similar to postfix conditionals, `EXPRESSION for LIST;` executes an expression once for each element of the list, binding `$_` to each item.
Deep Dive: How It Works
Postfix Loop Syntax: `print "$_\n" for @items;`.
Topic Binding: Automatically binds `$_` as the loop iterator.
Conciseness: Replaces multi-line blocks with crisp single-line idioms.
Core Rules to Remember

Postfix for: Runs the expression once per element in the list.

Compact Idiom: Extensively used in Perl scripts for transformation and logging.
Live Interactive Example
Hit Run Code to see it livePostfix Iteration Examples
Python 3.12
1
2
3
4
5
6
7
8
9
Output Console
Click "Run Code" to view the rendered output.
How it works: The postfix loop executed the statement for every element.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Print Server Nodes with Postfix for
Declare `my @nodes = ("node-1", "node-2", "node-3");`
Print `"Active: $_\n"` for each node using a postfix `for @nodes;`.
1
2
3
4
5
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.