Passing a Range Into a Function


#1

I am confused by how range() works. The first two examples make sense, but the third doesn't seem to follow a pattern. Can anyone explain?

ex 1: range(6) -> [0, 1, 2, 3, 4, 5]

ex 2: range(1-6) -> [1, 2, 3, 4, 5]

ex 3: range(1, 6, 3) -> [1, 4] What is the logic behind this?

Also, a range has 3 different versions: range(stop), range(start, stop), range(start, stop, step). What do these versions mean?

Thank you


#2

call help(range) to view its docstring and/or look up the range function in the documentation (google for python builtins)


#3

Already tried Google and it didn't help, that is why I asked the forum.


#4

Can anyone explain how range works? I have used Google and still do not understand. I think I can follow range(6) or range(1-6), but range(1, 6, 3) does not make sense.


#5

That doesn't rule out googling as the best way of obtaining the information you're looking for, and also doesn't say how you'd need it explained differently if the googling failed - if the searching failed then knowing why it failed is even more relevant when coming up with an explanation.

If you didn't find the documentation for that function, try the search query that I suggested which is going to get you the official documentation for that function in the first or second search result.

I'm not going to give you a worse explanation because you couldn't find the best one that is available - I'm going to tell you how to find it. Don't be so quick to dismiss that. It's a common mistake to ask about how to do a particular solution ("explain it to me") instead of asking about the problem and accepting any viable solutions ("I need help figuring out the behaviour of this function") When you come up with a bad solution and ask for help with that solution, people you are asking are likely to backtrack and address the problem and choose a different solution better suited. If such a solution doesn't satisfy you, you'll need to explain why that is, otherwise the ones you're asking won't be able to find a better solution, they don't know why you disqualify what was suggested.


#6

The information I found was technical and didn't provide much depth of information. Maybe it had everything, but I am not making the connections.


#7

And which information was that?
You should be arriving at this: https://docs.python.org/2/library/functions.html#range
It's using lists, addition, multiplication, mentions for-loops, arguments, some math. Most of these should be familiar, and even if you don't understand every single thing, that doesn't mean that it can't still be understood as a whole by puzzling together the rest. So which of them would need to be worked around in a different explanation?
It may also be that you should spend a bit more time reading it, it is fairly plain.
There are multiple examples involving step, and it says what it does with the step argument.
It also mentions arithmetic progressions, there's a wikipedia article on that, reading the first sentence of that article should tell you all you need to know about what that is.

You've also got the ability to call the function yourself and play around with the arguments and make observations about the outcome.

You have so very much information here, lots of overlapping things, you don't need to understand all the information, several parts by themselves are enough.

Most likely, you should spend more time with the information you have. Or you should be pointed at which information exists.

I absolutely understand that it still may not be easy to understand, but "doesn't work, explain it better" makes it impossible to understand what you need and most likely you don't need anything more anyway. But if you do, you'll need to be incredibly specific about what you ask, take the drivers seat instead of making it somebody else's responsibility to guess what you need.


#8

range(1, 6, 3) means start the range at 1 and stop it before it reaches 6 in steps of 3 (basically keep adding 3 to get to the next number), so it becomes 1, 4, 7 etc but 7 is outside 6, which means the range is 1,4

In the same way range(1, 8, 2) would be 1, 3, 5, 7 (in this case we just keep adding 2 which is called the step value :slight_smile:


#9

@rip4_ever I see how the range works. How would I handle range(1, 6, 3, 10) or is that not a possibility?

Thanks for your explanation.


#10

Honestly, we're all here to learn...not be talked down to.


#11

I already tried that, explain it to me better

This is going to result in me insisting on an explanation of what went wrong with the already available and superior resource, or more well defined questions.

Bad manners are going to be met with some disdain.

Yes, it is non-obvious for beginners what counts as good manners when asking technical questions, particularly when entirely focused on the problem. That's also why I explain my reactions rather frankly.

I could get really really sugar-coaty about it, but then I'd truly be talking down to someone, because then we're no longer talking as peers

I also disagree with the notion that arguing is offensive


#12

@ionatan - what did I do wrong when asking you a question? Did I misspell something, use improper punctuation, or use all CAPS? Please advise, I wouldn't want to be rude to anyone ever again.


#13

Dismissing the documentation and its examples, your own ability to experiment and observe, while at the same time not explaining what the though process was so far.

In a classroom setting it can be the case that it becomes the teacher's responsibility to teach. Students have to be there, so they have low motivation to seek knowledge - the teacher has to feed it to the student. Students learn to phrase questions as "help, don't get it".
If someone asks you that, you'll shrug and say it's not your problem.
So when you ask something, that's not a suitable approach.
If someone asks "help, don't get it" and you show them where they can find out, I think you'll find it rude if they say "nope, not good enough. someone should explain it to me".
Now, if they actually have well defined questions where it's clear that they're actually stuck with their information finding and where they've made what they can to make it easy to answer - at that point it's no longer rude and usually the question is welcomed. That's when the student takes their own responsibility for their learning, that's when they've earned the right to ask their peers for help.
Random people on the internet - they are peers. Nobody owes anyone anything. When somebody wants something, they'll need to make a good impression.

Nowhere here is there a measurement of skill or experience. It's fine to ask for help with anything. But there is much difference in how it's done.


#14

I see your point, but was not a fan of how you responded. I am new to
coding and I get frustrated because I want to learn something different.


#15

Hey dude I think you're totally fine. You did what @ionatan suggested but that didn't help you so you asked on the forum again. That's the whole point of the forum right? Don't feel bad about it. We're all here to learn, and here to help each other after all. And to answer your question, no, I don't think range(1, 6, 3, 10) is a possibility, because there are only three spots possible in the range function and they are range(start, stop, step). After you have three numbers in the range it doesn't do anything more, because the numbers are essentially meaningless, because they are not filling any role in the range.


#16

wow, oops, that scared me. Now, I have to think more than twice before i ask questions in case it is deemed "frivolous".


#17

I've said nothing about what I think is okay to ask about :confused:
It's a two-way conversation, and the one asking is probably the one who should be driving the conversation forward by explaining what they think and what they want
"help!" or "why?" or "explain it to me" isn't making an attempt at that.
Just consider how you'd go about asking someone on the street for help. You'll need to win them over.


#18

Thanks Josh,

After I asked I went back figured nothing would happen if a 4 number was in a range.

Thanks again for your information.


#19

@joshuaboyum,

I was messing around with the range function and I am not sure why this range seemed to stop short.

print range(-1, 99, 4) - > [-1, 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, 55, 59, 63, 67, 71, 75, 79, 83, 87, 91, 95]

Why does is stop at 95 when adding 4 gets to 99?


#20

The number in the 'stop' parameter of the range function tells what number the range cannot go to or past. In other words, the number in the 'stop' parameter is not included in the range of numbers. So if you run range(0, 8, 1), it will return [0, 1, 2, 3, 4, 5, 6, 7], because the number 8 in the range(0, 8, 1) says that the range will only go up to 7, the number before the stop parameter 8.