I understand that the following line will give the given result:
for in range(5):
print(i)
0 1 2 3 4
But I don't understand how if adding 3 separate parameters the result is confusing. How is this returning these particular results? (4 6 and 8) ????
for i in range(4, 10, 2):
print(i)
4 6 8
The range function takes one or at most three arguments, namely the start and a stop value along with a step size.
in the above code, range has 3 parameters : Start of Range (inclusive) End of Range (Exclusive) Incremental value.
The 3rd argument of range is the stepping, meaning how much to increment the last value for: >>> for i in range (0,10,2): ... print i 0 2 4 6 8.
The range() is an in-built function in Python. It returns a sequence of numbers starting from zero and increment by 1 by default and stops before the given number. It has three parameters, in which two are optional: start: It's an optional parameter used to define the starting point of the sequence.
for i in range(4, 10, 2):
print(i)
in the above code, range has 3 parameters :
For more clarity refer below for the java representation of above python code:
for (int i=0; i<10; i+=2){ System.out.println(i) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With