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

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

Sibylle Koczian

Am 12.06.2013 20:06, schrieb Îικόλαος ΚοÏÏας:
Whn i see:

if( x and y ):
i understand: if x expression = True AND ALSO y expression = True then
execute


if( x or y ):
i understand: if x expression = True OR y expression = True then execute
You didn't read MRABs explanation, did you?
if '=' not in ( name and month and year ):
i understand: if '=' not in name AND '=' not in month AND '=' not in year
Wrong. The "'=' not in (...)" first evaluates the expression in
parentheses, that's what parentheses are for. And then it looks for '='
in the result. And that result is just one of the three values, MRAB
told you which one.
if '=' not in ( name or month or year ):
i understand: if '=' not in name OR '=' not in month OR '=' not in year
Same here. You can't take the "'=' in" out of the parentheses, that
leads to a wrong result.
but i know it does not work like this, but tis is how i understand it.

??? If you know it doesn't work like this, then it won't help you to
wilfully understand something you'd like to be true.
its like reading an English sentence
No, this time it isn't.

HTH
Sibylle
 
Î

Îικόλαος ΚοÏÏας

Wrong. The "'=' not in (...)" first evaluates the expression in
parentheses, that's what parentheses are for. And then it looks for '='
in the result. And that result is just one of the three values, MRAB
told you which one.

okey first the expression eval:

( name and month and year ) = ( name=True and month=True and year=True )

then if '=' not in (name = True and month = True and year = True)

I still do not follow how this works. it just doesn't make any sense to
me at all.


if '=' not in ( name and month and year ):
if '=' not in ( name or month or year ):

I try to read both of them them as an English sentence but i cannot.
how can you be able to understand this?
 
C

Chris Angelico

If you still don't know why you get "0" read:
http://www.w3schools.com/tags/att_option_value.asp (or something in
greek about html forms)
(Sorry, I know, you do not read doks, because they describe what the software
DOES and not what you WANT it to DO)

Please try to avoid linking to w3schools; it has a number of flaws,
which are distinctly off-topic for this list, but mainly it is
non-authoritative. Equivalent and usually more thorough information
can be found in other places, such as Mozilla's Developer pages:

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option

Though he probably won't read it whatever the page is, so this is for
the archives more than anything else.

ChrisA
 
C

Chris Angelico

okey first the expression eval:

( name and month and year ) = ( name=True and month=True and year=True )

then if '=' not in (name = True and month = True and year = True)

I still do not follow how this works. it just doesn't make any sense to me
at all.



if '=' not in ( name and month and year ):
if '=' not in ( name or month or year ):

I try to read both of them them as an English sentence but i cannot.
how can you be able to understand this?

Go read the docs. In Python, 'in' is an operator. It behaves like an
operator. It does not behave like magic. Stop expecting magic of
Python.

ChrisA
 
S

Sibylle Koczian

Am 12.06.2013 22:00, schrieb Îικόλαος ΚοÏÏας:
okey first the expression eval:

( name and month and year ) = ( name=True and month=True and year=True )
No. Read MRABs post, he explains it. Or work through the tutorial. This
would be right in another language, but not in Python.

If this expression would really evaluate to True or False, you
definitely couldn't search for any character in the result.

As it is, it evaluates to a string or to None, but searching for '=' in
that string doesn't give the result you think it does.

Greetings
Sibylle
 
S

Steven D'Aprano

doesn't that mean?

if '=' not in ( name and month and year ):

if '=' does not exists as a char inside the name and month and year
variables?

i think it does, but why it fails then?

No. Python is very close to "English-like", but not exactly, and this is
one of the easiest places to trip.

In English:

"the cat is in the box or the cupboard or the kitchen"

means:

"the cat is in the box, or the cat is in the cupboard, or the cat is in
the kitchen".


But that is not how Python works. In Python, you have to say:

cat in box or cat in cupboard or cat in kitchen


Although this will work as well:

any(cat in place for place in (box, cupboard, kitchen))


In Python, an expression like this:

cat in (box or cupboard or kitchen)


has a completely different meaning. First, the expression in the round
brackets is evaluated:

(box or cupboard or kitchen)


and then the test is performed:

cat in (result of the above)


The expression (box or cupboard or kitchen) means "return the first one
of box, cupboard, kitchen that is a truthy value, otherwise the last
value". Truthy values are those which are considered to be "like True":

truthy values:

- True
- object()
- numbers apart from zero
- non-empty strings
- non-empty lists
- non-empty sets
- non-empty dicts
- etc.

falsey:

- False
- None
- zero (0, 0.0, Decimal(0), Fraction(0), etc.)
- empty string
- empty list
- empty set
- empty dict
- etc.

(Can you see the pattern?)


So you can experiment with this yourself:

42 or 23 or "foo"
=> the first object is truthy, so it is returned

0 or 23 or "foo"
=> the first object is falsey, and the second object is truthy,
so it is returned

0 or [] or "foo"
=> the first two objects are falsey, so the third is returned


The "and" operator works in a similar fashion. Experiment with it and see
how it works for yourself.
 
C

Chris Angelico

In English:

"the cat is in the box or the cupboard or the kitchen"

means:

"the cat is in the box, or the cat is in the cupboard, or the cat is in
the kitchen".


But that is not how Python works. In Python, you have to say:

cat in box or cat in cupboard or cat in kitchen

Or you can deem that there be one single location that is the merging
of box, cupboard, and kitchen, and say:

cat in (box+cupboard+kitchen)

which works fine for character-in-string and element-in-list searches.

ChrisA
 
K

Kushal Kumaran

Chris Angelico said:
Or you can deem that there be one single location that is the merging
of box, cupboard, and kitchen, and say:

cat in (box+cupboard+kitchen)

which works fine for character-in-string and element-in-list searches.

hm...

In [1]: s1 = 'abc'

In [2]: s2 = 'def'

In [3]: s3 = 'ghi'

In [4]: 'cd' in s1 or 'cd' in s2 or 'cd' in s3
Out[4]: False

In [5]: 'cd' in s1+s2+s3
Out[5]: True
 
C

Chris Angelico

In [4]: 'cd' in s1 or 'cd' in s2 or 'cd' in s3
Out[4]: False

In [5]: 'cd' in s1+s2+s3
Out[5]: True

That's why I said it works for *character* in string, not *string* in
string. If your first operand is a single character (which in Python
is still of type 'str'), then the equivalency holds.

ChrisA
 
Î

Îικόλαος ΚοÏÏας

doesn't that mean?

if '=' not in ( name and month and year ):

if '=' does not exists as a char inside the name and month and year
variables?

i think it does, but why it fails then?

No. Python is very close to "English-like", but not exactly, and this is
one of the easiest places to trip.

In English:

"the cat is in the box or the cupboard or the kitchen"

means:

"the cat is in the box, or the cat is in the cupboard, or the cat is in
the kitchen".


But that is not how Python works. In Python, you have to say:

cat in box or cat in cupboard or cat in kitchen


Although this will work as well:

any(cat in place for place in (box, cupboard, kitchen))


In Python, an expression like this:

cat in (box or cupboard or kitchen)


has a completely different meaning. First, the expression in the round
brackets is evaluated:

(box or cupboard or kitchen)


and then the test is performed:

cat in (result of the above)


The expression (box or cupboard or kitchen) means "return the first one
of box, cupboard, kitchen that is a truthy value, otherwise the last
value". Truthy values are those which are considered to be "like True":

truthy values:

- True
- object()
- numbers apart from zero
- non-empty strings
- non-empty lists
- non-empty sets
- non-empty dicts
- etc.

falsey:

- False
- None
- zero (0, 0.0, Decimal(0), Fraction(0), etc.)
- empty string
- empty list
- empty set
- empty dict
- etc.

(Can you see the pattern?)


So you can experiment with this yourself:

42 or 23 or "foo"
=> the first object is truthy, so it is returned

0 or 23 or "foo"
=> the first object is falsey, and the second object is truthy,
so it is returned

0 or [] or "foo"
=> the first two objects are falsey, so the third is returned


The "and" operator works in a similar fashion. Experiment with it and see
how it works for yourself.

First i wan tot thank you for taking the time to explain to me (the
languages examples to explai encode-decode was really great as well)


So, baring in ming your explanation:

if '=' not in ( name or month or year )

first eval the expr. The expr will result to return the first object
that has a truthy values.

in this particular example all 3 strings are truthies because they all
contain characters inside them, the one user submitted when hitting the
submit button

so that would turn to if '=' not in ( name ).
Allright, but what i wanted to check is if the char '=' ain't contained
in both 3 strings not for the 1st string(name) which will always be true.

So i guess having it like this "if '=' not in ( name or month or year )"
is wrong?


b) Also what "if '=' not in ( name and month and year )" returns in the
round brackets since in my case all the strings have values therefore
they are true, which string get returned?
 
Î

Îικόλαος ΚοÏÏας

doesn't that mean?

if '=' not in ( name and month and year ):

if '=' does not exists as a char inside the name and month and year
variables?

i think it does, but why it fails then?

No. Python is very close to "English-like", but not exactly, and this is
one of the easiest places to trip.

In English:

"the cat is in the box or the cupboard or the kitchen"

means:

"the cat is in the box, or the cat is in the cupboard, or the cat is in
the kitchen".


But that is not how Python works. In Python, you have to say:

cat in box or cat in cupboard or cat in kitchen


Although this will work as well:

any(cat in place for place in (box, cupboard, kitchen))


In Python, an expression like this:

cat in (box or cupboard or kitchen)


has a completely different meaning. First, the expression in the round
brackets is evaluated:

(box or cupboard or kitchen)


and then the test is performed:

cat in (result of the above)


The expression (box or cupboard or kitchen) means "return the first one
of box, cupboard, kitchen that is a truthy value, otherwise the last
value". Truthy values are those which are considered to be "like True":

truthy values:

- True
- object()
- numbers apart from zero
- non-empty strings
- non-empty lists
- non-empty sets
- non-empty dicts
- etc.

falsey:

- False
- None
- zero (0, 0.0, Decimal(0), Fraction(0), etc.)
- empty string
- empty list
- empty set
- empty dict
- etc.

(Can you see the pattern?)


So you can experiment with this yourself:

42 or 23 or "foo"
=> the first object is truthy, so it is returned

0 or 23 or "foo"
=> the first object is falsey, and the second object is truthy,
so it is returned

0 or [] or "foo"
=> the first two objects are falsey, so the third is returned


The "and" operator works in a similar fashion. Experiment with it and see
how it works for yourself.

I read yours psots many times,all of them, tryign to understand them.


if '-' not in ( name and month and 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 and 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 and 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 )

======================

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.

While it seems so beutifull saying:

if character '-' aint contained in string name , neither in string month
neither in string year.

But it just doesn't work like this.

Since ( name and month and year ) are all truthy values, what is
returned by this expression to be checked if it cotnains '=' within it?
 
S

Sibylle Koczian

Am 13.06.2013 09:11, schrieb Îικόλαος ΚοÏÏας:
I read yours psots many times,all of them, tryign to understand them.

But you didn't do what he recommended, did you? And it's really the best
or possibly the only way to understanding.

Try it out in the interactive Shell, using Stevens examples connected
with 'and' instead of 'or'.

Sibylle
 
N

Neil Cerutti

He's not a troll, he's a help vampire:

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

... a particularly extreme example, I'll admit: his lack of
consideration for others apparently knows no bounds. The email thing
is just another aspect of that.

He's also changed his NNTP-Posting-Host, just yesterday, along
with at least three changes in From address, and one change in
Reply-To.

And to start with he came here with an obvious troll name.
 
Î

Îικόλαος ΚοÏÏας

Am 13.06.2013 09:11, schrieb Îικόλαος ΚοÏÏας:

But you didn't do what he recommended, did you? And it's really the best
or possibly the only way to understanding.

Try it out in the interactive Shell, using Stevens examples connected
with 'and' instead of 'or'.

I try and try to work it out but i can't understand it even in theory.

==================

if '-' not in ( name and month and 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 and 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 and 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 )

======================

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.

While it seems so beautiful saying:

if character '-' ain't contained in string name , neither in string
month neither in string year.

But it just doesn't work like this.

Since ( name and month and year ) are all truthy values, what is
returned by this expression to be checked if it cotnains '=' within it?
 
Î

Îικόλαος ΚοÏÏας

Am 12.06.2013 22:00, schrieb Îικόλαος ΚοÏÏας:
No. Read MRABs post, he explains it. Or work through the tutorial. This
would be right in another language, but not in Python.

If this expression would really evaluate to True or False, you
definitely couldn't search for any character in the result.

As it is, it evaluates to a string or to None, but searching for '=' in
that string doesn't give the result you think it does.

(name and month and year) is a Boolean expression, correct?

It will return True if all three are True and False otherwise. I cannot
use it the way i desire regardless of how logical i think it looks.

Basically its just like i'am saying:


if "-" in True:
or
if "-" in False:

Obviously when i write it this way you can see it makes no sense.

======================
But (name or month or year) is also a Boolean expression.

It will return the first of the three depending which value comes first
as truthy.
it will return False if none of the three are False.

So how am i going to write it that?

if '-' not in name and '-' not in month and '-' not in year: ??????
 
Î

Îικόλαος ΚοÏÏας

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 )


This finally worked!
 
S

Steven D'Aprano

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


While it seems so beautiful saying:

if character '-' ain't contained in string name , neither in string
month neither in string year.

But it just doesn't work like this.

Correct. It doesn't work that way.
Since ( name and month and year ) are all truthy values, what is
returned by this expression to be checked if it cotnains '=' within it?

Stop asking these questions. Try it for yourself and learn for yourself.
You have a Python interactive interpreter. Try things, and see what they
do. Read the documentation. THEN, and ONLY after you have done these
things, should you ask for help.
 
N

Nick the Gr33k

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

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

Forum statistics

Threads
474,138
Messages
2,570,803
Members
47,348
Latest member
nethues

Latest Threads

Top