Re: sum() vs. loop

2021-10-11 Thread Steve Keller
Christian Gollwitzer writes: > > def sum_products(seq1, seq2): > > return sum([a * b for a, b in zip(seq1, seq2)]) > > def sum_products2(seq1, seq2): > > sum = 0 > > for a, b in zip(seq1, seq2): > > sum += a * b > > return sum > > [...] > > The first version construct

sum() vs. loop

2021-10-11 Thread Steve Keller
I have found the sum() function to be much slower than to loop over the operands myself: def sum_products(seq1, seq2): return sum([a * b for a, b in zip(seq1, seq2)]) def sum_products2(seq1, seq2): sum = 0 for a, b in zip(seq1, seq2): sum += a * b return sum In a program

Re: Different "look and feel" of some built-in functions

2021-09-24 Thread Steve Keller
"Dieter Maurer" writes: > Steve Keller wrote at 2021-9-24 11:48 +0200: > >Why do some built-in Python functions feel so differently: > > Because the typical use cases are different > > [...] > > >while other functions like set.union() and set.intersect

Different "look and feel" of some built-in functions

2021-09-24 Thread Steve Keller
Why do some built-in Python functions feel so differently: For example sum(), all(), any() expect exactly one argument which is a sequence to operate on, i.e. a list, an iterator or a generator etc. sum([1,2,3,4]) sum(range(1, 101)) sum(2**i for i in range(10)) all([True, False])

Style qeustion: Multiple return values

2021-04-12 Thread Steve Keller
Just a short style question: When returning multiple return values, do you use parenthesis? E.g. would you write def foo(): return 1, 2 a, b = foo() or do you prefer def foo(): return (1, 2) (a, b) = foo() Steve -- https://mail.python.org/mailman/listinfo/py

Animated functions with matplotlib

2020-06-03 Thread Steve Keller
I am trying to plot a live signal using matplotlib and it really drives me crazy. I have tried dozens of variants, with and without interactive (plt.ion()), with animation.FuncAnimation and doing things by hand, calling plt.show() and/or plt.draw() but nothing works as expected. One problem is th

Generators, generator expressions, and loops

2018-11-16 Thread Steve Keller
Cancel ill-formated article -- https://mail.python.org/mailman/listinfo/python-list

Iterators of iterators

2018-11-16 Thread Steve Keller
I wonder why iterators do have an __iter__() method? I thought iterable objects would have an __iter__() method (but no __next__()) to create an iterator for it, and that would have the __next__() method but no __iter__(). $ python3 Python 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5

Generators, generator expressions, and loops

2018-11-16 Thread Steve Keller
I have looked at generators, generator expressions, and iterators and I try to get more familiar with these. 1. How would I loop over all (with no upper bound) integers or all powers of two, for example? In C it would be for (int i = 0; ; i++) { ... } or for (int i = 1; ; i *= 2) { ... } I

Generators, generator expressions, and loops

2018-11-16 Thread Steve Keller
I have looked at generators, generator expressions, and iterators and I try to get more familiar with these. 1. How would I loop over all (with no upper bound) integers or all powers of two, for example? In C it would be for (int i = 0; ; i++) { ... } or for (int i = 1; ; i *= 2) { ... } I

Why do integers compare equal to booleans?

2018-11-16 Thread Steve Keller
Why do the integers 0 and 1 compare equal to the boolean values False and True and all other integers to neither of them? $ python3 Python 3.5.2 (default, Nov 12 2018, 13:43:14) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information.