Python For Loops

Python · Loops

Theory

A for loop runs its block once for each item of an iterable: a list, tuple, string, set, dict, range, file or any other object that can hand out items one at a time. In for month in ['Jan', 'Feb', 'Mar']:, the name month is bound to 'Jan' for the first pass, then 'Feb', then 'Mar', and the loop ends when the items run out. Behind the scenes Python calls iter() on the object to get an iterator and then next() repeatedly until it raises StopIteration.

range(n) produces the numbers 0 to n-1 without building a list: range(2, 10, 3) gives 2, 5, 8, and the stop value is never included. enumerate(items) gives (index, item) pairs, and zip(a, b) walks two sequences side by side, which beats writing for i in range(len(items)). The loop variable keeps its last value after the loop ends. Avoid adding or removing items in a list while looping over that same list; it skips items. Loop over a copy or build a new list instead.

Why we use it

Almost every data task repeats an action for each item: each row, each file, each customer. A for loop does that without counters or index bookkeeping, and works on any iterable.

When to use it

Use for when you are processing every item of a collection or a known number of repetitions with range(). Use while when you repeat until a condition changes and do not know the count in advance. For building a new list, a comprehension is often shorter.

How it works

  1. for month in months: begins
  2. Python calls iter(months) to get an iterator
  3. Call next() on the iterator
  4. Did it return an item? Yes: Bind it to month and run the loop body. No: StopIteration: leave the loop.
  5. After the body, go back for the next item
  6. Loop done; output: Jan, Feb, Mar

Syntax

for item in iterable:
    ...

Example

for month in ["Jan","Feb","Mar"]:
    print(month)

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic