Reputation: 27
I'm very new to python and I just don't understand why the value of 'a' doesn't change even after 'orange' has been appended. len(x) becomes 4 but 'a', which is supposed to be len(x) stays 3. Thankyou for your time.. code-
x = ['apple', 'banana', 'cherry']
a = len(x)
print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)
x.append('orange')
print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)
Upvotes: 0
Views: 700
Reputation: 1115
Most answers explained the how part. Let me explain why python behaves like this.
Python like all other languages reads a script from top to bottom. That means whatever it stored from or changed in the upper part of code stays the same if you don't explicitly change.
Consider the first part of your code:
x = ['apple', 'banana', 'cherry']
a = len(x)
print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)
In this case
Now consider the second part of your code:
x.append('orange')
print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)
To rectify this, you could reassign a like this:
a = len (x)
Now python would recalculate the length of modified list and assign it again to a. The problem here is an integer is immutable. Hence python would calculate the new value (4); store it in RAM; and reference a to point that value (4). Since the original value (3) is not going to be referenced anymore, that value (3) will be discarded by python. (Eventually removed from RAM by garbage collector)
Upvotes: 1
Reputation: 156
you need to change the value of a after you have changed the length of the list. a still remains 3 until and unless you say a= len(x)
again. this would change its value to 4
appended code:
x = ['apple', 'banana', 'cherry']
a = len(x)
print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)
x.append('orange')
a = len(x) #this has to be done
print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)
Upvotes: 1
Reputation: 336
a
variable refers to len(mylist)
: the length of the first list (before any modifications).
It points to the object returned by len(mylist)
which is an integer (immutable).
Lists are mutable, hence you can modify them inplace.
But a
still refers to the previous integer from len(mylist)
.
Hence you have to call again len(mylist)
after you modified mylist
and assign it back to a
so a
get updated.
Upvotes: 1
Reputation: 11
The answer is pretty simple:
x = ['apple', 'banana', 'cherry']
a = len(x)
print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)
x.append('orange')
a = len(x) # Note this extra line.
print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)
The extra line serves to change the value of a. In the beginning, a = len(x) makes a = 3 since len(x) = 3. However, after len(x) becomes 4, we must again assign the value of len(x) to a (that is make a = len(x)). Not doing this means the variable a stays stuck at 3.
Python programs and almost all other programming languages run top to bottom. Once, python runs a particular line, it will keep moving downwards and not repeat that line (unless you specifically ask the Python interpreter to do so).
Upvotes: 1
Reputation:
i dont have enough reputations to comment so : you must change the value of a varrible then the value will be changed.
x = ['apple', 'banana', 'cherry']
a = len(x)
print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)
x.append('orange')
#Add this to change for example:
a = len(x)
print('list: ',x)
print('Len_x:',(len(x)))
print('a :',a)
Upvotes: 3