for i in range(4): print(i) i += 1
How can I make this print:
0
2
Videos
You can use the value of the first loop as the starting value of the range of the second loop like
for fileX in range(1,4):
for fileY in range(fileX,4):
To also skip the equall ones do
for fileX in range(1,4):
for fileY in range(fileX+1,4):
Don't reinvent the wheel. If you need combinations, just use itertools.combinations:
for fileX, fileY in itertools.combinations(range(1,4), 2):
print "X is " + str(fileX) + ", Y is " + str(fileY)
Output:
X is 1, Y is 2
X is 1, Y is 3
X is 2, Y is 3
Compared to the double-for-loop, this is somewhat more readable (the code tells you exactly what it does) and less prone of introducing silly off-by-one errors and the like. Also, this works equally well with any sort of collection or iterable, not just with an ordered list of numbers.
for i in [float(j) / 100 for j in range(0, 100, 1)]:
print i
Avoid compounding floating point errors with this approach. The number of steps is as expected, while the value is calculated for each step.
def drange2(start, stop, step):
numelements = int((stop-start)/float(step))
for i in range(numelements+1):
yield start + i*step
Usage:
for i in drange2(0, 1, 0.01):
print i
You can use a range with a step size of 2:
Python 2
for i in xrange(0,10,2):
print(i)
Python 3
for i in range(0,10,2):
print(i)
Note: Use xrange in Python 2 instead of range because it is more efficient as it generates an iterable object, and not the whole list.
You can also use this syntax (L[start:stop:step]):
mylist = [1,2,3,4,5,6,7,8,9,10]
for i in mylist[::2]:
print i,
# prints 1 3 5 7 9
for i in mylist[1::2]:
print i,
# prints 2 4 6 8 10
Where the first digit is the starting index (defaults to beginning of list or 0), 2nd is ending slice index (defaults to end of list), and the third digit is the offset or step.
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.
In the provided code when you try to use i in a for loop with range, it always changes to the number provided by range in the function without bothering to look at the increment made to i. so basically if you try list(range(0, 10)) this will give you [0, 2, 3, 4, 5, 6, 7, 8, 9]. so for goes through that list one by one without thinking if any changes were made to i or not.
which if seen
loop_1: i=0
loop_2: i=1
loop_3: i=2
loop_4: i=3 (here now you increment value by 1), i=4
loop_5: i=4 (but the for loop was going though the list from range function so the change didn't do anything)
loop_6: i=5 (and so on until 9)
The third parameter of range is indeed step. But you should use it with a computed value like:
for i in range(1,1000,2):
#statements
The reason why your i += i didn't worked is because, under the hood, the for-loop is executing something similar to i = next(...) at the end of an iteration, overiding your last increment.
Edit
You can achieve the desired effect using a generator, but it kinda kills the "trying to avoid while-loops" thing:
def myrange(start, stop):
i = start
while i < stop:
yield i
i += i
for i in myrange(1, 1000):
print(i)
Anyway, while-loops are perfectly valid constructs and I’d personnally go with one in this case. Do not forget that the for-loop has a rather different semantic in python than in both languages you’re used to. So trying to use a for-loop because you are able to do so with javascript seems like a bad idea if all what you need is a while-loop.
range can step by a fixed amount, but not a variable amount. Use a while-loop to increment i by i:
i += i
You could replace the while-loop with an iterator, such as:
import itertools as IT
for i in (2**i for i in IT.count()):
if i >= 1000000000: break
print("when x is", i, ":")
binary(i)
but I don't think this has any advantage over a simple while-loop.
I have a counter set up to notify me how many times I write to a file: amount_of_times = 1. However every-time I write to the file it is not incrementing, is there a way for me to get it to increment, so I know how many times I ran the program?
with open ('TextInfo.txt','w') as filer:
amount_of_times = 1
filer.write('I wrote one time')
filer.write('Again')
print('Executed amont of times: {}'.format(amount_of_times))
amount_of_times = amount_of_times + 1