Python for loops are used to iterate over sequences such as lists, tuples, strings, dictionaries, and ranges. They execute a block of code once for each item in the sequence, making them ideal for repetitive tasks like processing data or automating operations.

Basic Syntax

for variable in iterable:
    # Code block to execute
  • variable takes the value of each item in the iterable (e.g., list, string).

  • The loop automatically terminates when the sequence is exhausted.

Common Use Cases

  • Iterating over a list:

    fruits = ["apple", "banana", "cherry"]
    for fruit in fruits:
        print(fruit)
  • Using range() to loop a specific number of times:

    for i in range(10):  # 0 to 9
        print(i)

    To loop from 1 to 10:

    for i in range(1, 11):  # 1 to 10
        print(i)

Advanced Features

  • enumerate() for accessing both index and value:

    for index, value in enumerate(['a', 'b', 'c']):
        print(f"Index: {index}, Value: {value}")
  • break and continue for loop control:

    • break exits the loop early.

    • continue skips the current iteration.

  • else clause executes if the loop completes without break.

Key Differences from While Loops

  • For loops are used for definite iteration (known number of iterations).

  • While loops are used for indefinite iteration (loop while a condition is true).

For more details, refer to W3Schools Python For Loops or Real Python: Python for Loops.

🌐
W3Schools
w3schools.com › python › python_for_loops.asp
Python For Loops
Python Examples Python Compiler ... Q&A Python Bootcamp Python Certificate Python Training ... A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string)....
🌐
Python
wiki.python.org › moin › ForLoop
ForLoop
When you have a block of code you want to run x number of times, then a block of code within that code which you want to run y number of times, you use what is known as a "nested loop". In Python, these are heavily used whenever someone has a list of lists - an iterable object within an iterable object. for x in range(1, 11): for y in range(1, 11): print('%d * %d = %d' % (x, y, x*y))
Discussions

New to python and I need some help with loops.
Loops allow you to repeat a block of code over and over again. There are two main types of loops in Python - for loops and while loops. For loops are used when you know how many times you want to repeat the code. # This will print the numbers 0 to 4 for i in range(5): print(i) The range(5) part creates numbers from 0 to 4. The for loop will go through each of those numbers, storing them in the variable i, and print them out. While loops are used when you don't know exactly how many times you need to repeat the code. You keep looping as long as some condition is true. # This will print numbers as long as x is less than 5 x = 0 while x < 5: print(x) x = x + 1 This starts x at 0, then keeps looping as long as x is less than 5. Each time, it prints x and then adds 1 to x. Once x reaches 5, the loop stops. More on reddit.com
🌐 r/learnpython
12
6
August 31, 2023
Skip for loop steps in Python like in C++
if i == 3: continue More on reddit.com
🌐 r/learnprogramming
15
0
April 18, 2023
For loop faster than generator expression?
The generator are not inherently faster. The major point is memory save by not saving intermediate values. List comprehension are a different thing. They save a lot of time by building the list as a whole and not doing continuous append. More on reddit.com
🌐 r/Python
39
23
February 10, 2015
Best way for an ever-running loop?
G'day Dirk, There's nothing wrong with what you're doing ... sleep isn't using any CPU time. The only problem is that if foo() takes any significant length of time to run then your schedule will run slightly slow, so if running exactly every 60 seconds matters you probably should do something trickier. I'd also tend to write the 'try' outside the 'while' for clarity: try: while True: foo() time.sleep(60) except KeyboardInterrupt: print('Manual break by user') except OtherThingWhichCanGoWrong: print('Other thing went wrong ... stopping!') just because I think that's clearer. More on reddit.com
🌐 r/Python
14
3
June 24, 2014
🌐
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu › notebooks › chapter05.01-For-Loops.html
For-Loops — Python Numerical Methods
A for-loop assigns the looping variable to the first element of the sequence. It executes everything in the code block. Then it assigns the looping variable to the next element of the sequence and executes the code block again.
🌐
Imperialcollegelondon
imperialcollegelondon.github.io › python-novice-mix › 07-for-loops › index.html
Programming in Python: For Loops
November 8, 2018 - A for loop tells Python to execute some statements once for each value in a list, a character string, or some other collection.
🌐
Real Python
realpython.com › python-for-loop
Python for Loops: The Pythonic Way – Real Python
3 weeks ago - Python’s for loop allows you to iterate over the items in a collection, such as lists, tuples, strings, and dictionaries. The for loop syntax declares a loop variable that takes each item from the collection in each iteration.
🌐
Mimo
mimo.org › glossary › python › for-loop
Python For Loop: Syntax and Examples [Python Tutorial]
Start your coding journey with Python. Learn basics, data types, control flow, and more ... item: The loop variable that takes the current item's value in the sequence during each iteration of the loop. in: The keyword that links the variable to the sequence. sequence: The array, tuple, dictionary, set, string, or other iterable data types to iterate over. Using indentation, the block of code after the for loop statement executes as long as there are items in the sequence.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-for-loops
Python For Loops - GeeksforGeeks
The range() function is commonly used with for loops to generate a sequence of numbers. It can take one, two or three arguments: ... Loop control statements change execution from their normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed. Python supports the following control statements.
Published   2 days ago
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › python › loops-in-python
Loops in Python - GeeksforGeeks
Loops in Python are used to repeat actions efficiently. The main types are For loops (counting through items) and While loops (based on conditions).
Published   1 month ago
🌐
Python
docs.python.org › 3 › library › itertools.html
itertools — Functions creating iterators for efficient looping
2 weeks ago - For example, product(A, B) returns the same as ((x,y) for x in A for y in B). The nested loops cycle like an odometer with the rightmost element advancing on every iteration.
🌐
W3Schools
w3schools.com › python › gloss_python_for_else.asp
Python For Else
Python For Loops Tutorial For Loop Through a String For Break For Continue Looping Through a Range Nested Loops For pass
🌐
Simplilearn
simplilearn.com › home › resources › software development › your ultimate python tutorial for beginners › python for loops - comprehensive guide
Python For Loops - Comprehensive Guide
January 30, 2025 - Learn how to use the 'for loop' in Python with examples. Understand syntax and practical applications for iteration in Python programming.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Code with C
codewithc.com › code with c › python tutorials › python panda 🐼 › looping made simple: python programming for loop explained
Looping Made Simple: Python Programming For Loop Explained - Code With C
March 4, 2024 - Ah, the ‘for’ loop – a loyal companion that marches through each item in a sequence, be it a list, tuple, or range. Its syntax is as sweet as a piece of cake, making it a favorite among Pythonistas. The syntax of the ‘for’ loop is as simple as ABC – no rocket science here!
🌐
Hostinger
hostinger.com › home › tutorials › python’s for loop: usage with examples
How to Use for Loop in Python
October 31, 2024 - Instead of repeating the task for hundreds of times, a for loop in Python can do this automatically. Tell the loop what list to use, and the loop picks the first item, performs the command it’s defined to do, and moves to the next item, repeating the action.
🌐
Reddit
reddit.com › r/learnpython › new to python and i need some help with loops.
r/learnpython on Reddit: New to python and I need some help with loops.
August 31, 2023 -

For the past 2 months I have been doing the Codecademy Python 3 course and last month I went into loops and functions and I was incredibly overwhelmed just like I was in Java. Functions are easy nothing special, at least yet, but loops are made me take that month break.

I know the theory of loops. I think I know what commands should be used where and when but for the love of god I can not write a simple loop, even after countless errors. Could anyone point me in the right direction? I really dont want to quit another language for the same reason.

Edit: a user pointed out that I need to elaborate on "simple loops". For example if I had a list and wanted to print a sentence as many times as the length of the list I know I would use len and range and have the print statement inside the loop but I can't implement it

Top answer
1 of 5
2
Loops allow you to repeat a block of code over and over again. There are two main types of loops in Python - for loops and while loops. For loops are used when you know how many times you want to repeat the code. # This will print the numbers 0 to 4 for i in range(5): print(i) The range(5) part creates numbers from 0 to 4. The for loop will go through each of those numbers, storing them in the variable i, and print them out. While loops are used when you don't know exactly how many times you need to repeat the code. You keep looping as long as some condition is true. # This will print numbers as long as x is less than 5 x = 0 while x < 5: print(x) x = x + 1 This starts x at 0, then keeps looping as long as x is less than 5. Each time, it prints x and then adds 1 to x. Once x reaches 5, the loop stops.
2 of 5
2
There are 3 kinds of loops that i can come up with that doesnt include itertools. 1: you have a loop that continues for x amount of iterations by being specific on how many iterations. This loop doesnt let you do anything with the values themself since they are all ints. Example: for i in range(5): (this will do the block of code 5 times starting from 0 and excluding the 5 and i will only be the integers 0 to 4) 2: you have a loop that will do the same as the first loop but this time its through all values in what you are looping through unless you specify on which values you want to loop through using indecies [start, stop, skip] if its a list or a string that is. Example: for my_value in my_list: (this will go through all the values in our list since we didnt specify what values to go through) 3: a condition based loop that will continue until the condition is met. Example of similar principal as the first loop: while i < 5: i+=1 (this will do the code block until i isnt less than 5, if you dont add to i it will never break since then i can never become not less than 5) So if you want to look through the actual values in something you have go with (2), if you want something to go a set number of times go with (1), if you want something to go until you tell it to stop do (3). If you want to know how to write better loops you should look into best practices for that, it doesnt mean like do tasks but just what to think about when making a loop and other things, there is ofc a bunch of mixed thoughts in what is the best and what not but being the best doesnt really mean anything, if it works it works. For your problem you dont need a loop, you already know len() so if you do print("hello")*len(my_list) it will print the msg as many times as the lenght of the list, but you can ofc use loops too, you can have that as excercise by knowing (1) and (3) and len and range.
🌐
Reddit
reddit.com › r/learnprogramming › skip for loop steps in python like in c++
r/learnprogramming on Reddit: Skip for loop steps in Python like in C++
April 18, 2023 -

Hi all,

I am trying to skip values with an if statement in a for loop in Python and I cannot seem to do it.

What I am trying to do in Python is the following in C++:

[in]:

#include <iostream>

using namespace std;

int main()

{

for (int i = 0;i < 10;i++)

{if (i == 3) i++;

cout<<i;}

}

[out]: 012456789.

When I do this in Python, the for loop does not "skip" value 3, it just prints 4 twice instead of 3 and 4.

I can take this to an extreme and say if i=3: i == 20 and weirdly Python distinguishes between the i inside the for loop and the i with the given value 20 which baffles me.

[in]:

for i in range(10):if i == 3: i = i+1 print(i)

[out] :0 1 2 4 4 5 6 7 8 9

Is there any solution for this?

Thank you in advance for the help!

🌐
Serverspace
serverspace.io › support › help › for-loop-python
Python 3 For Loop Explained: Syntax and Practical Examples
June 1, 2025 - Instead of strictly relying on a fixed number of repetitions, Python’s For loop is designed to iterate over any iterable object such as lists, tuples, strings, or ranges. This means that while you often use a For loop when you know the number of iterations in advance, the loop can also process items one by one in a sequence without explicitly managing a counter.
🌐
Unstop
unstop.com › home › blog › python for loop | the complete guide with multiple examples
Python For Loop | The Complete Guide With Multiple Examples
February 3, 2025 - In the Python language, a for loop is a control flow statement used for iterating over a sequence of elements, i.e., carrying out sequential traversals with ease. It allows you to execute a block of code repeatedly for each item in an iterable ...
🌐
OpenStax
openstax.org › books › introduction-python-programming › pages › 5-2-for-loop
5.2 For loop - Introduction to Python Programming | OpenStax
March 13, 2024 - Use a for loop to implement repeating tasks. In Python, a container can be a range of numbers, a string of characters, or a list of values. To access objects within a container, an iterative loop can be designed to retrieve objects one at a time.
🌐
IONOS
ionos.com › digital guide › websites › web development › python for loop
How to use for loops in Python - IONOS
September 30, 2022 - So, the Python for loop repeatedly executes a block of code. This is also referred to as “iteration”. Loops make it possible to repeat a process for several elements of a sequence.
🌐
BrainStation®
brainstation.io › learn › python › for-loops
Python For Loops (2026 Tutorial & Examples) | BrainStation®
February 4, 2025 - A for loop is the most widely used looping technique for iterating over a list, dictionary, string or a tuple. It repeats a certain logic over and over again for different items unless there are no more items left or an exit-condition is met.