On 15/6/2013 3:14 πμ, Cameron Simpson wrote:
On 14Jun2013 12:50, Nikos as SuperHost Support <supp...@superhost.gr> wrote:
| I started another thread because the last one was !@#$'ed up by
| irrelevant replies and was difficult to jeep track.
|
| >>> name="abcd"
| >>> month="efgh"
| >>> year="ijkl"
|
| >>> print(name or month or year)
| abcd
|
| Can understand that, it takes the first string out of the 3 strings
| that has a truthy value.
|
| >>> print("k" in (name and month and year))
| True
|
| No clue. since the expression in parenthesis returns 'abcd' how can
| 'k' contained within 'abcd' ?
Did you print the result of "name and month and year"? It is the
_last_ value (if true at all). You used "or" in the first example
and "and" in the second.
okey, lets see it again:
>>> print (name or month or year)
abcd
>>>
Yes, 'k' isn't contained in the result string 'abcd'
>>> print (name and month and year)
hijk
>>> print( "k" in (name and month and year) )
True
Yes they work as expected, i was mistaken, sorry.
| >>> print(name and month and year)
| ijkl
|
| Seems here is returning the last string out of 3 strings, but have
| no clue why Python doing this.
To evaluate an "and" it must test all of them to be true, and it
keeps the last value tested. (Or False, of course, if they are not
all true, in which case Python stops testing at the first False).
Yes, i know it behaves like that, the question is why:
As "Nobody" explained to me, the reason is that Python expressions
results back the argument that determined the evaluation of the Boolean
expression, which in turn can be a truthy or a falsey used in 'or' or
'and' respectively.
Returning a truthy value equals True
returning a falsey value equals False
so it all boils down to the Booleans type of values True or False.
But for what you are doing, "and" and "or" are not good operations.
Something like:
"k" in (name+month+year)
or
"k" in name or "k" in month or "k" in year
Used to wrote it myself like the latter but needed a more compact way of
writing it for clarity so i used the former.
but those 2 gives the same results back
"k" in (name+month+year) == "k" in (name and month and year)
True
so both seem to work as expected.
--
What is now proved was at first only imagined!
--
http://mail.python.org/mailman/listinfo/python-list