[Tutor] "x and y" means "if x is false, then x, else y"??

2010-07-05 Thread Richard D. Moores
See 
.
I am quite familiar with the meaning of "x and y" in Python, and how
it is evaluated -- first x, and only if x is False, then evaluate y.
But I just can't read "if x is false, then x, else y" that way. In
fact, I can't read it at all. Is this a mistake in the Python 3 docs?
If not, can someone tell me how to make sense of it?

BTW I came across this while reading the ingenuously designed and
remarkably clear Chapter 2 of Mark Pilgrim's "Dive Into Python".
(). A link down at the bottom of
, in Section 2.9
"Further Reading, Boolean Operations", took me to that section of the
docs.

Thanks,

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


Re: [Tutor] "x and y" means "if x is false, then x, else y"??

2010-07-05 Thread Steve Willoughby

On 05-Jul-10 00:27, Richard D. Moores wrote:

See.
I am quite familiar with the meaning of "x and y" in Python, and how
it is evaluated -- first x, and only if x is False, then evaluate y.
But I just can't read "if x is false, then x, else y" that way. In
fact, I can't read it at all. Is this a mistake in the Python 3 docs?
If not, can someone tell me how to make sense of it?


Yes, in fact this was a common idiom before Python picked up the (x if p 
else y) syntax, and something of this nature is still commonplace in the 
Perl world (even though it has the ?: operator anyway).


You already know about the "short circuiting" effect of "and" and "or", 
which affects whether the right argument is even evaluated at all, but 
the other piece of this puzzle is that the _return value_ of the 
expression is not a pure Boolean value of True or False, but is in fact 
either the value x or y itself.


So in the case of
x and y
if x is true, then we need to evaluate y, in which case y is returned. 
If y happened to be true, then that means both x and y were true, and 
hence the entire expression is true.  The particular "true" value we 
return here happens to be the (true) value of y.


If y is false, then returning it yields the correct false value for the 
whole expression, although the actual (false) value of y is what we return.


If x if false, then we need go no further and simply return x.

So.


1 and 2  ==>  2(true and true ==> true)
0 and 5  ==>  0(false and true ==> false)
'hello' and '' ==> '' (true and false ==> false)
'xx' and 'yy' ==> 'yy' (true and true ==> true)

x and y  ==>  x if x is false, else y


Some people like using this to set values to defaults if no (true) value 
was input, in a semantically pleasing manner, thus:


def __init__(self, a=None, b=None):
  self.a = a or 123
  self.b = b or 456


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


Re: [Tutor] "x and y" means "if x is false, then x, else y"??

2010-07-05 Thread Richard D. Moores
On Mon, Jul 5, 2010 at 00:55, Steve Willoughby  wrote:
> On 05-Jul-10 00:27, Richard D. Moores wrote:
>>
>>
>> See.
>> I am quite familiar with the meaning of "x and y" in Python, and how
>> it is evaluated -- first x, and only if x is False, then evaluate y.
>> But I just can't read "if x is false, then x, else y" that way. In
>> fact, I can't read it at all. Is this a mistake in the Python 3 docs?
>> If not, can someone tell me how to make sense of it?

> Yes, in fact this was a common idiom before Python picked up the (x if p
> else y) syntax, and something of this nature is still commonplace in the
> Perl world (even though it has the ?: operator anyway).
>
> You already know about the "short circuiting" effect of "and" and "or",
> which affects whether the right argument is even evaluated at all, but the
> other piece of this puzzle is that the _return value_ of the expression is
> not a pure Boolean value of True or False, but is in fact either the value x
> or y itself.
>
> So in the case of
>x and y
> if x is true, then we need to evaluate y, in which case y is returned. If y
> happened to be true, then that means both x and y were true, and hence the
> entire expression is true.  The particular "true" value we return here
> happens to be the (true) value of y.
>
> If y is false, then returning it yields the correct false value for the
> whole expression, although the actual (false) value of y is what we return.
>
> If x if false, then we need go no further and simply return x.
>
> So.
>
>
> 1 and 2  ==>  2(true and true ==> true)
> 0 and 5  ==>  0(false and true ==> false)
> 'hello' and '' ==> '' (true and false ==> false)
> 'xx' and 'yy' ==> 'yy' (true and true ==> true)
>
> x and y  ==>  x if x is false, else y
>
>
> Some people like using this to set values to defaults if no (true) value was
> input, in a semantically pleasing manner, thus:
>
> def __init__(self, a=None, b=None):
>  self.a = a or 123
>  self.b = b or 456

Steve,

Your answer seems very well-formulated. However, I've read it over and
over, but I keep getting hung up over the meaning of "the return
value" of an expression. I am of course familiar with values returned
by a function, but don't quite grasp what the return value of, say,
the y of "x and y" might mean.
Also, you distinguish between a return value of True and and the value
of y being such (say 5, and not 0) that it makes y true (but not
True). So another  thing I need to know is the difference between True
and true.  Also between False and false. And why the difference is
important.

I'm thinking that possibly what would help would be to contextualize
"x and y" in some snippet of code. I'm sorry to trouble you with my
denseness.

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


Re: [Tutor] "x and y" means "if x is false, then x, else y"??

2010-07-05 Thread Stefan Behnel

Richard D. Moores, 05.07.2010 11:37:

I keep getting hung up over the meaning of "the return
value" of an expression. I am of course familiar with values returned
by a function, but don't quite grasp what the return value of, say,
the y of "x and y" might mean.


Think of a different expression, like "1+1". Here, the return value (or 
maybe a better wording would be the result value) is 2.




Also, you distinguish between a return value of True and and the value
of y being such (say 5, and not 0) that it makes y true (but not
True). So another  thing I need to know is the difference between True
and true.  Also between False and false. And why the difference is
important.


"True" is the value "True" in Python, which is a singleton. You can test 
for it by using


x is True

However, other values can have a true values as well, without being True, e.g.

if 1: print("TRUE!!!")

will actuall print the string, as the value 1 is considered true when 
turned into a boolean result.


Stefan

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


Re: [Tutor] "x and y" means "if x is false, then x, else y"??

2010-07-05 Thread Richard D. Moores
On Mon, Jul 5, 2010 at 04:09, Stefan Behnel  wrote:
> Richard D. Moores, 05.07.2010 11:37:
>>
>> I keep getting hung up over the meaning of "the return
>> value" of an expression. I am of course familiar with values returned
>> by a function, but don't quite grasp what the return value of, say,
>> the y of "x and y" might mean.
>
> Think of a different expression, like "1+1". Here, the return value (or
> maybe a better wording would be the result value) is 2.
>
>
>> Also, you distinguish between a return value of True and and the value
>> of y being such (say 5, and not 0) that it makes y true (but not
>> True). So another  thing I need to know is the difference between True
>> and true.  Also between False and false. And why the difference is
>> important.
>
> "True" is the value "True" in Python, which is a singleton. You can test for
> it by using
>
>    x is True

Ah. But could you give me an x that would satisfy that? I can think of

>>> (5 > 4) is True
True

But how can (5 > 4) be an x? Could you show me some code where it could be?

>>> x = (5 > 4)
>>> x
True
>>> x is True
True

So it can! That surprised me.  I was expecting   "x = (5 > 4)"  to be
absurd -- raise an exception? Still seems pretty weird.

> However, other values can have a true values as well, without being True,
> e.g.
>
>    if 1: print("TRUE!!!")
> will actually print the string, as the value 1 is considered true when turned
> into a boolean result.

Yes, I see that.

Well, maybe I'm getting there.

Thanks,

Dick
--
CAUTION: Pseudo Vorticity Advection
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "x and y" means "if x is false, then x, else y"??

2010-07-05 Thread Walter Prins
On 5 July 2010 08:27, Richard D. Moores  wrote:

> See <
> http://docs.python.org/py3k/library/stdtypes.html#boolean-operations-and-or-not
> >.
> I am quite familiar with the meaning of "x and y" in Python, and how
> it is evaluated -- first x, and only if x is False, then evaluate y.
>

Sorry if this is being overly pedantic, but I thought I'd point out the
above isn't right as stated, although I understand what you're getting at
(re short circuit boolean evaluation) in general.  To be correct, I presume
you meant "OR" where you wrote "AND", as it would be correct in that case
e.g:

x AND y: Will only evaluate y if x is TRUE. (If x is FALSE then you don't
need to evaluate y since the resultant expression will be FALSE regardless,
see footnote 2 in the page you referenced.)

x OR y:  Will only evaluate y if x is FALSE. (If x is TRUE then you don't
need to evaluate y since the resultant expression will be TRUE regardless,
see footnote 1 in the page you referenced.)

See e.g. output of this. 

So then, to explain this line from the page you reference: x and y:
"if *x*is false, then
*x*, else *y"

*Think about it: As per the above, if x is false, then because it's false,
Python need only and will only evaluate x, and will therefore essentially
return whatever "x" is when evaluating the expression.  If x is true on the
other hand, then by the above rules, it has to *also* evaluate y as well,
and so will end up effectively returning whatever y returns as it determines
what the truth value of the overall expression is.  Shortening that
reasoning, you can say, "if x is false, then x, else y". See?  (The same
sory of reasoning applies for the "or" case if you think it out.)
*
*Hope that helps.

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


Re: [Tutor] "x and y" means "if x is false, then x, else y"??

2010-07-05 Thread Richard D. Moores
On Mon, Jul 5, 2010 at 04:54, Walter Prins  wrote:
>
>
> On 5 July 2010 08:27, Richard D. Moores  wrote:
>>
>> See
>> .
>> I am quite familiar with the meaning of "x and y" in Python, and how
>> it is evaluated -- first x, and only if x is False, then evaluate y.
>
> Sorry if this is being overly pedantic, but I thought I'd point out the
> above isn't right as stated, although I understand what you're getting at
> (re short circuit boolean evaluation) in general.  To be correct, I presume
> you meant "OR" where you wrote "AND", as it would be correct in that case

Yes, my careless mistake. Instead of "first x, and only if x is False,
then evaluate y" I should have written "first x, and only if x is
True, then evaluate y", right?

> e.g:
>
> x AND y: Will only evaluate y if x is TRUE. (If x is FALSE then you don't
> need to evaluate y since the resultant expression will be FALSE regardless,
> see footnote 2 in the page you referenced.)
>
> x OR y:  Will only evaluate y if x is FALSE. (If x is TRUE then you don't
> need to evaluate y since the resultant expression will be TRUE regardless,
> see footnote 1 in the page you referenced.)
>
> See e.g. output of this.
>
> So then, to explain this line from the page you reference: x and y:  "if x
> is false, then x, else y"
>
> Think about it: As per the above, if x is false, then because it's false,
> Python need only and will only evaluate x, and will therefore essentially
> return whatever "x" is when evaluating the expression.  If x is true on the
> other hand, then by the above rules, it has to *also* evaluate y as well,
> and so will end up effectively returning whatever y returns as it determines
> what the truth value of the overall expression is.  Shortening that
> reasoning, you can say, "if x is false, then x, else y". See?  (The same
> sort of reasoning applies for the "or" case if you think it out.)
>
> Hope that helps.

Wow, it did! Especially that last big paragraph. Thanks, Walter!

Dick
---
CAUTION: Dynamic Inertial Balance
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "x and y" means "if x is false, then x, else y"??

2010-07-05 Thread Adam Bark
On 5 July 2010 12:53, Richard D. Moores  wrote:

> On Mon, Jul 5, 2010 at 04:09, Stefan Behnel  wrote:
> > Richard D. Moores, 05.07.2010 11:37:
> >>
> >> I keep getting hung up over the meaning of "the return
> >> value" of an expression. I am of course familiar with values returned
> >> by a function, but don't quite grasp what the return value of, say,
> >> the y of "x and y" might mean.
> >
> > Think of a different expression, like "1+1". Here, the return value (or
> > maybe a better wording would be the result value) is 2.
> >
> >
> >> Also, you distinguish between a return value of True and and the value
> >> of y being such (say 5, and not 0) that it makes y true (but not
> >> True). So another  thing I need to know is the difference between True
> >> and true.  Also between False and false. And why the difference is
> >> important.
> >
> > "True" is the value "True" in Python, which is a singleton. You can test
> for
> > it by using
> >
> >x is True
>
> Ah. But could you give me an x that would satisfy that? I can think of
>
> >>> (5 > 4) is True
> True
>
> But how can (5 > 4) be an x? Could you show me some code where it could be?
>
> >>> x = (5 > 4)
> >>> x
> True
> >>> x is True
> True
>
> So it can! That surprised me.  I was expecting   "x = (5 > 4)"  to be
> absurd -- raise an exception? Still seems pretty weird.
>

Greater than (>) works like the mathematical operators in returning a value,
it just happens that for comparison operators (>, <, ==, !=) the values can
only be True or False.

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


Re: [Tutor] "x and y" means "if x is false, then x, else y"??

2010-07-05 Thread Adam Bark
On 5 July 2010 13:21, Adam Bark  wrote:

> On 5 July 2010 12:53, Richard D. Moores  wrote:
>
>> On Mon, Jul 5, 2010 at 04:09, Stefan Behnel  wrote:
>> > Richard D. Moores, 05.07.2010 11:37:
>> >>
>> >> I keep getting hung up over the meaning of "the return
>> >> value" of an expression. I am of course familiar with values returned
>> >> by a function, but don't quite grasp what the return value of, say,
>> >> the y of "x and y" might mean.
>> >
>> > Think of a different expression, like "1+1". Here, the return value (or
>> > maybe a better wording would be the result value) is 2.
>> >
>> >
>> >> Also, you distinguish between a return value of True and and the value
>> >> of y being such (say 5, and not 0) that it makes y true (but not
>> >> True). So another  thing I need to know is the difference between True
>> >> and true.  Also between False and false. And why the difference is
>> >> important.
>> >
>> > "True" is the value "True" in Python, which is a singleton. You can test
>> for
>> > it by using
>> >
>> >x is True
>>
>> Ah. But could you give me an x that would satisfy that? I can think of
>>
>> >>> (5 > 4) is True
>> True
>>
>> But how can (5 > 4) be an x? Could you show me some code where it could
>> be?
>>
>> >>> x = (5 > 4)
>> >>> x
>> True
>> >>> x is True
>> True
>>
>> So it can! That surprised me.  I was expecting   "x = (5 > 4)"  to be
>> absurd -- raise an exception? Still seems pretty weird.
>>
>
> Greater than (>) works like the mathematical operators in returning a
> value, it just happens that for comparison operators (>, <, ==, !=) the
> values can only be True or False.
>
> HTH,
> Adam.
>

I should add that this is how something like:

if x != y:
do_something()

works, if expects a True or False (this isn't always true but works for
comparison operators expressions such as this).
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] "x and y" means "if x is false, then x, else y"??

2010-07-05 Thread Steven D'Aprano
On Mon, 5 Jul 2010 07:37:12 pm Richard D. Moores wrote:
> On Mon, Jul 5, 2010 at 00:55, Steve Willoughby  
wrote:
[...]
> Steve,
>
> Your answer seems very well-formulated. However, I've read it over
> and over, but I keep getting hung up over the meaning of "the return
> value" of an expression. I am of course familiar with values returned
> by a function, but don't quite grasp what the return value of, say,
> the y of "x and y" might mean.

At the risk of adding confusion by butting in (I'm also a Steve, just a 
different one...), perhaps a better expression would be just "the 
value" of an expression.

If you have any expression, such as:

23 + 42
"hello world".split()
len("abc")

then the expression has a value:

65
["hello", "world"]
3

That's what Steve means by "the return value".


> Also, you distinguish between a return value of True and and the
> value of y being such (say 5, and not 0) that it makes y true (but
> not True). So another  thing I need to know is the difference between
> True and true.  Also between False and false. And why the difference
> is important.

This is a fundamental part of Python's programming model: every object, 
without exception, can be used where some other languages insist on 
actual boolean flags. In Python, there's nothing special about True and 
False -- until version 2.3, they didn't even exist, and in fact they 
are implemented as a subclass of int. True is an alternative way of 
spelling 1, and False of 0:

>>> True*3
3
>>> False + 5
5

You can see more about the history of bool in Python here:
http://www.python.org/dev/peps/pep-0285/

Since Python allows direct boolean operations (if...else, and, or, not) 
on all objects, we need some rules for deciding what objects are 
considered True-like ("true") and which are considered False-like 
("false"). The distinction Python uses for built-ins is between 
something and nothing: an object which represents something is true, 
and one which represents nothing is false.

The most obvious example comes from numbers. Numbers which are equal to 
zero are obviously Nothing, and hence considered false. All other 
numbers are Something, and hence true.

Similarly for strings: the empty string is a string-flavoured Nothing, 
and all other strings are Something.

Collections -- lists, tuples and dicts -- are considered Nothing if they 
are empty, otherwise Something.

None is a special type of Nothing, and so is considered false.

When it comes to custom-built classes, rather than built-ins, the 
distinction may be a little weaker, since of course the programmer can 
define their class any way they like. Python first looks to see if the 
class has a __nonzero__ method, and if so, calls that. Otherwise it 
calls __len__. If the class has neither of those methods, it is 
automatically considered to be Something. So it's easy to create 
strange classes that don't quite fit into the Something/Nothing 
dichotomy:

class Funny:
def __nonzero__(self):
import time
return time.time() % 2 == 0
# true on even seconds, false on odd seconds

Python is quite happy to let you shoot yourself in the foot if you try.

Ruby has a similar policy, except in Ruby everything is true except for 
two objects: false and nil.

http://www.skorks.com/2009/09/true-false-and-nil-objects-in-ruby/

Other languages may make different choices.

In Python, True and False are merely the canonical true and false 
objects, and bool(x) will return the canonical Boolean value of x:

>>> bool(None)
False
>>> bool("something funny")
True

It is very rare you need to use bool. Don't write this:

if bool(x):
...

that's just a waste of a function call. It's *nearly* as silly as 
writing:

if (x==y) is True:  # Or should that be if (x==y) is True is True ?
...


> I'm thinking that possibly what would help would be to contextualize
> "x and y" in some snippet of code. I'm sorry to trouble you with my
> denseness.

s = some_string_value()
if not s:
print "Empty string"
else:
print "The string starts with", s[0]



people_with_red_hair = "Phil George Susan Samantha".split()
people_with_glasses = "Henry Felicity Michelle Mary-Anne Billy".split()
a = set(people_with_red_hair)
b = set(people_with_glasses)
if not a:
print "There are no red-heads."
if not a.intersection(b):
print "There are no red-heads who also wear glasses."




-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] raw_input

2010-07-05 Thread Dipo Elegbede
Hello,

I seem to be having problems with raw_input.

i wrote something like:

raw_input('Press Enter')

it comes back to tell me raw_input is not defined, a NameError!

Is it that something about it has changed in python 3.1 or I have been
writing the wrong thing.

Please enlighten me.

regards.

-- 
Elegbede Muhammed Oladipupo
OCA
+2348077682428
+2347042171716
www.dudupay.com
Mobile Banking Solutions | Transaction Processing | Enterprise Application
Development
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] endless loop

2010-07-05 Thread prasad rao
hi
  I am trying problem 6 in projecteuler.org.
What is the smallest positive number that is evenly divisible by all
of the numbers from 1 to 20?


def rr(z,m=1):
  q=lambda n:m%n==0
  s=lambda False : 0
  a=filter(s,map(q,range(1,z)))
  if not a:
  m+=1
  rr(z,m)
  else:return m

This code is going into endless loop.

   rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
  File "", line 7, in rr
rr(z,m)
I tried dime a dozen  permutations oF the code.
Can some one show me  why it is going into Endless loop?

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


Re: [Tutor] raw_input

2010-07-05 Thread Shashwat Anand
use input() instead of raw_input() in Python3.x

On Mon, Jul 5, 2010 at 8:50 PM, Dipo Elegbede  wrote:

> Hello,
>
> I seem to be having problems with raw_input.
>
> i wrote something like:
>
> raw_input('Press Enter')
>
> it comes back to tell me raw_input is not defined, a NameError!
>
> Is it that something about it has changed in python 3.1 or I have been
> writing the wrong thing.
>
> Please enlighten me.
>
> regards.
>
> --
> Elegbede Muhammed Oladipupo
> OCA
> +2348077682428
> +2347042171716
> www.dudupay.com
> Mobile Banking Solutions | Transaction Processing | Enterprise Application
> Development
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] raw_input

2010-07-05 Thread Dipo Elegbede
Tried it out and it worked.
Thanks.
Regards,

On 7/5/10, Shashwat Anand  wrote:
> use input() instead of raw_input() in Python3.x
>
> On Mon, Jul 5, 2010 at 8:50 PM, Dipo Elegbede  wrote:
>
>> Hello,
>>
>> I seem to be having problems with raw_input.
>>
>> i wrote something like:
>>
>> raw_input('Press Enter')
>>
>> it comes back to tell me raw_input is not defined, a NameError!
>>
>> Is it that something about it has changed in python 3.1 or I have been
>> writing the wrong thing.
>>
>> Please enlighten me.
>>
>> regards.
>>
>> --
>> Elegbede Muhammed Oladipupo
>> OCA
>> +2348077682428
>> +2347042171716
>> www.dudupay.com
>> Mobile Banking Solutions | Transaction Processing | Enterprise Application
>> Development
>>
>> ___
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>

-- 
Sent from my mobile device

Elegbede Muhammed Oladipupo
OCA
+2348077682428
+2347042171716
www.dudupay.com
Mobile Banking Solutions | Transaction Processing | Enterprise
Application Development
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] endless loop

2010-07-05 Thread Emile van Sebille

On 7/5/2010 8:31 AM prasad rao said...

hi
   I am trying problem 6 in projecteuler.org.
What is the smallest positive number that is evenly divisible by all
of the numbers from 1 to 20?


def rr(z,m=1):
   q=lambda n:m%n==0
   s=lambda False : 0
   a=filter(s,map(q,range(1,z)))
   if not a:
   m+=1
   rr(z,m)
   else:return m

This code is going into endless loop.



You don't show us how you're invoking this function, but it seems to me 
the result of passing a terminally false function result (s is always 
false) into filter will always result in [] (so a is always false) and 
will thereby cause the else clause never to be reached.


Emile


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


Re: [Tutor] raw_input

2010-07-05 Thread Sander Sweers
On 5 July 2010 17:40, Shashwat Anand  wrote:
> use input() instead of raw_input() in Python3.x

To add to this, in Python 2 we had input() [1] (unsafe for most uses)
and raw_input() [2] (safe). Python 3 removed the old input() and
renamed raw_input() to input() [3,4].

Greets
Sander

[1] http://docs.python.org/library/functions.html#input
[2] http://docs.python.org/library/functions.html#raw_input
[3] http://docs.python.org/release/3.0.1/whatsnew/3.0.html#builtins
[4] http://www.python.org/dev/peps/pep-3111/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Running a python script as root.

2010-07-05 Thread Eike Welk
Hello Srihari!

On Sunday July 4 2010 20:17:12 Srihari k wrote:
> I did #chmod +s settime.py so that SUID bit be set and all users can
> execute the script and set the system time.
> now the permissions of file are:
> -rwsr-xr-x 1 root root 165 2010-07-04 23:16 settime.py
> 
> 
> The script still works as before ..
> date: cannot set date: Operation not permitted
> Sun Jul  4 23:37:37 IST 2010

I think this is a security feature of the operating system: SUID root is 
ignored for scripts. Only compiled programs can (IMHO) be SUID root. 

I didn't know that this is true for Python, but Bash scripts can definitely 
not be SUID root. 


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


Re: [Tutor] endless loop

2010-07-05 Thread Alan Gauld


"prasad rao"  wrote



def rr(z,m=1):
 q=lambda n:m%n==0
 s=lambda False : 0


This is always false???


 a=filter(s,map(q,range(1,z)))


So this is always empty?


 if not a:


So this is always true


 m+=1
 rr(z,m)


So you contuinuaslly call rr with the original z and an increasing m.
But nothing about m terminates the recursion, so it recurses 
forever - or until you hit the recursion limit.



 else:return m


This is never executed


This code is going into endless loop.


Yep, I'd say so.


Can some one show me  why it is going into Endless loop?


Because you wrote iit that way. This is one reason recursion is hard,
you must make 100% certain that there is a terminatin condition that 
will somehow get you out again...


HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


[Tutor] Help return a pattern from list

2010-07-05 Thread Vineeth Rakesh
Hello all,

Can some one help me to return a special pattern from a list.

say list =
["something1.mp3","something2.mp3","something4.pdf","something5.odt"]

now say I just need to return the files with .mp3 extension. How to go about
doing this?

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


Re: [Tutor] Help return a pattern from list

2010-07-05 Thread Sander Sweers
On 5 July 2010 19:54, Vineeth Rakesh  wrote:
> Can some one help me to return a special pattern from a list.
>
> say list =
> ["something1.mp3","something2.mp3","something4.pdf","something5.odt"]
>
> now say I just need to return the files with .mp3 extension. How to go about
> doing this?

Use os.path.splitext() to check for the extension and check if it
equals the extension you want. For example like below idle session:

>>> import os
>>> say_list = 
>>> ["something1.mp3","something2.mp3","something4.pdf","something5.odt"]
>>> mp3_list = [x for x in say_list if os.path.splitext(x)[1].lower() == ".mp3"]
>>> mp3_list
['something1.mp3', 'something2.mp3']

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


Re: [Tutor] Help return a pattern from list

2010-07-05 Thread Shashwat Anand
On Mon, Jul 5, 2010 at 11:24 PM, Vineeth Rakesh wrote:

> Hello all,
>
> Can some one help me to return a special pattern from a list.
>
> say list =
> ["something1.mp3","something2.mp3","something4.pdf","something5.odt"]
>

One suggestion. Don't name a list as list. Use l or List or any other
variable name. list is one of the syntax in python.


>
> now say I just need to return the files with .mp3 extension. How to go
> about doing this?
>

>>> list =
["something1.mp3","something2.mp3","something4.pdf","something5.odt"]
>>> [i for i in list if i[-4:] == '.mp3']
['something1.mp3', 'something2.mp3']

or may be ,
>>> [i for i in list if os.path.splitext(i)[0] == '.mp3']  # If you want to
deal with file extentions, that is.
For smaller case string is good, for obscure patter, you can try regex
module.

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


Re: [Tutor] Help return a pattern from list

2010-07-05 Thread Shashwat Anand
On Mon, Jul 5, 2010 at 11:58 PM, Shashwat Anand wrote:

>
>
> On Mon, Jul 5, 2010 at 11:24 PM, Vineeth Rakesh 
> wrote:
>
>> Hello all,
>>
>> Can some one help me to return a special pattern from a list.
>>
>> say list =
>> ["something1.mp3","something2.mp3","something4.pdf","something5.odt"]
>>
>
> One suggestion. Don't name a list as list. Use l or List or any other
> variable name. list is one of the syntax in python.
>
>
>>
>> now say I just need to return the files with .mp3 extension. How to go
>> about doing this?
>>
>
> >>> list =
> ["something1.mp3","something2.mp3","something4.pdf","something5.odt"]
> >>> [i for i in list if i[-4:] == '.mp3']
> ['something1.mp3', 'something2.mp3']
>
> or may be ,
> >>> [i for i in list if os.path.splitext(i)[0] == '.mp3']  # If you want to
> deal with file extentions, that is.
>

Oops, sorry for the typo.
It'll be ,
>>> [i for i in list if os.path.splitext(i)[1] == '.mp3']


>  For smaller case string is good, for obscure patter, you can try regex
> module.
>
>  ~l0nwlf
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] the ball needs a kick...

2010-07-05 Thread Schoap D
Hi,

I'm doing the exercises here: chapter 8
http://www.openbookproject.net/thinkCSpy/ch08.html

Now I have added another paddle to the pong game. So far so good, but the
ball isn't moving anymore and I am not able to fix it...
Any comments, tips, feedback?

Thanks in advance,

http://paste.pocoo.org/show/233739/


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


Re: [Tutor] Help return a pattern from list

2010-07-05 Thread Alan Gauld


"Shashwat Anand"  wrote


list =

["something1.mp3","something2.mp3","something4.pdf","something5.odt"]

[i for i in list if i[-4:] == '.mp3']

['something1.mp3', 'something2.mp3']


Or even easier:


[s for s in list if s.endswith('.mp3')]


But for the specific case of file extensions the os.path.splitext() is
a better solution.


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


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


Re: [Tutor] Help return a pattern from list

2010-07-05 Thread Thomas C. Hicks
On Mon, 05 Jul 2010 20:29:02 +0200
tutor-requ...@python.org wrote:

> Date: Mon, 5 Jul 2010 13:54:55 -0400
> From: Vineeth Rakesh 
> To: tutor@python.org
> Subject: [Tutor] Help return a pattern from list
> Message-ID:
>   
> Content-Type: text/plain; charset="iso-8859-1"
> 
> Hello all,
> 
> Can some one help me to return a special pattern from a list.
> 
> say list =
> ["something1.mp3","something2.mp3","something4.pdf","something5.odt"]
> 
> now say I just need to return the files with .mp3 extension. How to
> go about doing this?
> 
> Thanks
> Vin

I use the fnmatch module:

import fnmatch
fileList =
["something1.mp3","something2.mp3","something4.pdf","something5.odt"]
pattern='*.mp3'
for x in fnmatch.filter(fileList,pattern):
#do something to your files or list items here

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


Re: [Tutor] Help return a pattern from list

2010-07-05 Thread Emile van Sebille

On 7/5/2010 4:19 PM Alan Gauld said...


But for the specific case of file extensions the os.path.splitext() is
a better solution.





If, as the names suggest, the source is the file system, then I'd reach 
for glob.


Emile


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