#๐ what is this called
147 messages ยท Page 1 of 1 (latest)
@tender gust
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
the vocab you're looking for is "generator" https://wiki.python.org/moin/Generators
wont let me click that link
503 Service Unavailable
No server is available to handle this request.
oh that's weird
range returns a range object. not a list.
range object behaves a lot like lists, but aren't lists
list is called a generator?
err range is just sort of a generator-like object
the main difference is that range(0, 10000000000000000) works just fine, but using list() on it will allocate a lot of memory
I think i know what range is but why do i have to put list infront of it to make it spit out a list of numbers
because range isn't a list.
a list and a range have different string representations
im still kinda lost
i suppose a better terminology might be iterable
range is an "iterable" in python
which means it's an object that implements the __next__ method, which can be called with next(foo)
when you print it, the list generates the characters "[" and the items, separated with "," and ends with a "]" ... a range doesn't do that, because that would mislead what a range is if it prints like a list.
otherwise a range and list behave quite the same.
every time you call __next__, you get the next value from the iterator
give me one sec to read this ๐
by characters do you mean numbers in x = list(range(1,11))
what do you mean by range objects sorry im new
range returns an iterable, not a list. an iterable is anything that can be looped through. so a list is an iterable, but not all iterables are lists. range can be looped through because it implements a special function called __iter__. python handles this function specially, and makes it work with things like for loops. __iter__ lets range lazily define what values it holds
a range is a kind of object just like list or int
range used to return a list in old versions of Python. That wasn't great because range(1_000_000) would create a list with million numbers, taking up a lot of memory
im still rereading your guys msg btw
the reason you might want something like this, is that it lets you lazily define what values your iterable has. if you're just looping through the range, there's no need to create an entire list of every number in that range, when you only need 1 number at a time. so [0, 1, 2, 3, ... 10000] holds a lot of memory, but range(10000) holds the same amount of memory as range(5) because the values haven't been generated yet
@thick cape so list is a genrator?
no, a generator is a different type of thing that works a little like a range, but ultimately not relevant here
list is an iterable. it can be looped (or "iterated") through
would you say list is like a handi cap for range?
i wouldnt
what do you mean by handicap?
without the list function the you cant print the range numbers
no, I mean when you print a list, it converts the list into a string representation like "[1, 2, 3]" ... a range simply doesn't do that, because printing a range should give a developer information, that makes it clear it's a range, and not a list.
a range doesn't actually store it's values anywhere. A list does.
!e sure you can
for x in range(10):
print(x, end="")
@frank hare :white_check_mark: Your 3.12 eval job has completed with return code 0.
0123456789
there are a lot more things you want to do with lists/ranges than just print them out. the printing can maybe make debugging annoying sometimes, but the core functionality that most people care about is iterating through the values
would you say applying list func to the range func is iterating threw the range of the numbers passed into the range func
yes, exactly
oh shit ๐
list(...) works by iterating over the iterable and pushing everything into a list
it's basically ```py
arr = []
for e in iterable:
arr.push(e)
now range is admittedly not a generator, but it's useful sometimes to think of iterables like range as generators
for example, you can implement your own range pretty easily
def my_range(start, end):
while start < end:
yield start
start += 1
Well, if you only care about the iterable part of range
and also don't care about the step...
ranges can also be inspected for their start, stop, and step values, and can be indexed (including slicing)
you have me lost in that
when you pass an iterable to list, it takes the things from the iterable and puts them in a list
range is the iterable in my code?
yeah
: D
yeah so these are equivalent:
arr = []
for i in range(3, 4):
arr.append(i)
arr = list(range(3, 4))
is more or less what i'm trying to say there
the reason that range doesn't show you all the values in its string representation is because that would be against what range is meant to be: lazy
the start, stop and step values can represent all values in the range without showing you all the values. Showing all the values would nullify the laziness of the range (lazy as in not using a bunch of memory to store the values)
for here, yield in python lets you define a function which is a "generator". a generator is a function which is an iterable. you can call it multiple times and get a different value every time. so if you do something like this,
!e ```py
def my_range(start, end):
while start < end:
yield start
start += 1
print(my_range(1, 5))
print(list(my_range(1, 5)))
@thick cape :white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | <generator object my_range at 0x7f4a7dc08c40>
002 | [1, 2, 3, 4]
yield actually makes a generator btw
using yield in a function turns it into a function which returns a generator
err isn't that what i said haha
Would those range of objects be numbers?
Putting something in quotes makes it seem like an intentionally false statement for simplicity
oh i meant it more as emphasis of a vocab term
"range object" ... NOT "range of objects"
oh range in is the object?
You may also be more specific, as a generator is an iterator. Iterators will be exhausted when iterated through
yes, the range itself is an object, that only stores start, end and step.. it doesn't actually store any of the numbers
iterators are also iterables
let's keep it on topic and helpful, they didnt ask about generators
Connor mentioned it in explanation
are all functions objects?
yes!
in python, yes. but everything is kinda an object
>>> r = range(10)
>>> r
range(0, 10)
>>> type(r)
<class 'range'>
an object in python is anything that represents data
objects each have a type, e.g. 5 is an int, [5] is a list, range(5) is a range
in regards to range .. the range function (it's actually a class) returns a range object
there is an important difference between range and range(...)
the first is the range class/function itself, and the latter creates an instance/object of that range class.
what else would considered a object i know now that all functions are objects
everything
strings, sets, floats, dicts, etc
integers, strings, tuples, lists, classes/types, modules
is bob = 4 witch would be the object 4?
True, None
ints floats lists tuples dicts sets functions modules classes complex numbers
And so on
Yes, 4 is an object
all objects have a type, but types are objects as well, so they gotta have a type. which is called type
don't get confused between the semantics (what we're talking about here) and the syntax (what you see in your python file, e.g. bob = 4)
bob is a name that, after it's defined, refers to an object
4 is an int literal that creates an object
after bob = 4 is executed, bob refers to the object that 4 created
bob = 4 is an assignment. the whole thing itself is not an object. it's a statement. But the right side of an assignment is an expression, and expressions are always evaluated to some object (in this case 4 is an integer object)
im still trying to reread your guys messages btw
what is a string repersentation?
like the whole two words?
when you try to print an object, you want to see something, ideally some characters that tell you about that object
!e
print(range(2))
@frank hare :white_check_mark: Your 3.12 eval job has completed with return code 0.
range(0, 2)
range(0, 2) is the representation of the range object, but those characters are not what the object is
on some level the object is just some binary data handled by the python interpreter
so in short the string representation of an object is the string you get when you ask for it (and the print function implicitly asks for string representations for it to print)
So we use the list function with the range function because we want a string repersentation?
because the range func doesnt do it on its own
we use it bc the list's string representation shows the numbers inside, and the range's does not
oh okay : D
inside the range function right?
the list's string representation shows the objects inside the list, the range's doesn't show the numbers "inside" the range
and there's a good reason for that, which people were talking about earlier
are numbers inside the list objects
So i wrote in my notes: WE use the list func because list's string repersentation show the numbers inside the range func
is this right?
yeah
and string repersentation is a property of the list func?
every object has a string representation
im lost at what you said lol
you know what a string is?
a string representation of an object is just the string that represents the object
if it didnt have one, you couldnt print it
so every object has one
a string repersentation is just a printed statement?
@frank hare :white_check_mark: Your 3.12 eval job has completed with return code 0.
<object object at 0x7fd412854400>
!close
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.