Reputation: 1139
I am having trouble understanding some of the short hand notation I am seeing in Python. Would someone be able to explain the difference between these two functions please? Thank you.
def test1():
first = "David"
last = "Smith"
if first and last:
print last
def test2():
first = "David"
last = "Smith"
print first and last
Upvotes: 1
Views: 133
Reputation: 1414
The difference between the functions test1()
and test2()
is that test1()
explicitly prints the value of last
so long as the result of the expression first and last
evaluates as true, and test2()
prints the result of the expression first and last
. The string that is printed is the same because the result of the expression first and last
is the value of last
- but only because first
evaluates as true.
In Python, if the left-hand side of an and
expression evaluates as true, the result of the expression is right-hand side of that expression. Because of short-circuiting of boolean operators, if the left-hand-side of an and
expression evaluates as false, the left-hand side of the expression is returned.
or
also short-circuits in Python, returning the value of the leftmost part of the expression that determines the entire expression's truth value.
So, looking at a few more test functions:
def test3():
first = ""
last = "Smith"
if first and last:
print last
def test4():
first = ""
last = "Smith"
print first and last
def test5():
first = "David"
last = "Smith"
if first or last:
print last
def test6():
first = "David"
last = "Smith"
print first or last
def test7():
first = "David"
last = ""
if first or last:
print last
def test8():
first = "David"
last = ""
print first or last
test3()
will not print anything.
test4()
will print ""
.
test5()
will print "Smith"
.
test6()
will print "David"
.
test7()
will print ""
.
test8()
will print "David"
.
Upvotes: 3
Reputation: 168626
What, you ask, is the difference between these two snippets?
if first and last:
print last
and
print first and last
In the first case, either the code will print the value of last, or it won't.
In the second case, the code will print the value of first and last
. If you are used to C, then you might think that the value of a and b
is a boolean value of either True or False. But you'd be wrong.
a and b
evaluates a
; if a
is truthy, the value of the expression is b
. If a
is falsely, the value of the expression is a
:
"David" and "Smith" -> "Smith"
0 and "Smith" -> 0
1 and "Smith" -> "Smith"
"David" and 0 -> 0
"David" and 1 -> 1
Generallly:
last
, if it prints anything at all
first
or last
, according to the truthiness of first
.Specifically, if first
is ever ""
, then the 2nd example will print ""
while the
first won't print anything at all.
Upvotes: 0
Reputation: 309919
The first function always returns None
(printing Smith
) whereas the second one always returns "Smith"
*
Quick digression into and
:
the python and
operator returns the first "falsy" value it encounters. If it doesn't encounter a "falsy" value, then it returns the last value (which is "true-y") This explains why:
"David" and "Smith"
always returns "Smith"
. Since both are non-empty strings, they are both "true-y" values.
"" and "Smith"
would return ""
since it is a falsy value.
*The original function that OP posted actually looked like:
def test2():
first = "David"
last = "Smith"
return first and last
Upvotes: 5