+ 1

Question about assigning & len()

I was solving the “Longest Word” thing in python core First I came up with a possible solution which I still think should work, but there was something weird https://code.sololearn.com/cd18GGXS0mfu/?ref=app (the origin one was alot cleaner and had fewer variables it got a bit messy when I was trying to figure out what was wrong)

12th Mar 2023, 4:37 AM
AI 0523
14 Réponses
+ 3
AI 0523, strings belong to the "immutable types". You can't change them in any way, so for you as a Python user it doesn't matter if it's the same object or not. (Different versions of Python may handle it differently.) However with "mutable" Types, objects that can be altered, like a list where you can put stuff in and take it out, there it absolutely matters, if it's merely a similar list or the very same list. I know my tutorial is a bit lengthy, but I think you won't regret it reading and thinking it through a few times - because in Python you just have to know how this works.
13th Mar 2023, 6:12 PM
HonFu
HonFu - avatar
+ 2
I explain what you're dealing with in this educational code: https://code.sololearn.com/c89ejW97QsTN/?ref=app
12th Mar 2023, 12:08 PM
HonFu
HonFu - avatar
+ 1
As far as i know, when i do things like b=a; c=a; d=a; isnt it like making a clone of a, and whatever i do to the clones the origin variable a doesnt change, right?
12th Mar 2023, 4:40 AM
AI 0523
+ 1
As you said, the functions change the original variable as well as all thr clones, they are "connected". It's not the len() function which gives you that change, it's that "b[x] = int(len(b[x]))" line. You assign a new value to every "b[x]" which also gets assigned to the duplicate variables, a and d. No magic happening there, just how Python works.
12th Mar 2023, 10:19 AM
Paleon
Paleon - avatar
+ 1
And also you were right with the len() function - it doesn't change any of the content, it just gives you an information about the length of something - but you assigned the value to "b[x]" and that's where the things started to mess up.
12th Mar 2023, 10:20 AM
Paleon
Paleon - avatar
+ 1
AI 0523, is what exactly the same for strings?
13th Mar 2023, 11:44 AM
HonFu
HonFu - avatar
0
But in mine the part for x in range(len(d)): b[x]=int(len(b[x])) actually changed the original variable a, which i thought should only effect the clone(b) I also tried changing the part int(len(b[x])) into int(len(d[x])) or int(len(a[x])) but they did the same thing
12th Mar 2023, 4:46 AM
AI 0523
0
I solved it by setting it all independent txt = input() a=txt.split(" ") b=txt.split(" ") d=txt.split(" ") But the thing is, what i thought was right… len() function on a clone doesnt change the original variable(Actually len(a) doesnt even change the value of a, its just used to count the lengths and use it, not to change the variable itself) and i cant figure out why it happened in the post code
12th Mar 2023, 4:58 AM
AI 0523
0
One possible reason i think is its becuz variable a has a method .split() and when the len() effects it it somehow also changes all the related variables, especially the clone thing i did But im not sure and if im right i have no idea why
12th Mar 2023, 5:10 AM
AI 0523
0
Paleon oh the duplicates variables are all connected..? I just tested it and it is with lists, but does it work with strings too?
12th Mar 2023, 11:48 AM
AI 0523
0
AI 0523 Basically when you create a new variable based on a previously created one (for instance "b = a"), the variable "b" itself "remembers" that it was created as a copy of the variable "a". That is why it changes also when the other variable is changed. It is a concept of Python which messes up a lot of codes, so it is great that you learnt about it now!
12th Mar 2023, 1:19 PM
Paleon
Paleon - avatar
0
Paleon HonFu thx for the explanation But is it the same with strings?
13th Mar 2023, 11:13 AM
AI 0523
0
The duplicated variables being connected… it is with lists but i dont think it is with strings
13th Mar 2023, 5:51 PM
AI 0523
0
oh i get it thx for the explanation..!
14th Mar 2023, 11:11 AM
AI 0523