๐ŸŒ
W3Schools
w3schools.com โ€บ Python โ€บ python_for_loops.asp
Python For Loops
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp 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).
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ for-loop
Python for Loop (With Examples)
Here, when lang is equal to 'Go', the break statement inside the if condition executes which terminates the loop immediately. This is why Go and C++ are not printed. 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)
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
Can not understand for loops
You're following a recipe for potato soup. It says "wash and peel five potatoes." But you only know how to wash and peel one potato. So you wash and peel one potato, then another, then another, until you've done it to five potatoes. That's a for loop: "for each of five potatoes, wash the potato, then peel it." More on reddit.com
๐ŸŒ r/learnpython
90
262
August 14, 2021
For Loops - Best Ways to learn.
What's the difficulty exactly? The basic idea is you've got a bucket of items, and you want to take each one one at a time and do something with it, until you've gone through all the items. Think of it like "for each item in bucket", where the "each" is silent/invisible. Example: bucket = [1, 2, 3] for item in bucket: print(item) Sometimes you don't care about the actual items in the bucket; you just want to use them as a counter to make the loop run X number of times. Then it's common to use range() as a convenient way to make a bucket of a given size. for item in range(3): print('Huzzah!') The bucket can be anything that's an "iterable". So, lists, tuples, strings, dictionaries, generators, etc. If an item drawn from the bucket has multiple parts, you can use "tuple unpacking" to reference them individually. bucket = ['a', 'b', 'c'] for index, value in enumerate(bucket): print(index, value) More on reddit.com
๐ŸŒ r/learnpython
6
7
July 23, 2023
Understanding the For Loop
range() generates numbers. If you only pass 1 argument range(5) it will start from 0 and keep going up, and stop before hitting 5. I know sounds strange but that's how it works. So range(5) gives you 0 1 2 3 4 If you pass 2 arguments, range(1,5), same as above except this time it starts from your first argument, 1. So you get 1 2 3 4 The 3rd argument is how each steps increases. By default it's 1. Let's pass a 2 and see what happens? range(1, 10, 2) --> 1 3 5 7 9. Starts from 1, goes up 2 each time, stops before 10. More on reddit.com
๐ŸŒ r/learnpython
62
366
October 9, 2020
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ loops-in-python
Loops in Python - GeeksforGeeks
A for loop can also iterate through sequence elements using their index values with the help of range() and len().
Published ย  June 11, 2026
๐ŸŒ
Berkeley
pythonnumericalmethods.studentorg.berkeley.edu โ€บ notebooks โ€บ chapter05.01-For-Loops.html
For-Loops โ€” Python Numerical Methods
s = 0 a = [2, 3, 1, 3, 3] for i in a: s += i # note this is equivalent to s = s + i print(s) ... The Python function sum has already been written to handle the previous example. However, assume you wish to add only the even numbers. What would you change to the previous for-loop block to handle ...
๐ŸŒ
Imperialcollegelondon
imperialcollegelondon.github.io โ€บ python-novice-mix โ€บ 07-for-loops โ€บ index.html
Programming in Python: For Loops
November 8, 2018 - Doing calculations on the values in a list one by one is as painful as working with pressure_001, pressure_002, etc. A for loop tells Python to execute some statements once for each value in a list, a character string, or some other collection.
๐ŸŒ
Python Software Foundation
wiki.python.org โ€บ python โ€บ ForLoop.html
For loops - Python Wiki
While loop from 1 to infinity, therefore running forever. x = 1 while True: print("To infinity and beyond! We're getting close, on %d now!" % (x)) x += 1 ยท When running the above example, you can stop the program by pressing ctrl+c at the same time. As you can see, these loop constructs serve different purposes.
๐ŸŒ
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?!!
Find elsewhere
๐ŸŒ
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: ...
๐ŸŒ
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.
๐ŸŒ
Python
wiki.python.org โ€บ moin โ€บ ForLoop
ForLoop
While loop from 1 to infinity, therefore running forever. x = 1 while True: print("To infinity and beyond! We're getting close, on %d now!" % (x)) x += 1 ยท When running the above example, you can stop the program by pressing ctrl+c at the same time. As you can see, these loop constructs serve different purposes.
๐ŸŒ
IBM
ibm.com โ€บ reference โ€บ python โ€บ for-loop
What is a for loop in python? | IBM
November 21, 2025 - 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 ...
๐ŸŒ
PYnative
pynative.com โ€บ home โ€บ python exercises โ€บ python loops exercises: 40+ coding problems with solutions
40 Python Loops Coding Exercises with Solutions โ€“ PYnative
June 13, 2026 - Donโ€™t forget to increment i inside the loop. ... # Initialize the counter i = 1 # Iterate until i is greater than 10 while i <= 10: print(i) # Increment the counter to move to the next number i += 1Code language: Python (python) Run
๐ŸŒ
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 ...
๐ŸŒ
Coursera
coursera.org โ€บ tutorials โ€บ for-loop-python
How to Use For Loops in Python: Step by Step | Coursera
In the example above where the loop variable is a kitten, the sequence variable is box_of_kittens because it represents the grouping the single variable is chosen from. ... Write your loop statements in an indented block. The indentation lets Python know which statements are inside the loop and which statements are outside the loop. ... 1 2 # Write a for loop that prints the numbers from 1 to 10, inclusive.
๐ŸŒ
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.
๐ŸŒ
Dataquest
dataquest.io โ€บ home โ€บ blog โ€บ tutorial: advanced python for loops
Tutorial: Advanced Python for Loops
March 11, 2025 - In the above example, our if statement presents the condition that if our variable i evaluates to 7, our loop will break, so our loop iterates over integers 0 through 6 before dropping out of the loop entirely. Looking for more? Here are some additional resources that might be useful: Python Tutorials โ€” Our ever-expanding list of Python tutorials for data science.
๐ŸŒ
LinkedIn
linkedin.com โ€บ pulse โ€บ 25-python-loop-coding-questions-mrityunjay-pathak
25 Python Loop Coding Questions
August 4, 2023 - Let's get started โ†“ Print numbers from 1 to 10 using a for loop: for num in range(1, 11): print(num) Explanation: The for loop iterates over the range(1, 11) which generates numbers from 1 to 10 (inclusive) and prints each number
๐ŸŒ
Python Examples
pythonexamples.org โ€บ python-for-loop-example
Python For Loop - Syntax, Examples
Python For Loop can be used to iterate a set of statements once for each item, over a Range, List, Tuple, Dictionary, Set or a String. Example for each of the collection with for loop is provided.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_for_else.asp
Python For Else
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... Note: The else block will NOT be executed if the loop is stopped by a break statement. Break the loop when x is 3, and see what happens with the else block: for x in range(6): if x == 3: break print(x) else: print("Finally finished!") Try it Yourself ยป