Welcome to the Treehouse Community
Looking to learn something new?
Adam Ryde5,222 Points
Iterable and values
Hi guys, completed python basics, and i am now starting to work through python collections, i am only on the first part really and i have come across something that is a little confusing.
within the video lesson Kenneth explains in the teachers notes that for .append() a value goes in the parentheses, but on .extend() an iterable goes in the parentheses.
does anyone have a simple way of explaining the difference of a value and an iterable to me in lamens terms please.
i would google this, but i would like to hear it from people on here as i am sure some of you will have thought the same thing at some point also.
many thanks in advance. Adam
Ryan S27,276 Points
A value and an iterable are not necessarily different things. An iterable could be thought of as a type of value. Anytime you create a variable and store something in it, like a number or string or list, that something is a value. So a string is a value that is iterable.
In programming, iteration refers to the process of performing a task or set of tasks over and over. An iterable is something that allows you to loop through its contents and perform those repeated tasks. Examples of an iterable would be a list, dictionary or even a string.
var1 = [1, 2, 3, 4] var2 = "Treehouse" var3 = 5
In the above examples, everything to the right of the equal signs is a value, but only the values of
var2 are iterable. You can use a
for loop to loop through either the items in the list, or the letters in the string. But you cannot loop through
var3 as it is only a single integer.
Hope this clears things up.
james southFront End Web Development Techdegree Graduate 33,258 Points
an iterable is anything that can be iterated or looped through, so a word is iterable, a list, a tuple, a dict etc. a value is a number or letter generally, or really anything, you can append iterables for example to make a list of lists. but generally you will have iterables made up of values, like a word is made up of letters, or a list might be a collection of names, which are in turn made up of letters, etc. you will get an error trying to iterate through an integer as they are not iterable. they must be converted to something that is iterable, like a string.
Adam Ryde5,222 Points
ahhh ok so if i think of values as either letters or numbers (ints) and iterables as strings, lists etc?
would that be a simple way of looking at it at this early stage of my learning?