Back to PYTHON FUNDAMENTALS

Python For Loops

Master loops to perform repeated actions and process data efficiently in Python.

5 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.

Ready to continue?

Mark this lesson as complete when you're ready to proceed.

Key moments

  1. For Loop Definition — For loops execute a block of code a fixed number of times over any iterable sequence.
  2. Basic Range Syntax — The basic syntax uses range(start, stop) where the stop number is exclusive.
  3. Counting Backwards — Counting backwards can be achieved by enclosing the range function within the reversed() function.
  4. Using Step Parameter — The optional step parameter allows counting by increments other than one, such as by twos or threes.
  5. Iterating Over Strings — For loops can iterate directly over a string, processing each character sequentially.
  6. Continue Keyword — The continue keyword skips the current iteration and moves to the next item in the sequence.
  7. Break Keyword — The break keyword immediately terminates the entire loop execution.
  8. Loop Comparison Summary — For loops are best for fixed execution counts, while while loops are better for conditional or potentially infinite execution.
PDF notes

Frequently asked questions

Can I use a for loop to iterate over something that isn't a string or a range?

Yes, you can iterate over anything considered iterable, such as lists, tuples, dictionaries, and file objects.

What happens to the loop variable (x) after the loop finishes?

The loop variable retains the value of the last item it processed before the loop completed or was broken.

Why are for loops better than while loops for fixed counts?

For loops handle the initialization, condition checking, and increment/decrement automatically based on the iterable, making the code cleaner and less error-prone for definite iteration.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.