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.

Python Python Collections (2016, retired 2019) Tuples Packing

Sohail Mirza
seal-mask
.a{fill-rule:evenodd;}techdegree
Sohail Mirza
Python 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

twoples.py
def multiply(*args):
    product = 1
    for arg in args:
        product *= arg
    return product

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,063 Points

In 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

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.