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

  • Thread starter Íéêüëáïò Êïýñáò
  • Start date
C

Chris Angelico

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
 
R

R. Michael Weylandt

abcd

Can understand that, it takes the first string out of the 3 strings that has
a truthy value.

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

R. Michael Weylandt

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
 
J

Jussi Piitulainen

Nick said:
abcd

Can understand that, it takes the first string out of the 3 strings
that has a truthy value.

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():

Either way, the interactive prompt is your friend.
 
F

Fábio Santos

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
 
N

Nick the Gr33k

abcd

Can understand that, it takes the first string out of the 3 strings that
has a truthy value.

True

No clue. since the expression in parenthesis returns 'abcd' how can 'k'
contained within 'abcd' ?

ijkl

Seems here is returning the last string out of 3 strings, but have no
clue why Python doing this.


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

Someone want to explain this?
 
C

Chris Angelico

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
 
N

Nick the Gr33k

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

Chris Angelico

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

*eyeroll*

You have all the information. Go play with it in the interactive
interpreter until you understand. Seriously. That interpreter wants to
be your friend, just extend a hand and say "Hi"! Become familiar with
Python that way. Don't expect everything to be answered on-list.

ChrisA
 
J

Jussi Piitulainen

Nick said:
But why?

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

It returns a value that counts as true in a conditional statement or
expression:
.... else: print("didn't")
....
got a truish value
.... else: print("didn't")
....
didn't

Zeroes and empty things tend to count as false in Python, other values
as true. The values are tested as is, not coerced to a boolean first,
so the value that decides the value of the whole expression is the
value of the whole expression.
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: name 'whatever' is not defined
 
N

Nick the Gr33k

It returns a value that counts as true in a conditional statement or
expression:

When a look at ('Parker' and 'May' and '2001') i can't help it but
interpret it as:

Return True if 'Parker' is not an empty string AND 'May' is not an
empty string AND'2001' is not an empty string.

Why on eart doesn't work this way?

I can understand that the value it results to '2000' is a truthy value,
although i was expecting it to result in True if all parts of
expressions are true.

i just don't understand why it returns back the last value instead.
Zeroes and empty things tend to count as false in Python, other values
as true. The values are tested as is, not coerced to a boolean first,
so the value that decides the value of the whole expression is the
value of the whole expression.

''

Why does it return th first object back
isn't it like saying False and True and resulting in False?

Please put it in else word how Python unerstand that.

Same here? The 2nd part of the expression never is been calculated
because the 1st is False?

Same here? The 2nd part of the expression never is been calculated
because the 1st is False?
 
N

Nick the Gr33k

*eyeroll*

You have all the information. Go play with it in the interactive
interpreter until you understand. Seriously. That interpreter wants to
be your friend, just extend a hand and say "Hi"! Become familiar with
Python that way. Don't expect everything to be answered on-list.

ChrisA

but i'm doing this all day long i just dont comprehend why it works this
way.
it doesn't make any sense to me.
 
D

Denis McMahon

So, i must tell:

for i, month in enumerate(months):
print('<option value="%s"> %s </option>' % (i, month) )

to somehow return '==========' instead of 0 but don't know how.

You could test for (month == 0) instead of re.search('=', month)?

Or you could try using "==========" instead of i when i is 0

for i, month in enumerate(months):
if i != 0:
print('<option value="%s"> %s </option>' % (i, month) )
else:
print('<option value="%s"> %s </option>' % ("==========", month) )

But if you couldn't work either of these solutions out on your own,
perhaps the really important question you need to be asking yourself
right now is "should I even be trying to code this in python when my
basic knowledge of python coding is so poor?"
 
N

Nick the Gr33k

for i, month in enumerate(months):
if i != 0:
print('<option value="%s"> %s </option>' % (i, month) )
else:
print('<option value="%s"> %s </option>' % ("==========", month) )

This s exactly what i was looking for Denis, thank you.

I tough of that myself too, but i had implemented it wrongly as:

for i, month in enumerate(months):
if i == 0:
i = "=========="
else:
print('<option value="%s"> %s </option>' % (i, month))


I just cant think simple and clear some times.

Of course as you also said i could have left it as it it is and then
look for month == 0 in the if condition instead of month == "============"
 
J

Jussi Piitulainen

Nick said:
When a look at ('Parker' and 'May' and '2001') i can't help it but
interpret it as:

Return True if 'Parker' is not an empty string AND 'May' is not an
empty string AND'2001' is not an empty string.

Nah. That expression would be:

True if ('Parker' != '' and 'May' != '' and '2001' != '') else False

The one at hand is more like: 'Parker' if 'Parker' does not count as
true, else 'May' if 'May' does not count as true, else '2001' (which
counts as true if it counts as true, else it counts as false ...).
i just don't understand why it returns back the last value instead.

I suppose the value can be useful, and there is generally no harm in
it. But why not adjust your expectations to the reality? You can still
ask why.

This behaviour of the /and/ and /or/ was used to simulate the
conditional expression (_ if _ else _) before the latter was in the
language.
Why does it return th first object back
isn't it like saying False and True and resulting in False?

Please put it in else word how Python unerstand that.

That would be: '' if '' counts as false, else whatever.

And yes, when the first expression determines the value, the second
expression is not evaluated. /or/ is similar.
 
N

Nick the Gr33k

Nah. That expression would be:

True if ('Parker' != '' and 'May' != '' and '2001' != '') else False

The one at hand is more like: 'Parker' if 'Parker' does not count as
true, else 'May' if 'May' does not count as true, else '2001' (which
counts as true if it counts as true, else it counts as false ...).


I suppose the value can be useful, and there is generally no harm in
it. But why not adjust your expectations to the reality? You can still
ask why.

This behaviour of the /and/ and /or/ was used to simulate the
conditional expression (_ if _ else _) before the latter was in the
language.


That would be: '' if '' counts as false, else whatever.

And yes, when the first expression determines the value, the second
expression is not evaluated. /or/ is similar.

Thanks for explaining this but i cannot follow its logic at all.
My mind is stuck trying to interpret it as an English sentence:

if ('Parker' and 'May' and '2001')

if ('Parker' or 'May' or '2001')

i just don't get it and i feel silly about it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top