[CentralOH] 2014-11-21 道場 Scribbles 落書/惡文?
pybokeh
pybokeh at gmail.com
Tue Nov 25 03:58:04 CET 2014
For those wondering what it means when someone say Python code is idiomatic
or Pythonic, here is a video that I thought was helpful by a great speaker
/ Python core developer named Raymond Hettinger:
https://www.youtube.com/watch?v=OSGv2VnC0go
On Mon, Nov 24, 2014 at 7:17 PM, <jep200404 at columbus.rr.com> wrote:
> very common code pattern in many programming languages:
>
> loop executes n times
> first value is zero (NOT 1)
> last value in loop is n-1 (NOT n)
> test is < n.
> comparison is <, not <=
> compared value is n, not n-1
> next value is made at end of loop
>
> >>> def do_n_times(n):
> ... i = 0
> ... while i < n:
> ... print i
> ... i += 1
> ...
> >>> do_n_times(3)
> 0
> 1
> 2
> >>>
>
> A more Pythonic way of doing the above follows.
>
> >>> def do_n_times(n):
> ... for i in range(n):
> ... print i
> ...
> >>> do_n_times(3)
> 0
> 1
> 2
> >>>
>
> i is often used to index into data that one is going through.
> However in Python, one can often go through data one thing at a time,
> without have an index,
> so one often does something like the following fake Python code:
>
> >>> def do_stuff(data_collection):
> ... for thing in data_collection:
> ... print thing
> ...
> >>> do_stuff(['hello', 3.14159+2.71828j, True, 42])
> hello
> (3.14159+2.71828j)
> True
> 42
> >>>
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> https://mail.python.org/mailman/listinfo/centraloh
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20141124/072f70b1/attachment-0001.html>
More information about the CentralOH
mailing list