This forum is now read-only. Please use our new forums! Go to forums
A proper solution to 3.3 product
def product(x): #defining function
x=list(x) #the input x is a list
y=1 #y is a variable with value 1, didn't choose 0 because..
for i in x: #for every element of list x
y*=i #y*product of all elements [y=1, not 0]
return y
Answer 52c702027c82ca369c0002c1
I think it could be shorter.
def product(lst):
total = 1
for i in lst:
total *= i
return total
Only 5 lines!
Answer 52e15be8631fe9cdbd003449
Answer 52fe3d6b548c3511a50025ac
This works pretty well
def remove_duplicates(x):
x = list(x)
x = list(set(x))
return x
1 comments
it makes x a list, then turns the list into a listed set (grouping numbers to only show once). Then returns the list
Answer 5316f0579c4e9d7d110018f8
Answer 53b97bd452f86378360005a2
def product(li): pro=li[0] for i in range(1,len(li)): pro*=li[i] return pro print product(li) simple but i take more time to find the solution for this exercise
Answer 55466b9c937676d3c5000491
def product(lst):
total = 1
for num in lst:
total *= num
return total
Easiest method, and I assume this is the intended method for this tutorial.
Answer 52b30989282ae3c32a002190
Hmm is this the same as 13/15?
Either way, this is the easiest way of calculating the product of a bunch of arguments:
from numpy import prod
def product(*args):
return int(prod(*args))
3 comments
Well, it might work; but this is not the way one is supposed to do it. Nowhere in the whole Codecademy Python course are we instructed to import product from numpy; so I guess this is not what is expected out of a beginner on Codecademy. Still, good that you know of it.
I think it could be shorter…
Well, if everything we did is just what was “expected,” we wouldn’t have progressed very far now. @Anukul
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
5 comments
sorry but could you please explain what’s the reason and meaning for having “total = 1”
if you did “total = 0”, the the returned value would always be “0” because 0 times anything is…. ZERO.
Hence the use of 1.
If the list only contained the number “1”, our returned value would still be correct.
Let me know if that makes sense!
ditto this is exactly how i did it… first i put 0 instead of 1 and it was returning 0 and looked back and duh… anything times 0 is 0 lol
JAY_123 // You would know that ‘total’ should be integer, so that we allocate ‘total’ for ‘1’.
And the reason that integer is ‘1’, not ‘0’, is well explained by our friends!!
It worked for me!