Accumulate , Range and Zeros

2019-07-13 Thread Abdur-Rahmaan Janhangeer
Greetings,

Given this snippet

from itertools import *
import operator


x = [1, 2, 3] # [0, 1, 2, 3, ..., 10]

y = accumulate(x, operator.mul)

print(list(y))

why does x = list(range(5)) produces only zeros?

-- 
Abdur-Rahmaan Janhangeer
http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Accumulate , Range and Zeros

2019-07-13 Thread Frank Millman

On 2019-07-13 11:54 AM, Abdur-Rahmaan Janhangeer wrote:

Greetings,

Given this snippet

from itertools import *
import operator


x = [1, 2, 3] # [0, 1, 2, 3, ..., 10]

y = accumulate(x, operator.mul)

print(list(y))

why does x = list(range(5)) produces only zeros?



That is an easy one.

By default, range() starts from 0. Anything multiplied by 0 equals 0. So 
you can multiply as many numbers as you like, if the first one is 0, the 
rest will also be 0.


QED

Frank Millman
--
https://mail.python.org/mailman/listinfo/python-list


Re: Accumulate , Range and Zeros

2019-07-13 Thread Dan Sommers




On 7/13/19 5:54 AM, Abdur-Rahmaan Janhangeer wrote:
> > Greetings,
> >
> > Given this snippet
> >
> > from itertools import *
> > import operator
> >
> >
> > x = [1, 2, 3] # [0, 1, 2, 3, ..., 10]
> >
> > y = accumulate(x, operator.mul)
> >
> > print(list(y))
> >
> > why does x = list(range(5)) produces only zeros?

I see two things going on here.

(1) Don't type snippets of code and results from memory, or
in bits and pieces from an interactive session.  Copy and
paste exactly the code that ran and the output that it
produced.

(2) What is range(5)?  Okay, what is list(range(5))?  What
do you (the person) get when you multiply those five
integers together?

HTH,
Dan
--
https://mail.python.org/mailman/listinfo/python-list


Re: Accumulate , Range and Zeros

2019-07-13 Thread Abdur-Rahmaan Janhangeer
@Frank

So simple. Thanks!

Abdur-Rahmaan Janhangeer
http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ
Mauritius
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Accumulate , Range and Zeros

2019-07-13 Thread Thomas Jollans
On 13/07/2019 11:54, Abdur-Rahmaan Janhangeer wrote:
> Greetings,
> 
> Given this snippet
> 
> from itertools import *
> import operator
> 
> 
> x = [1, 2, 3] # [0, 1, 2, 3, ..., 10]
> 
> y = accumulate(x, operator.mul)
> 
> print(list(y))
> 
> why does x = list(range(5)) produces only zeros?

What would you expect it to produce?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Accumulate , Range and Zeros

2019-07-13 Thread Abdur-Rahmaan Janhangeer
@Thomas thought was a generator bug since instead of returning the usual
nums it was returning 0 0 0 ...
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Accumulate , Range and Zeros

2019-07-13 Thread Chris Angelico
On Sat, Jul 13, 2019 at 10:02 PM Abdur-Rahmaan Janhangeer
 wrote:
>
> @Thomas thought was a generator bug since instead of returning the usual
> nums it was returning 0 0 0 ...

When you find a bug or strange bit of behaviour, first make sure you
can reproduce it. Then create a small, self-contained program, just
enough to demonstrate what's going on. Post that code. Don't make us
guess :)

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Accumulate , Range and Zeros

2019-07-13 Thread Abdur-Rahmaan Janhangeer
oh not a real bug, i thought the effect had to do with generator working
rather a simple times 0

Abdur-Rahmaan Janhangeer
Mauritius

On Sat, 13 Jul 2019, 16:24 Chris Angelico,  wrote:

> On Sat, Jul 13, 2019 at 10:02 PM Abdur-Rahmaan Janhangeer
>  wrote:
> >
> > @Thomas thought was a generator bug since instead of returning the usual
> > nums it was returning 0 0 0 ...
>
> When you find a bug or strange bit of behaviour, first make sure you
> can reproduce it. Then create a small, self-contained program, just
> enough to demonstrate what's going on. Post that code. Don't make us
> guess :)
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Books for Python 3.7

2019-07-13 Thread RIchy M
On Friday, July 12, 2019 at 7:34:10 PM UTC-4, Louis Krupp wrote:
> On Fri, 12 Jul 2019 07:36:54 -0700 (PDT), mok...@gmail.com wrote:
> 
> >Can anyone help me.
> >New to Python.
> >Installed version 3.7
> >I purchased the "Python for Dummies" book But this book was written for an 
> >older version of Python.
> >All the examples and samples don't work with version 3.7
> >Can anyone direct me to which is the latest book to buy to properly learn 
> >Python.
> >Thanks
> 
> I found _Introducing Python_ useful:
> 
> http://shop.oreilly.com/product/0636920028659.do
> 
> It's also available on Amazon.
> 
> Louis

Thanks you all for all the advice.
At least now I know there is a Python 2 or 3.
I just bought a book and jumped right in without knowing all the facts first.

I found a free online course by Guru99 that I am trying out now.
I just need some basic knowledge to start of with.

And as Andrew Z suggested, its good to have a Pet Project.
I will do that as soon as I feel more comfortable with Python.

Thanks Louis for the book recommendation.

-- 
https://mail.python.org/mailman/listinfo/python-list