Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > A certainl part of an if() structure never gets executed.

Reply
Thread Tools

A certainl part of an if() structure never gets executed.

 
 
Chris Angelico
Guest
Posts: n/a
 
      06-14-2013
On Fri, Jun 14, 2013 at 6:03 PM, Nick the Gr33k <(E-Mail Removed)> wrote:
>>>> print(name or month or year)
>>>> print("k" in (name and month and year))


Seriously, you can't see the difference between those lines? Either
you're trolling - which is still a distinct possibility, but so is the
converse - or there's something between here and the northern
hemisphere that makes the obvious unobvious and vice versa. Or maybe
I'm actually living in Alice's Wonderland and nothing here makes
sense...

ChrisA
 
Reply With Quote
 
 
 
 
R. Michael Weylandt
Guest
Posts: n/a
 
      06-14-2013
On Fri, Jun 14, 2013 at 9:03 AM, Nick the Gr33k <(E-Mail Removed)> wrote:>
>>>> 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' ?


No it's not. See both above (where you use 'or' instead) and below
where _you yourself_ show that it's not 'abcd.'

Now read: https://en.wikipedia.org/wiki/Short-circuit_evaluation
noting especially the specified behavior for Python. If you find it
too technical, google for other uses of the terms.

>
>>>> 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.


Think about basic logic: 'or' means 'is at least one true?' so Python
only has to look at the first 'truthy value'. 'and' means 'are they
all true?' so Python has to look at all the values, ending up with the
last one, unless a 'falsey value' is found before.

Michael
 
Reply With Quote
 
 
 
 
R. Michael Weylandt
Guest
Posts: n/a
 
      06-14-2013
On Fri, Jun 14, 2013 at 9:24 AM, R. Michael Weylandt
<(E-Mail Removed)> wrote:
> On Fri, Jun 14, 2013 at 9:03 AM, Nick the Gr33k <(E-Mail Removed)> wrote:>
>>
>> No clue. since the expression in parenthesis returns 'abcd' how can 'k'
>> contained within 'abcd' ?

>
> No it's not. See both above (where you use 'or' instead) and below
> where _you yourself_ show that it's not 'abcd.'


s/it's not/it doesn't return/g

Typos always and forever,
MW
 
Reply With Quote
 
Jussi Piitulainen
Guest
Posts: n/a
 
      06-14-2013
Nick the Gr33k writes:

> >>> 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' ?


Why shouldn't (name or month or year) be different from (name and
month and year)?

Incidentally, you get better information without the print():

>>> 'Parker' and 'May' and '2001'

'2001'
>>>


>>> 'Parker' and 'May' and 2001

2001
>>>


Either way, the interactive prompt is your friend.
 
Reply With Quote
 
Fbio Santos
Guest
Posts: n/a
 
      06-14-2013
On 14 Jun 2013 09:09, "Nick the Gr33k" <(E-Mail Removed)> wrote:
> >>> 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.
>


You have been told this above.

All languages kind of do that. Ever seen this command on a shell?

mkdir directory && cd directory

The shell evaluated the first and if that was truthy it went on to evaluate
the second and return that.

Now. You've been told countless times that you won't get anything from "not
in (a and b and c)", nor from "not in (a or b or c)".

Also you have been shown this link and I feel you really need to read it.

http://slash7.com/2006/12/22/vampires/

Cheers

 
Reply With Quote
 
Nick the Gr33k
Guest
Posts: n/a
 
      06-14-2013
On 14/6/2013 11:28 πμ, Jussi Piitulainen wrote:

>>>> 'Parker' and 'May' and '2001'

> '2001'


But why?

that expression should return True since all stings are not empty.

> Either way, the interactive prompt is your friend.
>



--
What is now proved was at first only imagined!
 
Reply With Quote
 
Nick the Gr33k
Guest
Posts: n/a
 
      06-14-2013
On 14/6/2013 11:03 πμ, Nick the Gr33k wrote:
> On 14/6/2013 4:14 πμ, Steven D'Aprano wrote:
>> On Thu, 13 Jun 2013 17:26:18 +0300, Νικόλαος Κούρας wrote:
>>
>>> i just want 4 cases to examine so correct execute to be run:
>>>
>>> i'm reading and reading and reading this all over:
>>>
>>> if '-' not in ( name and month and year ):
>>>
>>> and i cant comprehend it.

>>
>> Don't just read it. Open the interactive interpreter and test it.
>>
>> name = "abcd"
>> month = "efgh"
>> year = "ijkl"
>>
>> print(name and month and year)
>>
>> If you run that, you will see what the result of
>> (name and month and year) is. Now, ask yourself:
>>
>> "k" in (name and month and year)
>>
>> True or false? Check your answer:
>>
>> print("k" in (name and month and year))

>
>
> >>> 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' ?
>
> >>> 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.
>
> >>> print("k" in (name and month and year))

> True
> >>>

>
> yes, since expression returns 'ijkl', then the in operator can detect
> the 'k' character within the returned string.
>


Someone want to explain this?

--
What is now proved was at first only imagined!
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      06-14-2013
On Fri, Jun 14, 2013 at 6:41 PM, Nick the Gr33k <(E-Mail Removed)> wrote:
> On 14/6/2013 11:28 , Jussi Piitulainen wrote:
>
>>>>> 'Parker' and 'May' and '2001'

>>
>> '2001'

>
>
> But why?
>
> that expression should return True since all stings are not empty.


It does. Not the bool value, but it does return a true value.

ChrisA
 
Reply With Quote
 
Chris Angelico
Guest
Posts: n/a
 
      06-14-2013
On Fri, Jun 14, 2013 at 6:44 PM, Nick the Gr33k <(E-Mail Removed)> wrote:
> Someone want to explain this?


Stop writing. Start reading. It has been explained. In the course of a
long and adventurous thread in the principal European courts, it has
been revealed to you that ...

Fill in whatever you like for the rest, it's probably all been
revealed at some point already.

ChrisA
 
Reply With Quote
 
Nick the Gr33k
Guest
Posts: n/a
 
      06-14-2013
On 14/6/2013 11:57 πμ, Chris Angelico wrote:
> On Fri, Jun 14, 2013 at 6:44 PM, Nick the Gr33k <(E-Mail Removed)> wrote:
>> Someone want to explain this?

>
> Stop writing. Start reading. It has been explained. In the course of a
> long and adventurous thread in the principal European courts, it has
> been revealed to you that ...
>
> Fill in whatever you like for the rest, it's probably all been
> revealed at some point already.
>
> ChrisA
>


Well i do not understand it.
Had to use:

if '-' not in name + month + year:
cur.execute( '''SELECT * FROM works WHERE clientsID = (SELECT id FROM
clients WHERE name = %s) and MONTH(lastvisit) = %s and YEAR(lastvisit) =
%s ORDER BY lastvisit ASC''', (name, month, year) )
elif '-' not in name + year:
cur.execute( '''SELECT * FROM works WHERE clientsID = (SELECT id FROM
clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER BY lastvisit
ASC''', (name, year) )
elif '-' not in month + year:
cur.execute( '''SELECT * FROM works WHERE MONTH(lastvisit) = %s and
YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', (month, year) )
elif '-' not in year:
cur.execute( '''SELECT * FROM works WHERE YEAR(lastvisit) = %s ORDER
BY lastvisit ASC''', year )

to am eit work.

but i really wont to understand how 'or' and 'and' works inside an
expression. please answer my previous post if you know.

--
What is now proved was at first only imagined!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
semple code accept never executed fakessh C Programming 11 09-15-2012 05:46 AM
Using callback, I fill dorpdownlist without postback but it gets empty when a postback is executed Fabio Mastria ASP .Net 5 01-30-2008 09:07 PM
server event never/always executed phil ASP .Net Web Controls 1 06-06-2006 12:00 PM
ORIGINAL CODE GETS EXECUTED NOT THE NEW CODE - ASCX sk ASP .Net 1 04-19-2006 11:50 AM
Page_Unload gets executed twice chefo@bulgaria.com ASP .Net 0 01-21-2005 01:44 PM



Advertisments