Recently started coding in python and I have a question about my code!
In the excercise Practice Makes Perfect - 13.Product you're asked to make a multiplication:
Define a function called product that takes a list of integers as input and returns the product of all of the elements in the list.
For example: product([4, 5, 5]) should return 100 (because 4 * 5 * 5 is 100).
Don't worry about the list being empty.
Your function should return an integer.
I solved this by doing:
def product(grades): score = grades for x in grades[1:len(grades)]: score = score * x return score print product([4, 5, 5])
But the unofficial answers have the following solution:
def product(x): t = 1 for n in x: t *= n return t print product([4, 5, 5])
I was wondering, as I move forward into coding, does the different ways of doing it matter?
Is my option a valid one as long as it works?
Are there any other tips regarding this topic?