Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Jason Pallone
895 Pointswhats iterable and a muteable?
I know its a dumb question but I keep getting confused on the meanings, can someone simplify them for me please haha
2 Answers
Aaron Loften
12,864 PointsAbsolutely not a dumb question. The problem here is one of them are very very confusing to answer.
Iterable - Means it can be looped through. Eg of iterable:
boom = "meow"
We can say boom[2] and get back "o" We can even use a for loop and assign "letter" to each index of boom. [index being each spot a character could take up. Such as "m", "e", "o", or "w". A space can hold an index as well. Anything that can be stored in a variable has an index]
Eg of non-iterable:
boom=True
The above is a boolean value. This means it can be true or it can be false. Not true as in "True", but true as in right or wrong. It is not the characters "t", "r", "u", "e". This means a for loop would fail. Although, atleast in javascript, you can make an array of booleans and those are iterable. Such as boom=[True, False, False]. However, each index is the full true/false value. Such as boom[0] would then be True, not "T".
Believe it or not, that was the easy answer.... -_-
Mutable....Dont get lost on my first response to mutable. Its seriously a little confusing.
A mutable object can be reset when using it. Its original value can be altered without redeclaring it. Consider the following...
#mutable list
boom = ["holla", "bro"]
bam = boom;
bam+=["yous", "a", "jive", "turkey"]
print("Bam = " + str(bam))
print("Boom = " + str(boom))
The above prints out...
Bam = ['holla', 'bro', 'yous', 'a', 'jive', '
turkey']
Boom = ['holla', 'bro', 'yous', 'a', 'jive',
'turkey']
How can this be, you ask? We never touched the value of boom.... It's because it mutable. I dont understand what the purpose is in this. It seems like a flaw. A bug. Im not sure why you would ever want to set a variable to the value of another variable and then change the value of this variable, and then later use both variables, which are now equal to the same value. The point in assigning boom to bam is so that I had a new starting point. sigh....again, I think it was a bug that we grew to live with. Someone should comment below an actual use case for this that makes it a helpful situation since it seems silly.
An immutable object would be a value that would not update when calling it. I THINK it returns a new instance of it. Like in the example below, I think it is saying I want to return the value of boom into the value of bam, not boom itself, just its value. Again, I know, confusing...
#immutable strings
boom = "holla bro"
bam = boom;
bam+=" yous a jive turkey"
print("Bam = " + bam)
print("Boom = " + boom)
Which returns...
Bam = holla bro yous a jive turkey
Boom = holla bro
So end of the day, this is weird....
HOWEVER, there is a way to make a list or really ANY object immutable(which...I think is desirable). Consider the following(which is close to the first example)
#immutable list - whaaaat?
boom = ["holla", "bro"]
bam = list(boom);
bam+=["yous", "a", "jive", "turkey"]
print("Bam = " + str(bam))
print("Boom = " + str(boom))
Returns...
Bam = ['holla', 'bro', 'yous', 'a', 'jive', 'turkey']
Boom = ['holla', 'bro']
Because the "list()" function is used to return a new instance of a variable's value that was previous immutable, I figured it would return a new instance of a list, and it does. So....theres that. :p
Henrik Christensen
Python Web Development Techdegree Student 38,322 Points- mutable: a mutable object can be changed after it is created
- iterable: I think this is best descriped as an item that can be looped through
Jason Pallone
895 PointsThank you for the answer!
Jason Pallone
895 PointsJason Pallone
895 PointsThank you for the answer! So a iterable and muteable are similar like boom = 'hello' so boom is iterable sense it can be looped through, and a mutable sense it can also be changed?
Henrik Christensen
Python Web Development Techdegree Student 38,322 PointsHenrik Christensen
Python Web Development Techdegree Student 38,322 Pointsstrings are not mutable - try to do this:
This would throw an error: TypeError: 'str' object does not support item assignment
To change a string you would have to split it into an list and make the change(s), and then join it back into a string
Aaron Loften
12,864 PointsAaron Loften
12,864 PointsNo problem!
Eh, no. To get more technical about it, mutable refers to the amount of space it currently takes up bit-wise on the system. I dont fully understand the bit-to-space conversion or technical details though I think it means that a string, integer, and float take up one space because they are one value and a list takes up multiple spaces because they have multiple values.
So...to answer your follow up question, boom is iterable because it has a length but is not mutable because it only has the potential for one value.
boom = ["holla", "my", "broooooo"] is mutable because it rewrites the value when you use it(might only be when you do math on it, im not sure) but this means that boom = ["holla"] is also mutable because it has the potential to have multiple values. both boom the string and boom the list are iterable because they either have multiple values or have multiple indices to go through. eg: word="boom" word[0]="b" word[1]="o" word[2]="o" word[3]="m"
An integer is not iterable because it is a single value. eg: number=1234 number[0]=1234 (though I think it would fail trying to call it by an index such as [0] but I think its important to show it has no real index because of its value)
A string with a number in it or a list is different. eg: boom="1234" boom[3]="4" eg: boom=[1234, "123", 1456] boom[0]=1234 boom[1]="123" boom[1][2]=3
Does that help ya? I know, its confusing. Basic rule of thumb would be to becareful when using any variable that could have multiple values attached to it when applying math unless you make it a new instance(eg: new_list=list(the_old_list)).
:D
Aaron Loften
12,864 PointsAaron Loften
12,864 PointsHenrick is right, you cant edit an immutable through variable[index]="stuff" Though you can call that index for a value.
Look at this example
However, you can still reassign a value to the string by redeclarring the full value, not just the index. Thats why this gets really confusing. Good example, Henrick. :)
Jason Pallone
895 PointsJason Pallone
895 PointsThank you Henrik and Aaron !! Ok so a string is a iterable and not muteable but a list is a iterable and muteable? for example boom = 'hello' is a iterable but not muteable because it can be looped but not changed? boom = ['h', 'e', 'l', 'l', 'o'] is a muteable and a iterable because it can be changed and looped through? boom = True is not muteable or a iterable, because it is a boolean?