So lets say
start = 10
end = 30
now I need a loop that goes from startto end
I was thinking of something like this:
for i in [start..end]
print i
but that does not quite work. Is there a clean way to do this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
will give you the output from 10 to inclusive 30.
NOTE: The reason I added
+1to yourendvalue is because the range() function specifies a half-closed interval. This means the first value is included, but the end value is not.I.e.,
would give you a list of
but not include 10.
You can also specify the step size for range() and a host of other things, see the documenation.
Finally, if you are using Python 2.x you can also use xrange() in place of
range(), though in Python 3.x you will only haverange()which is why I used it here.If you are curious about the differences between
range()andxrange()(though it is not really directly relevant to your question) the following two SO questions are a good place to start: What is the difference between range and xrange functions in Python 2.X? and Should you always favor xrange() over range()?