Python - Statement for Loops

General Format

The Python for loop specifies an assignment target, along with the object you want to step through.

The header is followed by a block of statements that you want to repeat:

for target in object:                 # Assign object items to target 
    statements                        # Repeated loop body: use target 
else:                                 # Optional else part 
    statements                        # If we didn't hit a 'break' 

The for loop's complete format:

for target in object:                 # Assign object items to target 
    statements 
    if test: break                    # Exit loop now, skip else 
    if test: continue                 # Go to top of loop now 
else: 
    statements                        # If we didn't hit a 'break' 

Related Topics