1. What is a For Loop?

A for loop is used to iterate over a sequence of values.

  • Executes a block of code once for each item in the sequence
  • Commonly used when the number of iterations is known

Key Point:
For loop = repeat code over a sequence


2. Basic Structure

for variable in sequence:
    # code to execute

Components:

  • for → keyword
  • variable → temporary variable
  • in → connects variable to sequence
  • sequence → collection of values
  • : → starts code block

Key Point:
Each value in the sequence is assigned to the variable


3. Example: Loop with Range

for x in range(5):
    print(x)

Output:

  • 0, 1, 2, 3, 4

Key Point:
Loop runs once per value in the sequence


4. Range Function

The range() function generates a sequence of numbers.

Default behavior:

  • Starts at 0
  • Increments by 1
  • Stops before given number

Example:

  • range(5) → 0, 1, 2, 3, 4

Key Point:
Range excludes the ending value


5. Loop Variable

The loop variable:

  • Takes each value in sequence
  • Changes automatically each iteration

Example:

  • x → 0 → 1 → 2 → 3 → 4

Key Point:
Variable updates automatically


6. Naming the Loop Variable

  • Can use any name:
    • x, n, number, etc.
  • Must be consistent inside loop

Key Point:
Name should be meaningful when possible


7. Execution Flow

For each iteration:

  1. Assign value to variable
  2. Execute loop body
  3. Move to next value

Key Point:
Loop processes values one at a time


8. For Loop vs While Loop

FeatureFor LoopWhile Loop
IterationFixed sequenceCondition-based
Use caseKnown number of stepsUnknown number of steps

Key Point:
Use for loop when sequence is known


9. Reading Files with For Loop

For loops can iterate over file lines.

Example concept:

  • Open file
  • Loop through each line
  • Process or print

Key Point:
For loop is useful for data processing


10. Nested For Loops

A nested loop is a loop inside another loop.

Example use:

  • 2D data (rows and columns)

Concept:

  • Outer loop → columns
  • Inner loop → rows

Key Point:
Nested loops handle multi-dimensional data


11. Real-World Example

ATM system:

  • Allows limited number of attempts
  • Loop tracks attempts

Key Point:
For loops control fixed repetitions


12. Benefits of For Loops

  • Efficient for repetitive tasks
  • Easy to read
  • Reduces code duplication

Key Point:
For loops simplify iteration tasks


13. Practical Use Cases

  • Iterating through lists
  • Processing datasets
  • Reading files
  • Generating outputs

Key Point:
Essential tool in data analysis


14. Key Insight

  • For loops work best with:
    • Lists
    • Ranges
    • Files
    • Any iterable

Key Point:
For loop requires an iterable


Final Summary

A for loop in Python is used to iterate over a sequence of values and execute a block of code for each value. It is especially useful when the number of iterations is known. The range() function is commonly used to generate sequences, and loop variables automatically update with each iteration. For loops are fundamental tools for data processing, file handling, and working with structured data.


Key Takeaways

  • For loop = iterate over sequence
  • Uses for variable in sequence
  • range() generates number sequences
  • Loop variable updates automatically
  • Stops before end value
  • Useful for fixed repetitions
  • Supports nested loops
  • Widely used in data analysis