๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_for_loops.asp
Python For Loops
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview 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).
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-for-loop-example
Python for loop | DigitalOcean
March 14, 2024 - We can also use a negative value for our step argument to iterate backwards, but weโ€™ll have to adjust our start and stop arguments accordingly: ... Here, 100 is the start value, 0 is the stop value, and -10 is the range, so the loop begins at 100 and ends at 0, decreasing by 10 with each iteration. This occurs in the output: ... When programming in Python, for loops often make use of the range() sequence type as its parameters for iteration.
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
What's the closest thing to a C++ for loop in Python?

if you playing with your for loop counter variable outside of the loop header, you are doing it wrong, c++ or not.
For loops are for cases that can be clearly defined in advance, it's while loops that are supposed to be more flexible about their control variables.

provide an example where this proves useful and has no cleaner equivalent.

More on reddit.com
๐ŸŒ r/learnpython
29
34
December 5, 2014
[Python] Why would I use a for loop instead of a while loop?

for means "do something with each of these things".

while means "keep doing this until something is no longer true".

So... say what you mean. Readability counts.

More on reddit.com
๐ŸŒ r/learnprogramming
59
49
November 23, 2011
ELI5 Python "for" loops

Do you understand the basic concept of loops? You use a loop when you want to repeat an operation a certain number of times.

You can divide loops up into two different categories if you like (it's not essential). The first one is where you know, before you even hit the loop, exactly how many times you'll need to repeat the operation. If, for example, you want to add the numbers 1-10 together (or even 1-n) then you can compute how many times you'll need to loop before you even do the loop. The second is where you don't know how many times you'll need to loop, but you know the ending condition. An example here would be a program that accepts numbers typed in by the user until the user enters '-1'. Then, stop. How many times are we going to loop? Beats me. Could be 1 time or 1 million times. We'll stop when we stop.

As it turns out, you don't really need to distinguish between these two cases. In both cases it's "Do this this until this ending condition happens", but some languages have special constructs that make it easier to break them up this way.

With me so far?

That's the general idea of loops. There are also some Python specific ideas, which may be what you are getting hung up on. Can you give an example of something that doesn't make sense and maybe we can help you with that.

More on reddit.com
๐ŸŒ r/learnprogramming
4
0
February 12, 2012
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-for-loop-example-how-to-write-loops-in-python
Python For Loop Example โ€“ How to Write Loops in Python
April 26, 2022 - The range() function accepts two arguments, so you can loop through the numbers within the two arguments. Example below: for i in range(1, 10): print(i, end="") # Output: 123456789
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ for-loop
Python for Loop (With Examples)
The continue statement skips the current iteration of the loop and continues with the next iteration. For example, languages = ['Swift', 'Python', 'Go', 'C++'] for lang in languages: if lang == 'Go': continue print(lang)
๐ŸŒ
Learn Python
learnpython.org โ€บ en โ€บ Loops
Loops - Learn Python - Free Interactive Python Tutorial
... # Prints out 0,1,2,3,4 and then it prints "count value reached 5" count=0 while(count<5): print(count) count +=1 else: print("count value reached %d" %(count)) # Prints out 1,2,3,4 for i in range(1, 10): if(i%5==0): break print(i) else: ...
๐ŸŒ
Mimo
mimo.org โ€บ glossary โ€บ python โ€บ for-loop
Python For Loop: Syntax and Examples [Python Tutorial]
In this example, the loop iterates over the list of numbers with a starting value of 1. Each iteration assigns the current value from the sequence of numbers to number. The function call in the code block prints each number in the list. range() is a built-in function that creates a list of ...
๐ŸŒ
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu โ€บ notebooks โ€บ chapter05.01-For-Loops.html
For-Loops โ€” Python Numerical Methods
Alternatively, we could use the item method in a dictionary, and get the key and value at the same time as show in the following example. for key, value in dict_a.items(): print(key, value) ... Note that, we could assign two different looping variables at the same time.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ loops-in-python
Loops in Python - GeeksforGeeks
The break statement in Python brings control out of the loop. ... for letter in 'geeksforgeeks': if letter == 'e' or letter == 's': break print('Current Letter :', letter) ... Explanation: break statement is used to exit the loop prematurely when a specified condition is met. In this example, the loop breaks when the letter is either 'e' or 's', stopping further iteration.
Published ย  1 month ago
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-for-loops
Python For Loops - GeeksforGeeks
Python continue Statement returns the control to the beginning of the loop. ... # Prints all letters except 'e' and 's' for i in 'geeksforgeeks': if i == 'e' or i == 's': continue print(i)
Published ย  2 days ago
๐ŸŒ
Tomasbeuzen
tomasbeuzen.com โ€บ python-programming-for-data-science โ€บ chapters โ€บ chapter2-loops-functions.html
Chapter 2: Loops & Functions โ€” Python Programming for Data Science
An iterable is really just any object with a sequence of values that can be looped over. In this case, we are iterating over the values in a list. word = "Python" for letter in word: print("Gimme a " + letter + "!") print(f"What's that spell?!!
๐ŸŒ
Codecademy
codecademy.com โ€บ learn โ€บ learn-python-3 โ€บ modules โ€บ learn-python3-loops โ€บ cheatsheet
Learn Python 3: Loops Cheatsheet | Codecademy
The range() function can be used to create a list that can be used to specify the number of iterations in a for loop. ... An infinite loop is a loop that never terminates. Infinite loops result when the conditions of the loop prevent it from terminating. This could be due to a typo in the conditional statement within the loop or incorrect logic. To interrupt a Python program that is running forever, press the Ctrl and C keys together on your keyboard.
๐ŸŒ
Serverspace
serverspace.io โ€บ support โ€บ help โ€บ for-loop-python
Python 3 For Loop Explained: Syntax and Practical Examples
June 1, 2025 - We'll write a small programm, the variable n is assigned after it and it is going to print numbers from 6 to 11 and using the print(). ... In programming, indexing of numbers starts with 0, for this reason, as a result, we see 5 different values ...
๐ŸŒ
Data Science Discovery
discovery.cs.illinois.edu โ€บ learn โ€บ Simulation-and-Distributions โ€บ For-Loops-in-Python
For-Loops in Python - Data Science Discovery
Ten cards randomly drawn (with replacement) using a for-loop. When we discussed generating Random Numbers in Python, we explored generating 100 rolls of a six-sided die.
๐ŸŒ
Real Python
realpython.com โ€บ python-for-loop
Python for Loops: The Pythonic Way โ€“ Real Python
3 weeks ago - In this example, color is the loop variable, while the colors list is the target collection. Each time through the loop, color takes on a successive item from colors. In this loop, the body consists of a call to print() that displays the value ...
๐ŸŒ
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.
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ ForLoop
ForLoop
When you have a block of code you ... 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))...
๐ŸŒ
IBM
ibm.com โ€บ reference โ€บ python โ€บ for-loop
What is a for loop in python? | IBM
November 21, 2025 - They work well for iterating over sequences (like lists, tuples, strings or ranges) and performing an action for each element. For example, if you want to iterate through a list of items or repeat an action a specific number of times, a for loop is ideal. It's concise and less prone to errors, especially off-by-one errors, because the iteration mechanics are handled by Python itself.
๐ŸŒ
OpenStax
openstax.org โ€บ books โ€บ introduction-python-programming โ€บ pages โ€บ 5-2-for-loop
5.2 For loop - Introduction to Python Programming | OpenStax
March 13, 2024 - The range() function is a common approach for implementing counting in a for loop. A range() function generates a sequence of integers between the two numbers given a step size. This integer sequence is inclusive of the start and exclusive of the end of the sequence. The range() function can take up to three input values. Examples are provided in the table below.
๐ŸŒ
Imperialcollegelondon
imperialcollegelondon.github.io โ€บ python-novice-mix โ€บ 07-for-loops โ€บ index.html
Programming in Python: For Loops
November 8, 2018 - Trace the execution of a simple (unnested) loop and correctly state the values of variables in each iteration. Write for loops that use the Accumulator pattern to aggregate values.