r/PythonLearning • u/Repulsive-Song-1594 • 1d ago
i am stuck with the range function
sorry but i am gonna crash out because of the range function i cant understand it well to use it this is driving me crazy any one could give me a video explaining everything i dont mind it being an hour long
7
Upvotes
4
u/ninhaomah 1d ago
What is it about range() function that driving you mad ?
It's like saying my Bluetooth mouse is driving me mad...
It doesn't move ? Not detected ? Buttons not working ? Who knows ?
6
u/BranchLatter4294 1d ago
Practice with it for a few minutes. There are only 3 parameters (start, stop, step). Experiment and observe the behavior.
1
u/Repulsive-Song-1594 1d ago
it isnt that i dont understand what range function do the problem is that i find it hard to use lists and numbers and function in it
2
u/tb5841 1d ago
range(n) produces an object.
If you put in 5, for example, range(5) produces the numbers 0, 1, 2, 3, 4.
It starts at 0 by default, goes up by 1 each time by default, and goes up to just before the number you put in.
You can change where it starts, and what it goes up by each time, but don't worry about that for now.
There are two things that make it seem confusing. One is that you most often see it in 'for' loops, and if you're not happy with loops yet then that won't make sense. The other is that the return value of the range() function, that contains the numbers 0, 1, 2, 3, 4, is a type of object you haven't come across yet.
Don't find a video. Open Python, type stuff, and see if you can work out what's happening. Avoid anything with a 'for' loop for now. Some stuff worth trying out:
r = range(5)
print(r)
print(list(r))
print(3 in r)
print(5 in r)
print(set(r))
...etc.
2
u/mountain-snowman 1d ago
Just read this 2 mins documentation from the devs: https://docs.python.org/2/library/functions.html#range
1
u/Sea-Ad7805 1d ago
It's easy, look at this example by clicking Memory Graph Web Debugger and see its state change step by step.
1
u/Adrewmc 1d ago
What’s the problem.
https://docs.python.org/3/builtins/stdtypes.html#range
The docs are fairly explicit on the range object.
1
u/AndrewBorg1126 1d ago edited 1d ago
Meanwhile, here I am wondering how there would possibly even be enough stuff involved in it to spend an hour talking about range.
It gives you a range of numbers described by the parameters. It's worth examining the edge behaviors like whether bounds are inclusive or exclusive, maybe what happens when the step size excludes the second bound as an element, but aside from that what really is there to talk about?
1
u/atarivcs 1d ago
The purpose of the range function is to create a sequence of integers. Usually you would then use this sequence in a for loop, but there are other uses too.
range() can take one, two, or three arguments.
If you pass one argument, the sequence starts at 0 and continues up to (but not including) the argument. So for example range(5) produces the sequence 0,1,2,3,4.
If you pass two arguments, the sequence starts at the first argument and continues up to (but not including) the second argument. So range(4,11) produces the sequence 4,5,6,7,8,9,10.
If you don't want the sequence to be consecutive, you can pass a third argument which is used as the "step" increment between values. range(1, 8, 2) produces the sequence 1,3,5,7.
What is the exact problem you have having?
1
u/aiuniverse-3p3_here 1d ago
Bro, range is simple, don't crash out!
range(5) = 0,1,2,3,4 It starts from 0 and stops before 5.
range(1,6) = 1,2,3,4,5 First number is start, second is stop.
range(0,10,2) = 0,2,4,6,8 Third number is step, how much to jump.
Try this: for i in range(5): print(i)
You'll see 0 to 4. That's it!
1
u/Due-Mortgage-6854 1d ago edited 23h ago
It's easy don't worry
range(start, stop, step)Now as example, let's say
You book time off work starting the 10th and tell your boss, "I'll be back at my desk on the 15th." You are off on the 10th, 11th, 12th, 13th, and 14th. Because you return on the 15th, that day isn't part of your vacation. That is why Python stops right before the second number
So your start here is 10 and stop is 15!You decide to hit the gym every other day for the month of June (which has 30 days), starting on June 1st. You start on the 1st, skip a day (step = 2), go on the 3rd, 5th, 7th, all the way up to the 29th. You set the stop number to 31 so that the 30th isn't accidentally cut off before the month ends. Here start is 1, stop 31 and step 2!
If you want some more help : https://discord.com/invite/QWGWKFVYyh