The usage of the word “append” in these instructions means literally appending some word to the result string, not to be confused with the .append() function available to lists.
We can append something to a string by taking its current value and adding to it the string we want to be at the end! This is possible using the += operator, which is used as: my_variable += some_value
It takes the existing value of the variable and adds on to it.
n = [“Michael”, “Lieberman”] def join_strings(words):
_ result = ‘’_
_ for item in range(len(words)):_
_ x = result + words[item]_
_ result = x _
_ return result_
print join_strings(n)
I find this pretty interesting so when I run the code the first time I had… x = words[item] + result
And the result printed out made the last name[1] first and the first name[0] last. So when I switched it around like it is in my code at the top it switched it the other way first name[0] first last name[1] last. What I’m wondering is is that a function in Python that I’m not aware of yet? Yes Im just assuming that in the iteration of the for loop it would go by the number of the iteration [ie 0,1,2,3] and not having an effect or not being affected by what position my list words [item] is when adding to a variable result in the function. Seems like it shouldn’t make an effect in any kind of order and I’m assuming the iterations go in chronological order no matter what order we put the list or the variable. It seems like I am incorrect in that assumption.
You say chronological order, but that suggests some very spooky interaction between operations. Generally speaking there isn’t any such interaction anywhere in python, or most languages.
The information of which one you acquired first isn’t included in that operation.
You can think of the + operator as this function:
def add(a, b):
return a + b
If you made this function, how would you know which of a and b was acquired first by the caller?
Okay so so I’m fully understanding if I say I have a list[a,b] and item number 0 which happens to be “a” i want in variable X and I want to iterate through the list then if I place the list first I’m saying the first item in the variable I want is ‘b’?
That a would come first?
no, a is the operand that was provided on the left side
def add(left, right):
return left + right
And if they switch places your function would receive them switched as well (how else would a function tell the different arguments apart in for example division)
And, in particular, when would this behaviour be useful, how would you reverse it if you meant to switch them, and where would that information be stored anyway? All of those speak against this being the case, and the fact that you yourself observed them switching when you switched them also speaks against it.
Maybe I’m thinking about this incorrectly in a for loop I’m saying I want item into a variable and if I took the first one put it in there it would be in position zero and then if I shoved another one in their it would I’m assuming would push the item in zero over to one and fill zero with the new item so it’s kind of like a reverse .append? Does that sound right?
That information simply doesn’t exist unless you put it there yourself. (Which is kind of what programming languages are - the only things that will ever happen is those that you write)
But, again, think about how this would affect division.
def div(a, b):
return a / b
b is the divisor. If you want to divide by b, but suddenly they swap around because you obtained b first…It would be completely unworkable and surprising, you’d never be able to write a correct program
Or for your specific example, you already answered how to do that in your very first post, you said what you changed and what the effect was.
If you don’t like how that is expressed you could write it in half a dozen other ways.
You might want to experiment with concatenating two string literals, it should be rather obvious which one ends up on which side: 'abra' + 'cadabra'
You might also want to add lots of print statements between every single operation that was carried out to watch it step by step
No it’s not empty the first time takes the first item the second time it takes second item, get some still having a difficult and how I asked for the last one of the items first
I’m thinking you have explained it and it’s just going over my head which is unfortunate and I don’t want to frustrate you. Think I’m going to as you suggested practice on the printing out strings and seeing the results and maybe it will light bulb in my brain.
As an aside, the generally preferred way to create strings is to store all the small parts in some iterable and then join them all at once. Otherwise you’re making lots of strings sort of like this:
['a', 'b', 'c', 'd'] <- concat these
'' <- shouldn't have made this in the first place
'a' <- shouldn't have made this in the first place
'ab' <- shouldn't have made this in the first place
'abc' <- shouldn't have made this in the first place
'abcd' <- just make this one
''.join(['a', 'b', 'c', 'd']) this produces 'abcd'
(that’s not the same string “growing”, those are completely new and separate copies with an additional letter)
I’ve just managed to complete the exercise using the ‘range’ function, but it doesn’t seem to work with the normal way (simple loop). Below is my code:
n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(words):
result = ""
for word in range(len(words)):
result += words[word]
return result
print join_strings(n)
n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(words):
result = ""
for word in words:
result += words[word]
return result
print join_strings(n)
n = ["Michael", "Lieberman"]
# Add your function here
def join_strings(words):
result = ""
for word in words:
result += word
return result
print join_strings(n)