for the code,

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

the output is going to be,

0
1
2
4
5
6
7
8
9

Breaking down the code,

for i in range(0, 10)

for loop runs for i=0 to i=9, each time initializing i with the value 0 to 9.

if i == 3:
 i = i + 1
 continue
print(i)

when i = 3, above condition executes, does the operation i=i+1 and then continue, which looks to be confusing you, so what continue does is it will jump the execution to start the next iteration without executing the code after it in the loop, i.e. print(i) would not be executed.

This means that for every iteration of i the loop will print i, but when i = 3 the if condition executes and continue is executed, leading to start the loop for next iteration i.e. i=4, hence the 3 is not printed.

Answer from developer6811 on Stack Overflow
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › how to increment for loop in python
How to Increment For Loop in Python - Spark By {Examples}
May 31, 2024 - The loop variable i takes on these values, and they are printed in each iteration. # Increment the value using slicing list=[10, 20, 50, 30, 40, 80, 60] for i in list[0::3]: print(i) # Output: # 10 # 30 # 60 · You can use list comprehension to create a list of values where each value is obtained by dividing the corresponding value from the range by y.
🌐
TutorialKart
tutorialkart.com › python › python-for-loop › python-for-loop-increment-step
Python For Loop Increment in Steps
July 22, 2021 - To iterate through an iterable in steps, using for loop, you can use range() function. range() function allows to increment the “loop index” in required amount of steps. In this tutorial, we will learn how to loop in steps, through a collection ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › specifying-the-increment-in-for-loops-in-python
Specifying the increment in for-loops in Python - GeeksforGeeks
July 15, 2025 - Let us see how to control the increment in for-loops in Python. We can do this by using the range() function.
🌐
GeeksforGeeks
geeksforgeeks.org › python › ways-to-increment-iterator-from-inside-the-for-loop-in-python
Ways to increment Iterator from inside the For loop in Python - GeeksforGeeks
April 22, 2020 - In Python, a for loop iterates over an iterator object rather than using a manual counter. As a result, modifying the loop variable inside the loop does not affect iteration, and controlled stepping must be handled explicitly. Example: Why Incrementing the Loop Variable Does Not Work
🌐
The Geek Stuff
thegeekstuff.com › 2017 › 07 › python-for-loop-examples
12 Essential Python For Loop Command Examples
July 11, 2017 - In range function inside for loop, we can also specify negative values. In the example below, we are using negative numbers for end-value (-5) and increment-value (-2). ... As you see from the above output, it sequence started from the start-value (which is 4), and then increment the next number by the increment-value (which is -2), and keeps going all the way through end-value-1. You can use “continue” statement inside python for loop.
Find elsewhere
Top answer
1 of 5
22

It seems that you want to use step parameter of range function. From documentation:

range(start, stop[, step]) This is a versatile function to create lists containing arithmetic progressions. It is most often used in for loops. The arguments must be plain integers. If the step argument is omitted, it defaults to 1. If the start argument is omitted, it defaults to 0. The full form returns a list of plain integers [start, start + step, start + 2 * step, ...]. If step is positive, the last element is the largest start + i * step less than stop; if step is negative, the last element is the smallest start + i * step greater than stop. step must not be zero (or else ValueError is raised). Example:

 >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 >>> range(1, 11) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 >>> range(0, 30, 5) [0, 5, 10, 15, 20, 25]
 >>> range(0, 10, 3) [0, 3, 6, 9]
 >>> range(0, -10, -1) [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
 >>> range(0) []
 >>> range(1, 0) []

In your case to get [0,2,4] you can use:

range(0,6,2)

OR in your case when is a var:

idx = None
for i in range(len(str1)):
    if idx and i < idx:
        continue
    for j in range(len(str2)):
        if str1[i+j] != str2[j]:
            break
    else:
        idx = i+j
2 of 5
11

You might just be better of using while loops rather than for loops for this. I translated your code directly from the java code.

str1 = "ababa"
str2 = "aba"
i = 0

while i < len(str1):
  j = 0
  while j < len(str2):
    if not str1[i+j] == str1[j]:
      break
    if j == (len(str2) -1):
      i += len(str2)
    j+=1  
  i+=1
🌐
Python Guides
pythonguides.com › increment-and-decrement-operators-in-python
Increment and Decrement Operators in Python
September 2, 2025 - This code shows how to increment ... way to iterate over a sequence of items without the need for a separate increment operation. total_cars = 0 for x in range(10): total_cars += 1 print("Total number of cars passed through the toll booth:", total_cars)...
🌐
W3Schools
w3schools.com › python › python_for_loops.asp
Python For Loops
The range() function defaults to increment the sequence by 1, however it is possible to specify the increment value by adding a third parameter: range(2, 30, 3): ... Note: The else block will NOT be executed if the loop is stopped by a break ...
🌐
Python
wiki.python.org › moin › ForLoop
ForLoop - Python Wiki
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))
🌐
Tutorial Gateway
tutorialgateway.org › python-while-loop
Python While Loop
March 25, 2025 - If the condition result is true, the number adds to the total. Otherwise, it will exit from the Python while loop. We also used the + operator to increment the number value (number = number +1).
🌐
Alma Better
almabetter.com › bytes › tutorials › python › for-loop-in-python
For Loop in Python
December 13, 2023 - For example, range(5, 10) would create a sequence of numbers from 5 to 9. range(start, increment, stop): Creates a sequence of numbers from the given start number up to the given stop number, incrementing the numbers by the given increment. For example, range(6, 2, 21) would create a sequence of numbers from 6 to 19, incrementing the numbers by 2. ... A Python for Loop with an else statement executes a block of code multiple times, and the else statement runs after the Loop finishes all iterations.
Top answer
1 of 2
2

range gives you an iterable object:

>>> range(10,20 , 2)
range(10, 20, 2)
>>> list(range(10,20 , 2))
[10, 12, 14, 16, 18]

The values in it are fully decided as soon as the call returns, and aren't re-evaluated each time around the loop. Your step only goes up to 337 because you are incrementing it once for each element in the object range(0, 1000, 3), which has 334 items, not 1000:

>>> len(range(0,1000,3))
334

To get something that works like range but advances the step, you would need to write your own generator:

def advancing_range(start, stop, step):
    ''' Like range(start, stop, step) except that step is incremented 
        between each value 
    '''
    while start < stop:
        yield start
        start += step
        step += 1

You can then do for i in advancing_range(0, 1000, 3): and it will work as you intend.

But this is a very strange thing to want to do. Judging by your variable names, I would guess you're coding the locker problem, which says:

A new high school has just been completed. There are 1,000 lockers in the school and they have been numbered from 1 through 1,000. During recess (remember this is a fictional problem), the students decide to try an experiment. When recess is over each student will walk into the school one at a time. The first student will open all of the locker doors. The second student will close all of the locker doors with even numbers. The third student will change all of the locker doors that are multiples of 3 (change means closing lockers that are open, and opening lockers that are closed.) The fourth student will change the position of all locker doors numbered with multiples of four and so on. After 1,000 students have entered the school, which locker doors will be open, and why?

But the advancing range logic says something more like "the first student opens the first locker, then the second opens the second locker after that, then the third student opens the third locker after that ...". You want to affect multiple lockers each time, but further spaced out. Essentially, you want to copy and paste your first two loops another 998 times with a one higher step each time. Of course, you can do better than copy and paste, and this seems like you want two nested loops, where the outer one advances the step that the inner one uses. That would look like this:

for step in range(1, len(lockers)):
    for i in range(step, len(lockers), step):

Simplifying your other logic by using booleans instead of 1 and 0, the whole program looks like this:

lockers = [True] * 1000

for step in range(1, len(lockers)):
    for i in range(step, len(lockers), step):
        lockers[i] = not lockers[i]

print(sum(lockers))

It prints that the number of open lockers is 969.

2 of 2
0

If you want to adjust the step size while iterating, you can have an own range object:

class AdjustableRange(object):
    def __init__(self, start, stop, step):
        self.start = start
        self.stop = stop
        self.step = step
        self.value = None
    def __iter__(self):
        if self.value is None:
            self.value = start
        while self.value < self.stop:
            yield self.value
            self.value += self.step

This (untested) one you can use for iterting like

rg = AdjustableRange(0, len(lockers), step):
for i in rg:
    if lockers[i] == 0:
        lockers [i] = 1
    else:
        lockers[i] = 0
    rg.step += 1 # this influences the iteration

But, as was already said, there are better ways to solve your "real" problem.

🌐
CodeGym
codegym.cc › java blog › learning python › increment and decrement in python
Increment and Decrement in Python
November 11, 2024 - Here’s an example of using a for loop to increment a variable. ... In this example, range(3) generates numbers from 0 to 2. Each iteration, the loop variable i is incremented automatically. Just like incrementing, Python lacks the -- operator for decrementing.
🌐
Python Course
python-course.eu › python3_for_loop.php
20. For Loops | Python Tutorial | python-course.eu
This statement is the one used by C. The header of this kind of for loop consists of a three-parameter loop control expression. Generally it has the form: for (A; Z; I) A is the initialisation part, Z determines a termination expression and I is the counting expression, where the loop variable is incremented or dcremented.
🌐
Thinglabs
thinglabs.io › python-increment-by-1-a-guide-to-simple-counting-in-code
Python Increment by 1: A Guide to Simple Counting in Code - thinglabs
November 7, 2024 - ... This loop continues running and incrementing count until count is equal to 5. Each iteration increases count by 1 using the += operator, which is the standard way to increment a value in Python.
🌐
Codecademy
codecademy.com › forum_questions › 55dfe28086f552612b00043f
how do i increment for loop in powers of 2? | Codecademy
Submitted by bitPro70622 · over ... · The code is fine apart from the for loop line. Read that line again. for i in range(0, len(my_list), 2**i) means to take the next value from the range and then assign it to i. So first, ...