Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSohail Mirza
Python Web Development Techdegree Student 5,158 PointsNeed help with one line of the code
I don't understand why product =1 , why isnt it product = 0. What is the logic behind it
def multiply(*args):
product = 1
for arg in args:
product *= arg
return product
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsIn arithmetic, 1 is the multiplicative identity, whereas, 0 is the additive identity:
n = 1 * n
n = 0 + n
That is,
5 * 4 * 3 == 1 * 5 * 4 * 3
So, initializing the product to 1 will allow the loop to be seeded with a neutral starting value.
You could also initialize product to the first item in *args
then loop over the remaining args
:
product = args[0]
for arg in args[1:]:
product *= arg
return product
KRIS NIKOLAISEN
54,971 PointsBecause you are multiplying with product *= arg, the equivalent to product = product*arg. If product were initialized to 0 the result would always be 0.