for i in range(0,10,-1):
print (i)
Why the above program prints nothing ,i expect it to print at least 0
According to
"for i in range(start, end, iterator)" definition ,it evaluates first element and then uses iterator to get to next element.
So in theory the Example code snippet should first take 0 and print it and then next element is evaluated as -1 which is not in 0-10 then it should bail out
With a negative "step", python keeps on yielding1 elements while the current value is greater than end. In this case, you start at 0. 0 is not greater than or equal to 10 so python's done and nothing gets yielded.
1This is a simplification of course -- range returns a range object on python3.x which is an indexable sequence type so it doesn't exactly yield, but the basic idea is the same ...
There is no evaluation of the first element by the range() call, and Python's range() function will not return anything if step is negative and start + i * step is not greater than stop. For your example, start = 0 + 0 * -1 is not greater than stop = 10, so your range call returns the empty list, and your for loop has nothing to iterate over.
$ python -c 'print(range(0,10,-1))'
[]
range()'s documentation:
range(stop)
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:
Third argument in range is step
In range you can give step as 1, 2 etc.
When you give -1, it will not do step in reverse.
If you want to print reverse order you can try
>>> range(10)[::-1]
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
In Python, the range function works with the arguments range(StartingValue, EndingValue, Step) the problem you have is that you are assigning a negative step to a situation where the StartingValue is less than EndingValue. Since this is the case, it never enters the loop, because the end value has already been reached, and exceeded.
To fix this, just reverse the first two values: for I in range(10,0,-1) . Think of it as if you were saying it in a sentence, such as FOR each ITEM in the RANGE of 10 to 0 decreasing by 1
Related
string = input("Enter a string: ")
for i in range(len(string)-1, -1, -1):
print(string[i], end="")
I am confused in this start, stop, step not sure why it is -1 in the stop bit - I tried putting 0.
range(start, stop, step)
start: It is optional. default value is 0.
stop: It is required although the number defined in stop is not included [exclusive]
step: It is optional. default value is 1
for i in range(len(string)-1, -1, -1):
This will iterate from last character to first character
for i in range(len(string)-1, 0, -1):
This will iterate from last character to second character
For better understanding:
Ex- range(1,5) -> 1,2,3,4 #5 is not included.
Ex- range(-9,-5) -> -9,-8,-7,-6 #-5 is not included.
Ex- range(8,3,-1) -> 8,7,6,5,4 #3 is not included.
The range function generates a sequence of number from the first value (start) to the second one (stop) you give it.
As the documentation of the stop parameter states:
Required. An integer number specifying at which position to stop (not included).
Therefore, being stop not included in the sequence, if you put 0 there you obtain values from len(string)-1 to 1, with the first character of the string you are iterating (the one at index 0) that will not be reached from the for loop.
So you usually put the next value to the one you are interested in (in this case -1 to iterate until 0).
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)
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)
This question already has answers here:
Why are slice and range upper-bound exclusive?
(6 answers)
Closed last month.
>>> range(1,11)
gives you
[1,2,3,4,5,6,7,8,9,10]
Why not 1-11?
Did they just decide to do it like that at random or does it have some value I am not seeing?
Because it's more common to call range(0, 10) which returns [0,1,2,3,4,5,6,7,8,9] which contains 10 elements which equals len(range(0, 10)). Remember that programmers prefer 0-based indexing.
Also, consider the following common code snippet:
for i in range(len(li)):
pass
Could you see that if range() went up to exactly len(li) that this would be problematic? The programmer would need to explicitly subtract 1. This also follows the common trend of programmers preferring for(int i = 0; i < 10; i++) over for(int i = 0; i <= 9; i++).
If you are calling range with a start of 1 frequently, you might want to define your own function:
>>> def range1(start, end):
... return range(start, end+1)
...
>>> range1(1, 10)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Although there are some useful algorithmic explanations here, I think it may help to add some simple 'real life' reasoning as to why it works this way, which I have found useful when introducing the subject to young newcomers:
With something like 'range(1,10)' confusion can arise from thinking that pair of parameters represents the "start and end".
It is actually start and "stop".
Now, if it were the "end" value then, yes, you might expect that number would be included as the final entry in the sequence. But it is not the "end".
Others mistakenly call that parameter "count" because if you only ever use 'range(n)' then it does, of course, iterate 'n' times. This logic breaks down when you add the start parameter.
So the key point is to remember its name: "stop".
That means it is the point at which, when reached, iteration will stop immediately. Not after that point.
So, while "start" does indeed represent the first value to be included, on reaching the "stop" value it 'breaks' rather than continuing to process 'that one as well' before stopping.
One analogy that I have used in explaining this to kids is that, ironically, it is better behaved than kids! It doesn't stop after it supposed to - it stops immediately without finishing what it was doing. (They get this ;) )
Another analogy - when you drive a car you don't pass a stop/yield/'give way' sign and end up with it sitting somewhere next to, or behind, your car. Technically you still haven't reached it when you do stop. It is not included in the 'things you passed on your journey'.
I hope some of that helps in explaining to Pythonitos/Pythonitas!
Exclusive ranges do have some benefits:
For one thing each item in range(0,n) is a valid index for lists of length n.
Also range(0,n) has a length of n, not n+1 which an inclusive range would.
It works well in combination with zero-based indexing and len(). For example, if you have 10 items in a list x, they are numbered 0-9. range(len(x)) gives you 0-9.
Of course, people will tell you it's more Pythonic to do for item in x or for index, item in enumerate(x) rather than for i in range(len(x)).
Slicing works that way too: foo[1:4] is items 1-3 of foo (keeping in mind that item 1 is actually the second item due to the zero-based indexing). For consistency, they should both work the same way.
I think of it as: "the first number you want, followed by the first number you don't want." If you want 1-10, the first number you don't want is 11, so it's range(1, 11).
If it becomes cumbersome in a particular application, it's easy enough to write a little helper function that adds 1 to the ending index and calls range().
It's also useful for splitting ranges; range(a,b) can be split into range(a, x) and range(x, b), whereas with inclusive range you would write either x-1 or x+1. While you rarely need to split ranges, you do tend to split lists quite often, which is one of the reasons slicing a list l[a:b] includes the a-th element but not the b-th. Then range having the same property makes it nicely consistent.
The length of the range is the top value minus the bottom value.
It's very similar to something like:
for (var i = 1; i < 11; i++) {
//i goes from 1 to 10 in here
}
in a C-style language.
Also like Ruby's range:
1...11 #this is a range from 1 to 10
However, Ruby recognises that many times you'll want to include the terminal value and offers the alternative syntax:
1..10 #this is also a range from 1 to 10
Consider the code
for i in range(10):
print "You'll see this 10 times", i
The idea is that you get a list of length y-x, which you can (as you see above) iterate over.
Read up on the python docs for range - they consider for-loop iteration the primary usecase.
Basically in python range(n) iterates n times, which is of exclusive nature that is why it does not give last value when it is being printed, we can create a function which gives
inclusive value it means it will also print last value mentioned in range.
def main():
for i in inclusive_range(25):
print(i, sep=" ")
def inclusive_range(*args):
numargs = len(args)
if numargs == 0:
raise TypeError("you need to write at least a value")
elif numargs == 1:
stop = args[0]
start = 0
step = 1
elif numargs == 2:
(start, stop) = args
step = 1
elif numargs == 3:
(start, stop, step) = args
else:
raise TypeError("Inclusive range was expected at most 3 arguments,got {}".format(numargs))
i = start
while i <= stop:
yield i
i += step
if __name__ == "__main__":
main()
The range(n) in python returns from 0 to n-1. Respectively, the range(1,n) from 1 to n-1.
So, if you want to omit the first value and get also the last value (n) you can do it very simply using the following code.
for i in range(1, n + 1):
print(i) #prints from 1 to n
It's just more convenient to reason about in many cases.
Basically, we could think of a range as an interval between start and end. If start <= end, the length of the interval between them is end - start. If len was actually defined as the length, you'd have:
len(range(start, end)) == start - end
However, we count the integers included in the range instead of measuring the length of the interval. To keep the above property true, we should include one of the endpoints and exclude the other.
Adding the step parameter is like introducing a unit of length. In that case, you'd expect
len(range(start, end, step)) == (start - end) / step
for length. To get the count, you just use integer division.
Two major uses of ranges in python. All things tend to fall in one or the other
integer. Use built-in: range(start, stop, step). To have stop included would mean that the end step would be assymetric for the general case. Consider range(0,5,3). If default behaviour would output 5 at the end, it would be broken.
floating pont. This is for numerical uses (where sometimes it happens to be integers too). Then use numpy.linspace.
lines = file('info.csv','r').readlines()
counts = []
for i in xrange(4):
counts.append(fromstring(lines[i][:-2],sep=',')[0:-1])
If anyone can explain this code to me, it would be greatly appreciated. I can't seem to find more advanced examples on slicing--only very simple ones that don't explain this situation.
Thank you very much.
A slice takes the form o[start:stop:step], all of which are optional. start defaults to 0, the first index. stop defaults to len(o), the closed upper bound on the indicies of the list. step defaults to 1, including every value of the list.
If you specify a negative value, it represents an offset from the end of the list. For example, [-1] access the last element in a list, and -2 the second last.
If you enter a non-1 value for step, you will include different elements or include them in a different order. 2 would skip every other element. 3 would skip two out of every three. -1 would go backwards through the list.
[:-2]
Since start is omitted, it defaults to the beginning of the list. A stop of -2 indicates to exclude the last two elements. So o[:-2] slices the list to exclude the last two elements.
[0:-1]
The 0 here is redundant, because it's what start would have defaulted to anyway. This is the same as the other slice, except that it only excludes the last element.
From the Data model page of the Python 2.7 docs:
Sequences also support slicing: a[i:j] selects all items with index k such that i <= k < j. When used as an expression, a slice is a sequence of the same type. This implies that the index set is renumbered so that it starts at 0.
Some sequences also support “extended slicing” with a third “step” parameter: a[i:j:k] selects all items of a with index x where x = i + n*k, n >= 0 and i <= x < j.
The "what's new" section of the Python 2.3 documentation discusses them as well, when they were added to the language.
A good way to understand the slice syntax is to think of it as syntactic sugar for the equivalent for loop. For example:
L[a:b:c]
Is equivalent to (e.g., in C):
for(int i = a; i < b; i += c) {
// slice contains L[i]
}
Where a defaults to 0, b defaults to len(L), and c defaults to 1.
(And if c, the step, is a negative number, then the default values of a and b are reversed. This gives a sensible result for L[::-1]).
Then the only other thing you need to know is that, in Python, indexes "wrap around", so that L[-1] signifies the last item in the list, L[-2] is the second to last, and so forth.
If list is a list then list[-1] is the last element of the list, list[-2] is the element before it and so on.
Also, list[a:b] means the list with all elements in list at positions between a and b. If one of them is missing, it is assumed to mean the end of the list. Thus, list[2:] is the list of all elements starting from list[2]. And list[:-2] is the list of all elements from list[0] to list[-2].
In your code, the [0:-1] part it the same as [:-1].