How does a for loop know to increment - python

When defining a Fibonacci sequence the following code was used:
def fibonacci(n):
current=0
after=1
for i in range(1,n):
current,after=after,current+after
return after
How does the for loop know to increment by one every time we pass through? I tried using a while loop while e<=n: and it returned an error as I hadn't defined e.

A for loop in python iterates through items or generators. In your example, range(1,n) will return a list of elements between [1, n) in python2, or a generator that will yield the same items in python3.
Essentially, a for loop can iterate any kind of iterables:
for item in [1, 6, 8, 9]:
print(item)
It will print 1, 6, 8 and 9.
Using range, there is a 3rd optional parameter which allows you to specify the increment:
range(1, 10, 1) # The defaut [1 .. 9] numbers
range(1, 10, 2) # From 1 until 9 with 2 of increment

for loop does not increment, instead it iterates
The for loop does not increment, instead it is asking so called iterable to provide values to work with.
In your example, the iterable is range(1,n).
In Python 2.x, the range(1, n) creates a list of values and the for loop is only asking the list to provide next value in each run, until the list gets exhausted from values.
In Python 3.x the range(1, n) is optimized and returns special iterable type <range>, which when asked by for loop to provide next value provide it.

By default if you don't specify your step i.e for i in range (start,stop,step) the increment is considered as one otherwise it's your step that you specified.

Related

python range and for loop understanding

I am new to python / coding and looking to understanding the range function more and how it is used in conjunction with the "for" loop.
So I know that the range function takes in 3 parameters: range(start, stop, step). For the below function, I passed in my array called test_numbers, then created a "for" loop where "i" starts counting from len(list_of_numbers)-1 (to give me the index values)
I was expecting the result for "i" to print 0,1,2,3,4,5 but it only printed 5 for "i". I am wondering why is that? If I put 6 as the "stop" argument, would it not just print from range start of the length of the array as in [0,1,2,3,4,5] all the way then stop before 6? that is my confusion. Any help /explanation would be great!
test_numbers = [1,2,4,5,6]
def testRange(list_of_numbers):
for i in range(len(list_of_numbers), 6):
print(i)
testRange(test_numbers)
The result: 5
Was expecting: 0,1,2,3,4,5
When you call range() with two arguments, the first argument is the starting number, and the second argument is the end (non-inclusive). So you're starting from len(list_of_numbers), which is 5, and you're ending at 6. So it just prints 5.
To get the results you want, the starting number should be 0, and the end should be len(list_of_numbers)+1. If you call it with one argument, that's the end, and 0 is the default start. So use
for i in range(len(list_of_numbers)+1):
or if you want to pass the start explicitly:
for i in range(0, len(list_of_numbers)+1):
range gives you and iterator between (start, end) end not included.
So in your case the iterator is (start=len(list_of_numbers), end=6).
Since len(list_of_numbers) = 5, this translates to range(5,6) which is 1 element, 5, since 6 is excluded.
https://docs.python.org/3/library/functions.html#func-range
range stop parameter is exclusive and start is inclusive - if you provided len result (of 5) and 6 as the stop then the range result will contain only one element [5]
If you'd like to have 0..6 you should use
range(0, 6)
or, what you probably want to do to iterate over all array indices
range(0, len (list_of_numbers))
The simplest way is just iterate throught list, beacause it is iterable:
test_numbers = [1,2,4,5,6]
def testRange(list_of_numbers):
for i in list_of_numbers:
print(i)
testRange(test_numbers)

Why does this reversed for loop miss the last item?

I want to reverse loop through a table and join the table items to make a string. This code works fine but it misses the last item of the table :
t = [0, 0, 2, 6, 14, 4, 7, 0]
for i in range(len(t) - 1, 0, -1):
res = str(t[i]) + res
return res
It prints 02614470 instead of 002614470.
I know if I change 0 to -1 in the loop parameter it would work properly but I want to understand why. It seems that when I want to use -1 as step, the middle parameter (0 in this case ) adds +1. So for example if I want the loop to stop at index 1 I have to write 0 in the parameter. Is that right?
That's my thought process but I don't know if it's correct.
The typical construction of a for loop with range() is:
t=[0,0,2,6,14,4,7,0]
for i in range(0,len(t)):
print(f"{i}, {t[i]}")
This makes sense to iterate through a list of items which starts at zero and is len() long. Since we start at zero, we have to stop at one less than the value returned for len(t), so range() is built to accommodate this most common case. As you noted in your case, since you are reversing this you would have to iterate through and use a -1 to capture the zero'th index in the list. Fortunately, you can use the following syntax to reverse the range, which leads to better readability:
t=[0,0,2,6,14,4,7,0]
for i in reversed(range(0,len(t))):
print(f"{i}, {t[i]}")
The second parameter in the range is a stop value so it is excluded from the generation.
for example, when you do range(10), Python processes this as range(0,10) and yields values 0,1,2,...,7,8,9 (i.e. not 10)
Going in reverse, if you want zero to be included, you have to set the stop value at the next lower value past zero (i.e. -1)
Other answers have explained that range does not include the end number. It always stops one short of the end, whether the range is forward or backward.
I'd like to add some more "Pythonic" ways to write the loop. As a rule of thumb try to avoid looping over list indices and instead loop over the items directly.
things = [...]
# Forward over items
for thing in things:
print(thing)
# Backward over items
for thing in reversed(things):
print(thing)
If you want the indices use enumerate. It attaches indices to each item so you can loop over both at the same time. No need for range or len.
# Forward over items, with indices
for i, thing in enumerate(things):
print(i, thing)
# Backward over items, with indices
for i, thing in reversed(enumerate(things)):
print(i, thing)

Python: Why for loop is behaving weirdly?

I am just trying to merge two sorted lists into one sorted list. I know it is a simple task and plenty solutions online, but my question is different. Here's my code:
def merge(list1, list2):
len1 = len(list1)
len2 = len(list2)
list3 = []
pointer = 0
for i in range(len1):
if (list1[i] >= list2[pointer]):
while (pointer < len2 and list1[i] >= list2[pointer]):
list3.append(list2[pointer])
pointer += 1
i -= 1
else:
list3.append(list1[i])
while (pointer < len2):
list3.append(list2[pointer])
pointer += 1
return list3
if __name__ == "__main__":
print(merge([1, 2, 3, 10, 11, 22], [4, 5, 6, 7, 20, 21, 30]))
I did debugging and I was confused to see that when I decrease the value i by 1, for example from 3 to 2, on the next iteration it goes back to 4. I have no idea why? You can check it by running the code and seeing the result. I just need explanation why that is happening. Thanks
I was confused to see that when I decrease the value i by 1, for example from 3 to 2, on the next iteration it goes back to 4. I have no idea why?
Because for i in range(x) means "execute the for body with i assuming the values of 0 through x-1". Assigning a different value to i does not affect its value in the next iteration.
In other words, for i in range(10) is not a translation of C's or JavaScript's for (i = 0; i < 10; i++). Instead, you can think of it as for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Seen like that, it is clear that changing one value of i will not affect the subsequent value, which is blindly taken out of a pre-generated list. If you need to modify the iteration progress based on changing conditions, you can write the C/JS-style loop explicitly:
i = 0
while i < len1:
# ... loop body goes here ...
i += 1
Written like this, modifying i in loop body will affect iteration in the way you expected.
You are editing i inside the for loop that runs on i. I don't believe it will work the way you intend it.
Also, you can simply merge the lists and sort the outcome with this:
list1 = [1,2,3,10,11,22]
list2 = [4,5,6,7,20,21,30]
list3 = list1 + list2
list3.sort()
print(list3)
Hope this helps.
This is because range() is a generator function. It does not create the list of numbers, as you might expect, but generates a new number as you need it. And, even if it created the list, the numbers would be taken from the list, one after the other, regardless how you modify them. You can think of the result of range() in a sense as 'read-only'. user4815162342 below is right, you should not confuse it with a C-style loop. More like a Fortran loop, where the number of iterations is computed in advance.
From https://pynative.com/python-range-function/:
Python 3’s range uses the generator. Python 3’s range() will produce value when for loop iteration asked for it. i.e., it The range() doesn’t produce all numbers at once.
Python range() function returns an immutable sequence object of integers, so it is possible to convert range() output to the Python list. Use list class to convert range output to list. Let’s understand this with the following example.

Infinite loops using 'for' in Python [duplicate]

This question already has answers here:
Is there an expression for an infinite iterator?
(7 answers)
Closed 5 years ago.
Why does this not create an infinite loop?
a=5
for i in range(1,a):
print(i)
a=a+1
or this
for i in range(1,4):
print(i)
i=i-1
or this
for i in range(1,4):
print(i)
i=1
Is there any way we can create infinite loops using a for loop? I know there is the while loop for that but I was just curious.
range is a class, and using in like e.g. range(1, a) creates an object of that class. This object is created only once, it is not recreated every iteration of the loop. That's the reason the first example will not result in an infinite loop.
The other two loops are not infinite because, unlike the range object, the loop variable i is recreated (or rather reinitialized) each iteration. The values you assign to i inside the loop will be overwritten as the loop iterates.
Consider a for loop:
for item in iterable:
print(item)
The idea is that as long as iterable is unchanged, we will loop through each and every item inside iterable once. For example,
for item in [3, 2, 1, 666]:
print(item)
will output 3 2 1 666. In particular, we find that range(1, 4) is a easy way to represent an iterable [1, 2, 3]. Thus,
for i in range(1, 4):
print(i)
will output 1 2 3.
Example 1
a=5
for i in range(1,a):
print(i)
a=a+1
In this case, range(1,a) is evaluated once, when the loop begins.
Example 2
for i in range(1,4):
print(i)
i=i-1
In this case, i is reevaluated every loop, before executing the print and i=i-1 statements within the body of the loop.
Example 3
for i in range(1,4):
print(i)
i=1
Just like Example 2, i is reevaluated every loop.
You can't, in this case, update the iterator that your for loop is looping over.
The range in for i in range(a): is actually a function - it takes a value, a, and returns an object that contains the values that it will loop through. Once you've built that object you can change the input variable as much as you'd like, and that object won't change.
Imagine if we made our own similar function called my_range that generates a list (whereas the built in range function generates a range):
def my_range(end):
my_list = []
for i in range(end):
my_list.append(i)
return my_list
Now if we were to use our new function, like so:
a = 4
for i in my_range(a):
print(i)
a += 1
It'd be obvious that we can't update the list object that we're looping over by changing a, because the list that we're looping over has already been made, and isn't being remade on every loop.
Can you make an infinite loop in python? Yes, just add a new entry to the object that you're looping through, e.g.:
my_list = [0]
for i in my_list:
print(i)
my_list.append(i+1)
Now we're updating the object that we're looping over.
for loops and the range(..) object
If you write for i in range(..): Python does not translate this into something like for(int i = 0; i < n; i++) (in the C-programming language family).
Furthermore the range object is constructed once, before the for loop. The range(..) object, does not know which variables have been used to construct it. Once constructed, the range is fixed.
It sees range(..) as an iterable object, and each iteration, it takes the next item the iterable yields. So whether you set the variable or not in the for loop, has no effect for the next iteration.
In python-2.x, range(..) is not a specific object, but a call to construct a list. So if you call range(10) (without the for loop), you get [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].
Why it does not work?
So then why does the examples do not work?
a=5
for i in range(1,a):
print(i)
a=a+1
Here we construct range(..) once. After that, the variables based on which it was constructed can change, since the range(..) object does change anymore. Incrementing a thus will not mean the range object will get larger.
for i in range(1,4):
print(i)
i=i-1
The for loop each time takes the next item of the iterable. So if we first have collected 1 from the range loop, the next iteration, we collect 2. This is regardless what the value of i is.
for i in range(1,4):
print(i)
i=1
For the very same reason: for does not take into account the previous value of i. It only fetches the next item the iterable (here range(..) yields). Since range(..) is fixed, it will simply feed the for loop the next item.
Emulating an infinite loop
So we need to construct an iterable that keeps yielding elements. A way to do this is itertools.count:
from itertools import count
for i in count():
# ...
pass
Or in case you are not interested in any value, we can use repeat as well:
from itertools import repeat
for _ in repeat(None):
# ...
pass
range copies the parameters given to it for internal use. So changes to those afterwards have no effect. Same as with the loop variable, which is only created from the internal values every time.
That's different though if you use a mutable object like a list to iterate over:
a = [1,2,3]
for i in a:
a.append(i)
This loop will indeed run infinitely.
Because a range is either a list (Python2) or a range object both of which are finite. That range is created once before the loop starts. Your loop variable is assigned the next element of the range at the beginning of each iteration, regardless of what you assign it later in the loop body. You need an infinite iterator for an infinite for loop, e.g. itertools.cycle:
from itertools import cycle
for x in cycle(range(5)):
# endless

How to use more than one condition in Python for loop?

How to use more than one condition in Python for loop?
for example in java:
int[] n={1,2,3,4,6,7};
for(int i=0;i<n.length && i<5 ;i++){
//do sth
}
How dose the python for loop do this?
The Python for loop does not, itself, have any support for this. You can get the same effect using a break statement:
n = [1, 2, 3, 4, 6, 7]
for i in n:
if i >= 5:
break
# do something with i
In Python, a for is really a foreach that iterates over some "iterator" or some "iterable object". This is even true when you just want to repeat a specific number of times:
for i in range(1, 8):
# do something with i
In Python 2.x, the above for loop builds a list with the numbers 1 through 7 inclusive, then iterates over the list; in Python 3.x, the above loop gets an "iterator object" that yields up the values 1 through 7 inclusive, one at a time. (The difference is in the range() function and what it returns. In Python 2.x you can use xrange() to get an iterator object instead of allocating a list.)
If you already have a list to iterate over, it is good Python to iterate over it directly rather than using a variable i to index the list. If you still need an index variable you can get it with enumerate() like so:
n = [3, 5, 10, "cat", "dog", 3.0, 4.0] # list can contain different types
for i, value in enumerate(n):
# we only want to process the first 5 values in this list
if i >= 5:
break
# do something with value
EDIT: An alternate way to solve the above problem would be to use list slicing.
for value in n[:5]:
# do something with value
This works if n is a list. The for loop will set value to successive items from the list, stopping when the list runs out or 5 items have been handled, whichever comes first. It's not an error to request a slice of longer length than the actual list.
If you want to use the above technique but still allow your code to work with iterators, you can use itertools.islice():
from itertools import islice
for value in islice(n, 5):
# do something with value
This will work with a list, an iterator, a generator, any sort of iterable.
And, as with list slicing, the for loop will get up to 5 values and it's not an error to request an islice() longer than the number of values the iterable actually has.
The direct equivalent of your Java code is a while loop:
n = [1, 2, 3, 4, 6, 7]
i = 0
while i < len(n) and i < 5:
# do sth
i += 1
You could also do:
n = [1, 2, 3, 4, 6, 7]
for x in n[:5]:
# do sth
Here is one way to have two or more conditions with the for loop, which is what the question actually asks. The point I am trying to make is that it can be done, but isn't very pythonic and it's generally not a good idea to try to rewrite code from another language line by line.
from itertools import takewhile, count
n=[1,2,3,4,6,7]
for i in takewhile(lambda i:i<len(n) and i<5, count()):
print(i)
You can write a while loop with the same sort of logic (but in Python, && is spelled and, and || is spelled or); or you can use a for loop that iterates over a sequence and then add extra logic to break out of the loop.
In Python, we prefer not to use integers to index into a container. This would actually break a lot of our code, because we do not check the data types at compile-time, and some containers cannot be indexed into. Wanting to index into a container is already a design smell: the fact that everything is in a sequential container is supposed to mean that none of the elements are "special", or we would have kept them separate. Iteration is iteration, and we should not go out of our way to make it more complicated.
Assuming that you want the ith item of n somewhere in the loop, enumerate saves us from typing n[i] all over the place - the value will be stored in the variable item
n = [1,2,3,4,6,7]
for i, item in enumerate(n):
if i>=5:
break
# do something
print item # for example
Note that the loop will terminate automatically if the length of n is less than 5
Python's for is not like the for in languages based on C syntax. In Python, for iterates over a sequence, whereas in C it loops while a condition is true. This is a profound difference.
The C-like for can be replaced roughly with the following code:
i = 0;
while (i < n.length && i < 5) {
// do sth
i++;
}
(There are some complications from break and continue, but let's ignore those for now.)
This rewrite also indicates a way to do what you want in Python: use while:
i = 0
while i < len(n) and i < 5:
// do something
i += 1
In your particular case, however, it is easiest to use for with a suitable list of indexes:
for i in range(min(len(n), 5)):
// do something
range will return a list of integers (0, 1, 2, ...) and what you want is to have the list go up to 5, or the length of your array, whichever is smaller. The above code achieves that.
The for statement in Python iterates a "list" of objects (I put list in quotes because I mean it in the generic sense, it can iterate over anything that is iterable).
To code a conditional loop (rather than iterating until completion), use while:
n = [1, 2, 3, 4, 6, 7]
i = 0
while i < len(n) and i < 5:
# do stuff
i += 1
Now just to be complete, your example could also be written as:
n = [1, 2, 3, 4, 6, 7]
for i in range(0,min(len(n),5)):
# do stuff
or:
n = [1, 2, 3, 4, 6, 7]
for i in range(0,len(n)):
if i >= 5:
break
# do stuff

Categories

Resources