From the documentation:
range([start], stop[, step])
The start defaults to 0, the step can be whatever you want, except 0 and stop is your upper bound, it is not the number of iterations. So declare n to be whatever your upper bound is correctly and you will not have to add 1 to it.
e.g.
>>> for i in range(1, 7, 1): print(i)
...
1
2
3
4
5
6
>>> for i in range(1, 7, 2): print(i)
...
1
3
5
A nice feature, is that it works in reverse as well.
>>> for i in range(7, 0, -1): print(i)
...
7
6
5
4
3
2
1
If you aren't using it as an index but for something that can have positive or negative values, it still comes in handy:
>>> for i in range(2, -3, -1): print(i)
...
2
1
0
-1
-2
>>> for i in range(-2, 3, 1): print(i)
...
-2
-1
0
1
2
Answer from Rolf of Saxony on Stack OverflowFrom the documentation:
range([start], stop[, step])
The start defaults to 0, the step can be whatever you want, except 0 and stop is your upper bound, it is not the number of iterations. So declare n to be whatever your upper bound is correctly and you will not have to add 1 to it.
e.g.
>>> for i in range(1, 7, 1): print(i)
...
1
2
3
4
5
6
>>> for i in range(1, 7, 2): print(i)
...
1
3
5
A nice feature, is that it works in reverse as well.
>>> for i in range(7, 0, -1): print(i)
...
7
6
5
4
3
2
1
If you aren't using it as an index but for something that can have positive or negative values, it still comes in handy:
>>> for i in range(2, -3, -1): print(i)
...
2
1
0
-1
-2
>>> for i in range(-2, 3, 1): print(i)
...
-2
-1
0
1
2
range(1, n+1) is not considered duplication, but I can see that this might become a hassle if you were going to change 1 to another number.
This removes the duplication using a generator:
for _ in (number+1 for number in range(5)):
print(_)
I am pulling data from a dictionary and appending it to a list, but it appears that dictionary keys start at 1, not 0.
We have been taught to use for loops for this kind of task, but they initialize at 0. Can I start at 1? If not, what is an alternative? Thanks!
for i in range(len(dict)):
<code goes here>python - How to start with the number 1 in a for loop? - Stack Overflow
iteration - Start index for iterating Python list - Stack Overflow
Select first in for loop
How do you start a for loop at 1 instead of 0?
Videos
For loops work like so:
for [variable] in range(start, end, increment)
per your example, you would like to start at 1, and end at 5 for example
for [variable] in range(1, 5)
the values will display
1
2
3
4
End value always is 1 less because it counts 0, so you want to add +1 to the end if you want the exact number.
Python's range function returns integers starting between 0 and the given number if there is no starting parameter. For instance:
for i in range(3):
print (i)
returns:
0
1
2
if you want to alter your code to print the range starting from 1 and inclusive of the given input, you may consider slightly changing the function to this:
num = int(input('Enter the number of processes: '))
for i in range(1,num+1):
b = input('Enter the burst time of process ' + str(i) + ': ')
a = input('Enter the arrival time of process ' + str(i) + ': ')
If you don't want your range to be inclusive of the given integer you can just do it like this:
num = int(input('Enter the number of processes: '))
for i in range(1,num):
b = input('Enter the burst time of process ' + str(i) + ': ')
a = input('Enter the arrival time of process ' + str(i) + ': ')