Using strings in lists in functions - what is i?

n = [“Michael”, “Lieberman”]
def join_strings(words):
result = “”
for i in range(len(words)):
result += words[i]
return result

print join_strings(n)

This code seems to work but I don’t feel like I fully understand it. Where does n come back into play if our function is being defined using the argument (words).

I also don’t 100% grasp what i is? Is this just python syntax for index?

This is a follow up question regarding the following exercise…I know I am missing some obvious logic here:

m = [1, 2, 3]
n = [4, 5, 6]

def join_lists(x, y):
return x + y

Add your code here!

print join_lists(m, n)

You want this to print [1, 2, 3, 4, 5, 6]

If we just defined x, y as the arguments but the lists are (a, b) how does the program understand that we want to concatenate m + n? Don’t we need to define x and y?

i is a variable. It is created by the for loop, and is set to the next word in the list, every time the loop is iterated. Aside from the first and last lines, this is a function. The way functions work, is they carry out a certain section of code, and you can do that many times (For example, do your chores, is a short way of saying take out the trash, mow the lawn, etc.). Functions can take in parameters(what is in the parentheses), so you can pass information into the function. As in the first example, putting n in the function parentheses is basically saying:

words = n

This is used so you can modify words without modifying the original input, so this function can be used for different purposes. The function in your example(join_strings), as implied in the name, joins two strings. No matter what you input, the output will be a combination of those two strings. For example:

def join_strings(words):
    result = ""
    for i in range(len(words)):
    result += words[i]
    return result

n = ["Michael", "Lieberman"]
print join_strings(n) #will return MichaelLieberman

t = ["Hello", "World"]
print join_strings(t) #will return HelloWorld

z = ["Does this make", " sense?"]
print join_strings(z) #will return Does this make sense?


Like I said, join_lists(m, n), basically is defining x and y, as m and n are being passed into the function, making x = m, and y = n


Thank you for the outstanding explanation!