#๐Ÿ”’ what is this called

147 messages ยท Page 1 of 1 (latest)

tender gust
#

in the following codes:
x=list(range(1,11))
print(x)
vs
x = range(1,11)
print(x)
Why do i have to type list first to make this code work? sorry my communcation skills arent so good D:

pure spruceBOT
#

@tender gust

Python help channel opened

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.

thick cape
tender gust
#

wont let me click that link

#

503 Service Unavailable
No server is available to handle this request.

thick cape
#

oh that's weird

boreal kiln
#

range returns a range object. not a list.
range object behaves a lot like lists, but aren't lists

tender gust
#

list is called a generator?

thick cape
#

err range is just sort of a generator-like object

boreal kiln
#

the main difference is that range(0, 10000000000000000) works just fine, but using list() on it will allocate a lot of memory

thick cape
#

and a list is not a generator

tender gust
#

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

boreal kiln
#

because range isn't a list.

#

a list and a range have different string representations

tender gust
#

im still kinda lost

thick cape
#

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)

boreal kiln
#

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.

thick cape
#

every time you call __next__, you get the next value from the iterator

tender gust
#

give me one sec to read this ๐Ÿ˜„

thick cape
tender gust
tender gust
thick cape
#

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

frank hare
slate spoke
#

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

tender gust
#

im still rereading your guys msg btw

thick cape
#

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

tender gust
#

@thick cape so list is a genrator?

frank hare
#

no, a generator is a different type of thing that works a little like a range, but ultimately not relevant here

thick cape
#

list is an iterable. it can be looped (or "iterated") through

tender gust
#

would you say list is like a handi cap for range?

frank hare
#

i wouldnt

thick cape
#

what do you mean by handicap?

tender gust
#

without the list function the you cant print the range numbers

boreal kiln
#

a range doesn't actually store it's values anywhere. A list does.

frank hare
#

!e sure you can

for x in range(10):
    print(x, end="")
pure spruceBOT
#

@frank hare :white_check_mark: Your 3.12 eval job has completed with return code 0.

0123456789
thick cape
tender gust
thick cape
#

yes, exactly

tender gust
#

oh shit ๐Ÿ˜„

thick cape
#

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

wet saffron
#

Womp-womp

#

list.append

thick cape
#

for example, you can implement your own range pretty easily

def my_range(start, end):
    while start < end:
        yield start
        start += 1
wet saffron
#

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)

tender gust
frank hare
#

when you pass an iterable to list, it takes the things from the iterable and puts them in a list

tender gust
frank hare
#

yeah

tender gust
#

: D

thick cape
#

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

wet saffron
#

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)

thick cape
#

!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)))

pure spruceBOT
#

@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]
tender gust
#

give me a bit to read this ๐Ÿ˜„

#

ty for the help guys

wet saffron
#

using yield in a function turns it into a function which returns a generator

thick cape
#

err isn't that what i said haha

tender gust
wet saffron
#

Putting something in quotes makes it seem like an intentionally false statement for simplicity

thick cape
#

oh i meant it more as emphasis of a vocab term

boreal kiln
tender gust
#

oh range in is the object?

wet saffron
boreal kiln
wet saffron
#

iterators are also iterables

frank hare
wet saffron
#

Connor mentioned it in explanation

tender gust
#

are all functions objects?

thick cape
#

yes!

tender gust
#

: D

#

im taking notes to guys !

boreal kiln
#

in python, yes. but everything is kinda an object

#
>>> r = range(10)
>>> r
range(0, 10)
>>> type(r)
<class 'range'>
frank hare
#

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

boreal kiln
#

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.

tender gust
wet saffron
#

everything

frank hare
#

strings, sets, floats, dicts, etc

boreal kiln
#

integers, strings, tuples, lists, classes/types, modules

tender gust
#

is bob = 4 witch would be the object 4?

frank hare
#

True, None

wet saffron
#

ints floats lists tuples dicts sets functions modules classes complex numbers

#

And so on

wet saffron
boreal kiln
#

all objects have a type, but types are objects as well, so they gotta have a type. which is called type

wet saffron
#

All things which can be a value are objects

#

so like, a for loop is not an object

frank hare
#

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

boreal kiln
# tender gust is bob = 4 witch would be the object 4?

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)

tender gust
#

im still trying to reread your guys messages btw

tender gust
#

like the whole two words?

frank hare
#

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))
pure spruceBOT
#

@frank hare :white_check_mark: Your 3.12 eval job has completed with return code 0.

range(0, 2)
frank hare
#

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)

tender gust
#

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

frank hare
#

we use it bc the list's string representation shows the numbers inside, and the range's does not

tender gust
#

oh okay : D

tender gust
frank hare
#

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

tender gust
#

are numbers inside the list objects

frank hare
#

yeah

#

or said another way, the list holds references to its elements

tender gust
#

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?

frank hare
#

yeah

tender gust
#

and string repersentation is a property of the list func?

frank hare
#

every object has a string representation

tender gust
#

im lost at what you said lol

frank hare
#

you know what a string is?

tender gust
#

yea a string is a set of characters

#

or a sentence?

#

too

frank hare
#

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

tender gust
#

a string repersentation is just a printed statement?

frank hare
#

its just a string

#

!e some of them are more helpful than others

print(object())
pure spruceBOT
#

@frank hare :white_check_mark: Your 3.12 eval job has completed with return code 0.

<object object at 0x7fd412854400>
tender gust
#

!close

pure spruceBOT
#
Python help channel closed

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.