# Purify-This works, but I don't know how? Explanations anyone?

#1

Here is my code! I did this with a little help from others solution; however, i'm still very new at this and am still not sure what i'm doing means. Can anyone help explain to me how this code works line for line?

``````def purify(x):
numbers = []
for i in x:
if i % 2 == 0:
numbers.append(i)
return numbers
print purify([1,2,3,4])``````

#2

def purify(x): #You are declaring a function called purify and saying that it needs a value import which will be saved as x.

number = [] #You are creating a variable called number and the variable type is list and the list is empty.

for i in x: #you are creating a for loop that is going to loop through the list (x) by how many index values(i) there are

if i % 2 == 0 #You are checking to see if the index value the loop is currently on is completely divisible by 2

numbers.append(i) #You are saying that if i is divisible by 2 then add or append it to the numbers list

return numbers #After the loops completes its iteration and all the values are added, it will send the value of numbers back to where the function was called.

print purify([1,2,3,4]) #You are calling the function to take the numbers in parentheses and process them in the manner that I just explained and after processing, take the value that was returned and print it.

Hope this helps

#3

Thanks, this is exactly what I needed!

#4

No problem, it can get pretty confusing. I get confused sometimes and I have been doing it for years lol. As you start programming more and learning the syntax, it will come naturally. Good luck!

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.