Reputation: 13
This function compares the Instagram followers count of two accounts, and taking the input from the user (a guess as to which one has more followers). I created a longer version of if/elif statements but instructor wrote this shorter version:
def check_answer(guess, a_followers, b_followers):
if a_followers > b_followers:
return guess == 'a'
else:
return guess == 'b'
I am unable to understand why return guess == 'a'
returns a boolean value. I know that a double equal sign checks equality between two entities.
Upvotes: 1
Views: 1206
Reputation: 222
==
operator is used to check equality of two objects, and if two objects are equal then this operator returns boolean object True
, if not - False
:
>>> "string" == "string"
True
>>> "String 2" == "string 2"
False
In the case with return
, python interpreter at first counts the result of the expression guess == 'a'
and the return the given result. So if guess
is equal to 'a'
, then function returns True
, otherwise False
.
Upvotes: 1
Reputation: 1623
Python, like most languages (except lisp, cough cough), has something called infix operators (or simply operators).
These operators take two arguments, and returns a value.
The syntax is
argument1 <OPERATOR> argument2
For example, +
is an operator, that can take two numbers, and return a number. Same for *
>>> 2 + 1
3
>>> 5 * 2
10
At execution, the argument1 <OPERATOR> argument2
is replaced by its value. For example, take the function f
>>> def f(x):
return x + 3
>>> f(4)
7 # x + 3 in the function has been replaced by the value of 4 + 3, which is 7
Now the question is What is ==
?
==
is just another operator.But it is a so-called boolean operator, it doesn’t return numbers, but booleans
For example:
>>> 3 == 2
False
>>> "hello" == "hello"
True
What does that mean for your function? Well the syntax guess == 'a'
will be replaced by the value of ==
for the arguments guess
and a
. Which is
True
if the value of guess
is 'a'
False
otherwiseUpvotes: 0
Reputation: 21220
Your question does not state what you think the function should return, but to understand why this function returns a boolean, we have to look at the return statements:
return guess == 'a'
return
is a keyword in Python that has the form return SOME_VALUE
, and will hand back to the calling function SOME_VALUE
. Now, the value can be either a value (for instance, return True
, in which case the value is True
), or one might find an 'expression' - most easily defined as 'something that can be converted into a value'.
Because the interpreter finds an expression (in this case, guess == 'a'
) after the return
keyword, it decides to convert that expression into a value. We might see this same sort of expression in an if
function:
if guess == 'a':
print("Correct")
In both the return
and if
cases, the expression is 'evaluated' - converted to a value. In this case, guess
is a variables - also an expression - so this is converted into values by 'dereferencing' it. Thus, if guess
is equal to b
, guess == 'a'
is converted to 'b' == 'a'
. This is still an expression, though, though one without any sub-expressions in it. As a result, a final evaluation is done.
The ==
sign is what is known as 'semantic sugar' for a function of the form:
is_equal(left, right) -> bool
This function will return True
if the two parameters are equal, and False
otherwise. This is all under the hood, but as we can see b
and a
are not equal, the function returns the value False
. Thus, 'b' == 'a'
is evaluated to False
, and the statement return guess == 'a'
is evaluated in several steps to be equivalent to return False
.
(Note that if guess
was also 'a'
it would evaluate to return True
.)
Almost everything you see will be this sort of series-of-evaluations as statements are evaluated into values. And that is why your function returns a boolean value - the only possible return values are True
or False
, both of which are booleans.
Upvotes: 0
Reputation: 379
==
is a comparison operator. I usually think of them as a function that takes the two values on either side of it, then returns either True
or False
. For example, guess == 'a'
compares the value of guess
to 'a'
and the returns True
if they are the same and False
if they are different. Other comparators are >
, <
, !=
, and so on that check for different things.
Upvotes: 0
Reputation: 11650
'guess' is a value passed in the function along with the a-folowers and b-followers parameters. When a-followers are greater than b-followers, if condition holds true and it compares whether the guess passed in as a parameters matches with the literal 'a'. When its true the expression returns True, otherwise False.
return statement evaluates to True or False, depending upon whether the two matches or not.
Upvotes: 1