Reputation: 101
I've been given the following, and I'm asked to give the type of value and the expression it returns:
>>> b = 10
>>> c = b > 9
>>> c
I know that in the first part, we're defining b
to be 10, but in the second sentence, I'm interpreting this as: Define c
to be equal to b>9
. Now b>9
as a value, doesn't make sense, so c
can't be equal to it, so I put that the answer was error
and the type was Nonetype
.
The correct answer is apparently True
, but why? Why do we take the c=b
part first, and then ask whether it's >9? Is there some sort of standard order in which you're supposed to apply these things?
PS: What do the three >>>
symbols mean in programming? I'm doing an introductory CS course, so please forgive any misnomers.
Upvotes: 0
Views: 56
Reputation: 191864
Python's order precedence is well documented. b > 9
returns a boolean value that must be evaluated before it can be assigned with c =
.
And >>>
is part of the interpreter REPL. It doesn't have a specific meaning to all programming languages.
You could run your code in any Python interpreter to see what the output values are. I'm not sure what you mean by getting a Nonetype error as nothing is evaluated to None in those lines
Upvotes: 1
Reputation: 1237
I think you're getting confused between:
=
), which assigns the result of the expression on the right side of the operator to the variable on the left side of the operator and;==
), which tests the expressions on the right and left of the operator for equality and returns a boolean (true/false) value.The first expression assigns the value 10 to the variable b
. The second expression assigns the expression b > 9
(i.e. 10 > 9), which evaluates to true, to c
. Therefore, I hope you can see how c
ends up being true.
The other issue you might need clarification on is that the =
operator is right associative, which means that the expression to the right of the operator will be evaluated first. i.e. in the second line, b > 9
is evaluated first before assigning the result (true) to c
.
In answer to the second part of your question. Your code wouldn't actually compile as it stands in a regular C# compiler. I'm not sure what the >>>
are. Are you using an online editor or something?
Valid C# code would be:
int b = 10;
bool c = b > 9;
Console.WriteLine(c); //Outputs true
Upvotes: 0