The range function takes 3 arguments: start, stop, and step:
for i in range(-1, 9):
yield i
To go in reverse
for i in range(10, 0, -1):
yield i
Take a look at the documentation.
Edit:
Of course, there's nothing to stop you from using the range directly. Thus map(print, range(10, 0, -1)) will print them all to the screen, while range(10, 0, -1) will give you access to the integers directly.
The range function takes 3 arguments: start, stop, and step:
for i in range(-1, 9):
yield i
To go in reverse
for i in range(10, 0, -1):
yield i
Take a look at the documentation.
Edit:
Of course, there's nothing to stop you from using the range directly. Thus map(print, range(10, 0, -1)) will print them all to the screen, while range(10, 0, -1) will give you access to the integers directly.
You can't modify the iterator variable like this. The for in python here is more similar to a foreach.
To achieve the behavior I assume you want you should do it like this:
i = 0
while (i<10):
i-=1
print i
i+=1
although it looks a bit silly compared to:
while(True):
print(-1)
python - Decrementing for loops - Stack Overflow
Iterate over a list and subtract 1 subsequently and pop in python - Stack Overflow
Python for loop decrementing index - Stack Overflow
Negative index in python for loop - Stack Overflow
Videos
This can be done in a more Pythonic one liner:
out_arr = [in_arr[i] + (i+1) for i in range(-1, -1*len(in_arr)-1, -1)] # [7, 4, 7, 2]
This relies on the observation that you decrement the elements (pointed out by @Thierry Lathuille). In my answer, I use the faster list comprehension to generate the output list. This explains what the for loop does: for i in range(start_index=-1, on_over_stop_index=-5, step_size=-1).
Solution
l=[5,9,5,7]
result= []
for i in range(0,len(l)):
last_elem = l.pop()
result.append(last_elem)
l = [i-1 for i in l]
print(result)
I would like to go over the range() function yet again through the documentation as provided here: Python 3.4.1 Documentation for range(start, stop[, step])
As shown in the documentation above, you may enter three parameters for the range function 'start', 'stop', and 'step', and in the end it will give you an immutable sequence.
The 'start' parameter defines when the counter variable for your case 'i' should begin at. The 'end' parameter is essentially what the size parameter does in your case. And as for what you want, being that you wish to decrease the variable 'i' by 1 every loop, you can have the parameter 'step' be -1 which signifies that on each iteration of the for loop, the variable 'i' will go down by 1.
You can set the 'step' to be -2 or -4 as well, which would make the for loop count 'i' down on every increment 2 down or 4 down respectively.
Example:
for x in range(9, 3, -3):
print(x)
Prints out: 9, 6. It starts at 9, ends at 3, and steps down by a counter of 3. By the time it reaches 3, it will stop and hence why '3' itself is not printed.
EDIT: Just noticed the fact that it appears you may want to decrease the 'i' value in the for loop...? What I would do for that then is simply have a while loop instead where you would have a variable exposed that you can modify at your whim.
test = 0
size = 50
while (test < size)
test += 1
if (some condition):
test -= 1
For such a construct, where you need to vary the loop index, a for loop might not be the best option.
for <var> in <iterable> merely runs the loop body with the <var> taking each value available from the iterable. Changing the <var> will have no effect on the ensuing iterations.
However, since it is usually tricky to work out the sequence of values i will take beforehand, you can use a while loop.
i = 0
while i < size:
if condition:
...
i -= 1
else:
...
i += 1
note: a[-1] refers to the last element of the list.
you assign to a[index] in every loop iteration over a
for a[index] in a:
a[index] will be assigned the values a[0], a[1], ..., a[-1] during the loop and end up being assigned to
a[index] = a[-1]
usually you try not to mess with the list you are iterating over:
for item in a:
# do something with item
This is because in Python, this:
a = [0, 1, 2, 3]
for i in a:
... statements ...
can be understood as this:
a = [0, 1, 2, 3]
it = iter(a)
i = next(it)
... statements ...
i = next(it)
... statements ...
i = next(it)
... statements ...
i = next(it)
... statements ...
Now if you replace i by a[index], then you end up replacing a[index] by the last element you iterated over, which is the last element in your list.
Would anyone be able to give me a hint for how to solve this? Im having trouble having my digit_2 go down by 1 every time the subtraction happens in my last part of the code. Here is my code
while True:
try:
digit = int(input("Part 1: Enter a digit greater than 9 & less than 20: "))
if digit >= 9 and digit <= 20:
break
else:
print("try again")
except ValueError:
print("Provide a number")
continue
while True:
try:
digit_2 = int(input("Part 2: Enter a digit greater than Part 1: "))
if digit_2 > digit:
break
else:
print("Digit 2 is not greater than first digit. Please try again")
except ValueError:
print("Provide a number")
continue
x = digit_2
for x in range(x, 0, - digit):
print(x)