Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
Sohail MirzaPython Web Development Techdegree Student 5,158 Points
Need 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
Chris FreemanTreehouse Moderator 68,063 Points
In arithmetic, 1 is the multiplicative identity, whereas, 0 is the additive identity:
n = 1 * n n = 0 + n
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
product = args for arg in args[1:]: product *= arg return product
Because you are multiplying with product *= arg, the equivalent to product = product*arg. If product were initialized to 0 the result would always be 0.