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

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

rusi

:

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.

You've been advised many times to experiment in the Python
interpreter. I may be mistaken, but I don't recall seeing any evidence
at all that you've ever done so.

Try the following in a Python interpreter:

Carefully study the results you get. This is simple, basic stuff;
don't come back here asking for explanations of it. If you get stuck,
*carefully* read this article:

 http://en.wikipedia.org/wiki/Short-circuit_evaluation

Repeat the steps above until you do understand. If all else fails,
google "short circuit logic" or "short circuit evaluation python" or
similar search terms, until you find a resource which you do follow.

 -[]z.

You get my prize 'Zero' for best answer!

[You've also given me a nice example for my next python class -- I
usually spend time showing how to play in the interpreter. And the
examples I usually give are numeric/string/list based. Short-circuit
evaluation is good to show. So thanks]

Incidentally, you have also proved right Nicolas' claim that this is
helpful to all :) All that is needed is that other charitable-to-
Nick souls on this list should exercise some restraint and provide the
answers that they *know* he *needs* rather than what he *claims* to
*want*.
 
N

Nick the Gr33k

:

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.

You've been advised many times to experiment in the Python
interpreter. I may be mistaken, but I don't recall seeing any evidence
at all that you've ever done so.

Try the following in a Python interpreter:

Carefully study the results you get. This is simple, basic stuff;
don't come back here asking for explanations of it. If you get stuck,
*carefully* read this article:

http://en.wikipedia.org/wiki/Short-circuit_evaluation

Repeat the steps above until you do understand. If all else fails,
google "short circuit logic" or "short circuit evaluation python" or
similar search terms, until you find a resource which you do follow.

-[]z.
(a or b or c)

is like saying True or True or True.
the first of these 3 variables that hasn;t as value an emptry string,
which means they contain a truthy value, that variable's value will be
returned

For 'and' operator, i do not understand it at all.
 
S

Steven D'Aprano

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.

Python is not English. You just have to accept that, no matter how much
you wish it worked like English, it does not.

Think of it like this:

"If today is Tuesday AND I finish work early, then I can go to the
movies."

Unless *both* conditions are true, I cannot go to the movies.


"If today is Tuesday AND I finish work early AND I have more than $16
spare cash to pay for a ticket, then I can go to the movies."

All three conditions must be true, or I cannot go to the movies.


If today is Monday, I don't need to check whether I finish work early, or
whether I have spare cash. It is enough to know that today is not
Tuesday, so I'm not going to the movies.


Python works the same way:

today_is_tuesday = True
finish_work_early = True
spare_cash = 11


if today_is_tuesday and finish_work_early and spare_cash > 16:
print("Going to the movies")
else:
print("No movies today.")
 
N

Nick the Gr33k

Python is not English. You just have to accept that, no matter how much
you wish it worked like English, it does not.

Think of it like this:

"If today is Tuesday AND I finish work early, then I can go to the
movies."

Unless *both* conditions are true, I cannot go to the movies.


"If today is Tuesday AND I finish work early AND I have more than $16
spare cash to pay for a ticket, then I can go to the movies."

All three conditions must be true, or I cannot go to the movies.


If today is Monday, I don't need to check whether I finish work early, or
whether I have spare cash. It is enough to know that today is not
Tuesday, so I'm not going to the movies.


Python works the same way:

today_is_tuesday = True
finish_work_early = True
spare_cash = 11


if today_is_tuesday and finish_work_early and spare_cash > 16:
print("Going to the movies")
else:
print("No movies today.")
It will print the latter since the overall boolean evaluation of the
expression is False since (spare_cash > 16) = False

That's understandable and works just like an English sentence, but in
this example it was easy since you assigned the vars values to be either
True or False.

This is my difficulty.

a = 'abcd'
b = 'efgh'
c = 'ijkl'

'abcd'

This for me, should evaluate to True but instead it has been evaluated
to the first variable's value, which is a truthy value of course since
its not an empty string, but shouldn't it return True instead?

Returning True is the same thing as returning a variable's truthy value?

'ijkl'

This in my head should have been evaluated to True also since all 3
strings hold truthy values

Why on earth this boolean expression evaluates to the value of the last
variable? This is what can't at all seem to follow.


What i'm trying to say that both these exprs are Boolean Expressions
therefore should return Boolean values not variable's values, even if
they are truthy.
 
J

Jussi Piitulainen

Nick said:
'abcd'

This for me, should evaluate to True but instead it has been
evaluated to the first variable's value, which is a truthy value of
course since its not an empty string, but shouldn't it return True
instead?

In your own programs, write bool(a or b or c) instead. And instead of
writing (k in (a or b or c)), write ((k in a) or (k in b) or (k in c))
-- you can use fewer parentheses if you like, but only if you are
comfortable with it. (Here, I use the outer parentheses to separate
Python from English. I might not use them in code.)

Usually such expressions occur as conditions in conditional statements
or conditional expressions. There, the bool() makes no observable
difference.
Returning True is the same thing as returning a variable's truthy
value?

I think that's a good approximation. Strictly speaking, True is a
particular value in Python, but I think that at the moment you need to
understand that what is important is how the value is interpreted as a
condition in a conditional statement (if condition:, elif: condition),
a conditional expression (x if condition else y), a while loop (while
condition:).
 
N

Nick the Gr33k

NO! 'True' and 'False' are the two values of the boolean type. The
'and' and 'or' logical operators do NOT return a boolean type of True or
False.
Indeed.
True
abcd
print( bool( name or month or year ) )
True
Also they do NOT return "a variable's truthy value", they return the
variable itself.

No, as seen from my above examples, what is returned after the expr eval
are the actual variables' values, which in turn are truthy, *not* the
variable itself.
Now, that returned variable can then be interpreted as
a boolean value for other operations in the same way that (virtually)
all data types can be interpreted as a boolean. Let me emphasize...
they are INTERPRETED as having a boolean VALUE, but they are NOT a
boolean TYPE.

Yes the returned value of 'hijk' is being interpreted as bool('hijk'),
which boils down as truthy.
 
L

Lele Gaifax

Nick the Gr33k said:
No, as seen from my above examples, what is returned after the expr
eval are the actual variables' values, which in turn are truthy, *not*
the variable itself.

In the context we are talking about, "the variable itself" has the very
same meaning as "the actual variable value":
mylist = ['foo']
emptylist = []
result = emptylist or mylist
result.append('bar')
result is mylist True
print(mylist)
['foo', 'bar']

ciao, lele.
 
N

Nick the Gr33k

In the context we are talking about, "the variable itself" has the very
same meaning as "the actual variable value":

Are there cases that a variable and the variable's value cosidered to be
2 different things?
mylist = ['foo']
emptylist = []
result = emptylist or mylist

result = mylist (since its a no-emoty list)

Never seen the last statement before. What does that mean?
result is mylist ????
 
M

Michael Torrie

result = mylist (since its a no-emoty list)


Never seen the last statement before. What does that mean?
result is mylist ????

Yes. Surprisingling good question.

http://docs.python.org/3.3/reference/expressions.html#is
http://docs.python.org/3/reference/datamodel.html

One thing that you may find interesting is that what we often call
variables in Python, and which from your code's point of view look and
act like variables are in fact names. Whereas in C, assignment can be
thought of as copy (a = b in C means that b's value is copied to a), in
Python assignment is associating a name with an object. Thus a = b in
Python means that now the names a and b both are bound (reference to)
the same object. That's why the "is" operator is there, to help one
know if two names point to the same object.

I bring this up on the list from time to time because I find it really
interesting and intellectually appealing the way Python works. Hearkens
back to my class at uni on programming language theory.
 
N

Nick the Gr33k

Yes. Surprisingling good question.

http://docs.python.org/3.3/reference/expressions.html#is
http://docs.python.org/3/reference/datamodel.html

One thing that you may find interesting is that what we often call
variables in Python, and which from your code's point of view look and
act like variables are in fact names. Whereas in C, assignment can be
thought of as copy (a = b in C means that b's value is copied to a), in
Python assignment is associating a name with an object. Thus a = b in
Python means that now the names a and b both are bound (reference to)
the same object. That's why the "is" operator is there, to help one
know if two names point to the same object.

I bring this up on the list from time to time because I find it really
interesting and intellectually appealing the way Python works. Hearkens
back to my class at uni on programming language theory.

(a = b in C means that b's value is copied to a)

in C:

a = memory chunk able to hold some specific type's value
b = memory chunk able to hold some specific type's value

a = b means

So we have 2 memory units hod, the same value.

in Python:

a and b you say are names, which still are memory chunks

In both situations we still have 2 memory units holding values, so hows
that different?
 
M

Michael Torrie

a and b you say are names, which still are memory chunks

Yes no matter how you look at it, a dictionary of names and objects is
memory and "variables" in that sense. But at a higher level, we can
consider the differences with how a language like C defines variables.
In both situations we still have 2 memory units holding values, so hows
that different?

Perhaps one could think of python names as more like pointers or
references in C. But python references are counted and garbage-collected
(more like a C++ reference-counting pointer type).

For example, a = 4 makes the name "a" be a reference to the object
int(4), which will never ever change in its lifetime (indeed it wouldn't
make sense for the integer 4 to change otherwise it wouldn't be a 4).
Thus a = a + 1 creates a new object that represents the integer value of
4 + 1, and throws the old object away.

Note that the identity (object) of a has changed. If a were a variable
in the same sense as C, the identity of a would not change.

A mutable object like a list acts more like a variable in some ways:
b = []
id(b) 3076765292
b.append(3)
id(b)
3076765292

In many cases the distinction is little more than intellectual for all
intents and purposes, though it some cases the idea is very powerful.

But there a couple of cases where the difference between a variable and
a name referencing an object does bite people in Python:
http://effbot.org/zone/default-values.htm
http://stackoverflow.com/questions/986006/python-how-do-i-pass-a-variable-by-reference
 
S

Steven D'Aprano

It's just a rule you'll have to learn. The "and" and "or" operators in
Python simply do not return a boolean value. The expression "a or b" is
evaluated as:
if a is true then return a otherwise return b

I prefer to say that as

"if a is true-ish then return a otherwise return b"

or even

"if a quacks like a true value then return a otherwise return b"


to emphasis that this is a form of duck-typing, and avoid any confusion
with "if a is True".


But otherwise, well said.
 
D

Denis McMahon

In both situations we still have 2 memory units holding values, so hows
that different?

Consider that each named variable is a pointer to a memory location that
holds a value. This is one of the ways in that a typed compiled language
and an untyped scripted language may differ in their treatment of data
items (or variables).

Consider the following C and Python code:

C:

int a, b;
b = 6;
a = b;

In C, this places the numeric value 6 into the memory location identified
by the variable "b", then copies the value from the location pointed to
by "b" into the location pointed to by "a".

b is a pointer to a memory location containing the value 6
a is a pointer to another memory location also containing the value 6

Python:

b = 6
a = b

In Python, this first puts the value 6 in in a memory location and points
"b" at that memory location, then makes "a" point to the same memory
location as "b" points to.

b is a pointer to a memory location containing the value 6
a is a pointer to the same memory location

Do you understand the difference?
 
N

Nick the Gr33k

Consider that each named variable is a pointer to a memory location that
holds a value. This is one of the ways in that a typed compiled language
and an untyped scripted language may differ in their treatment of data
items (or variables).

Consider the following C and Python code:

C:

int a, b;
b = 6;
a = b;

In C, this places the numeric value 6 into the memory location identified
by the variable "b", then copies the value from the location pointed to
by "b" into the location pointed to by "a".

b is a pointer to a memory location containing the value 6
a is a pointer to another memory location also containing the value 6

Python:

b = 6
a = b

In Python, this first puts the value 6 in in a memory location and points
"b" at that memory location, then makes "a" point to the same memory
location as "b" points to.

b is a pointer to a memory location containing the value 6
a is a pointer to the same memory location

Do you understand the difference?
Yes and thank you for explaining in detail to me.
So Python economies(saves) system's memory. Good job Python!

There is no point having 2 variables point to 2 different memory
locations as C does since both of those memory locations are supposed to
contain the same value!

One thing that i want you guys to confirm to me:

a and b in both of the languages mentioned are pointers to memory
locations which hold a value.

A pointer = a variable that has as a value a memory address
a variable = a memory address that has as a value the actual value we
want to store.

But since pointer itself is a memory location that holds as a value the
address of another memory location(by definition) that in it's own turn
holds the actual value we want stored?

So the scheme would look like this in Python:

memory address = number 6

memory address = memory address value that stores number 6 (pointer a)

memory address = memory address value that stores number 6 (pointer b)


So having in mind the above.

The memory address that actually stores number 6 have no name at all and
the only way to access it's value is by printing the variables a or b?
Doesn't that memory address have a name itself?

if someone wants the memory address of the pointer's a or b, how can he
access it? in C it was somethign like &a and &b

And in Python internally shall i imagine two tables that has
associations of:

variable's_name <-> memory_address <-> actual value

And ALL 3 of them are actually bits(1s and 0s)

Is this how the thing works?
 
N

Nick the Gr33k

Because Python lets you use arbitrary values in a Boolean context, the net
result is exactly the same.

What is an arbitrary value? don even knwo what arbitrary means literally
in English.
In a long series separated by "or", the expression is true as soon as one
of the subexpressions is true. So, as a short-circuit, Python simply
returns the first one that has a "true" value. So, for example, these all
return 'abcd':

'abcd' or 'defg' or 'hjkl' ==> 'abcd'
0 or 'abcd' or 'defg' or 'hjkl' ==> 'abcd'
0 or None or 'abcd' or 'defg' or 'hjkl' ==> 'abcd'

Similarly, "and" returns the first "false" value, or if they're all true,
the last value. Why? Because it can't know whether the whole expression
is true unless it looks at every value. So:

0 and 1 and 'what' ==> 0
1 and 0 and 'what' ==> 0
1 and None and 0 ==> None
1 and 1 and 'what' ==> 'what'
Thank you Tim.

I decided that it's better to thing it through as:

The argument being returned in an "and" or "or" expression is the one
that *determined' the evaluation of the expression.

And actually what's being returned is not the argument itself but the
argument's value.
 
N

Nick the Gr33k

No. Don't read that into it.

For example, in Python

a = 6
b = a
c = 6

a and b point to one memory location that contains the value 6
c points to a different memory location that contains the value 6

I believe you are mistaken.

a here is not a pointer but variable,
which is a memory location that stores value 6.

b here is a pointer. It's value is the memory location of variable a
which stores value 6.

c here is just te same as a , a variable.
These are really C terms, not Python terms. Stop thinking that C is
behaving like Python.


I think it behaves the same way, but lets here from someone else too.
No.

Python is an interpreted language. C is a compiled language. They present
very different feature sets to the user. You need to understand this, and
at the moment you're not doing so.

Whats the difference of "interpreting " to "compiling" ?

I have also asked other things too which you didn't quote, please do.
 
R

R. Michael Weylandt

I believe you are mistaken.

a here is not a pointer but variable,
which is a memory location that stores value 6.

b here is a pointer. It's value is the memory location of variable a which
stores value 6.

c here is just te same as a , a variable.

Actually, y'all both might be. This is a bit CPython specific and not
mandated by the language specification.

To Nikos: please don't extrapolate from the examples below. They are a
CPython (the most common implementation of the Python language)
specific detail.

## CODE SNIPPET##
a = 6; b = a; c = 6

id(a)
id(b)
id(c)
## END CODE##

These are all the same, indicating that they all point to the "same 6"
in memory. That's a CPython specific optimization (caching small
integers) which is not guaranteed by the language and changes between
pythons and between compiles.

For example,

## CODE SNIPPET##
a = 552315251254
b = a
c = 552315251254

a is b # True _on my machine_
a is c # False _on my machine_

id(a)
id(b)
id(c)
## END CODE##

Note that to compare if two names point to the same "object, you can
use the "is" operator.

a is b
c is a

etc.
I think it behaves the same way, but lets here from someone else too.

I understand the Greeks invented democracy and all that, but facts
aren't subject to it.
Whats the difference of "interpreting " to "compiling" ?

If only it could be googled.... Alas, no one has ever written anything
about technology on the internet. Ironic that...

Michael
 
M

Mark Lawrence

If only it could be googled.... Alas, no one has ever written anything
about technology on the internet. Ironic that...

Michael

I'm very sorry but I don't understand the words "googled" and
"internet". Could you please explain them?

--
"Steve is going for the pink ball - and for those of you who are
watching in black and white, the pink is next to the green." Snooker
commentator 'Whispering' Ted Lowe.

Mark Lawrence
 

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
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top