[Tutor] (no subject)

2015-06-10 Thread rakesh sharma

I have come across this syntax in python. Embedding for loop in []. How far can 
things be stretched using this [].
l = [1, 2, 3, 4, 5]
q = [i*2 for i in l]
print qthanksrakesh   
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2015-06-10 Thread Joel Goldstick
On Wed, Jun 10, 2015 at 6:36 AM, rakesh sharma
 wrote:
>
> I have come across this syntax in python. Embedding for loop in []. How far 
> can things be stretched using this [].
> l = [1, 2, 3, 4, 5]
> q = [i*2 for i in l]
> print qthanksrakesh

This is called a list comprehension.  They are a more concise way of
writing various for loops.  You can google to learn much more
-- 
Joel Goldstick
http://joelgoldstick.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] (no subject)

2015-06-10 Thread Alan Gauld

On 10/06/15 11:36, rakesh sharma wrote:


I have come across this syntax in python. Embedding for loop in []. How far can 
things be stretched using this [].
l = [1, 2, 3, 4, 5]
q = [i*2 for i in l]


This is a list comprehension which is a specific form of
the more generalised "generator expression":

 for item in iterable if 

And it is possible to have multiple loops and
complex expressions and conditions.

How far they can be taken is a good question.
They can be taken much further than they should be,
to the point where the code becomes both unreadable
and untestable.

Keep them relatively simple is the best advice.

There is no shame in unwrapping them to something
like:

aList = []
for item in anIterable:
if condition:
   aList.append(expression)

If it is more maintainable and testable.
You can always wrap them up again later if it
is needed for performance reasons.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generate Prime Numbers

2015-06-10 Thread Mirage Web Studio



On 2015-05-31 5:04 AM, Alan Gauld wrote:

On 30/05/15 19:14, George wrote:


Excuse me please for replying late.

I got lists to use the method and it is more efficient and faster. 
(Takes about 10 secs to process first 50 mil numbers)


But now another problem i seem to notice that only 1 core of my amd 
Athlon X2 4core processor is being used.  I suppose if all the four 
cores are simultaneously used then the programme might run even faster.  
Is there a way.


Kindly guide me.

Thank You.

George




---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generate Prime Numbers

2015-06-10 Thread Laura Creighton
In a message of Wed, 10 Jun 2015 23:11:36 +0530, Mirage Web Studio writes:
>
>
>
>On 2015-05-31 5:04 AM, Alan Gauld wrote:
>> On 30/05/15 19:14, George wrote:
>
>Excuse me please for replying late.
>
>I got lists to use the method and it is more efficient and faster. 
>(Takes about 10 secs to process first 50 mil numbers)
>
>But now another problem i seem to notice that only 1 core of my amd 
>Athlon X2 4core processor is being used.  I suppose if all the four 
>cores are simultaneously used then the programme might run even faster.  
>Is there a way.
>
>Kindly guide me.
>
>Thank You.
>
>George

If you want, you can use the STM branch of the pypy interpreter.  This
is a Python without the global interpreter lock.  One of the tests
we did was, surprise, to calculate prime numbers.

See the blog post here:
http://morepypy.blogspot.se/2014/11/tornado-without-gil-on-pypy-stm.html

Laura

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Generate Prime Numbers

2015-06-10 Thread Alan Gauld

On 10/06/15 18:41, Mirage Web Studio wrote:


But now another problem i seem to notice that only 1 core of my amd
Athlon X2 4core processor is being used.  I suppose if all the four
cores are simultaneously used then the programme might run even faster.
Is there a way.


One of the problems with the "standard" CPython implementation is
that it only uses one CPU, even if you write threaded code. There
are implementations that address that, but I've never used them
so can't comment on whether they would help in your case.

I'm also not sure how Jython or IronPython handle things, they
might use their internal threading engines more effectively.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor