What meaning is "if k in [0, len(n_trials) - 1] else None"?
Terry Reedy
tjreedy at udel.edu
Sat Dec 3 19:10:50 EST 2016
On 12/3/2016 6:27 PM, Chris Angelico wrote:
> On Sun, Dec 4, 2016 at 10:11 AM, Robert <rxjwg98 at gmail.com> wrote:
>> I just notice that there is a slash character (\) before the if line.
>> What is it for?
>
> Yes, that's important. The entire line of code is:
>
> plt.xlabel("$p$, probability of heads") \
> if k in [0, len(n_trials) - 1] else None
>
> The backslash means "this continues on the next line".
> The ternary conditional looks like this:
> 5 if 1 < 2 else 7
>
> Since 1 < 2, this has the value of 5. If not, it would have the value 7.
>
> But the expression result isn't even used. So this is better written:
matplotlib.pyplot.xlabel sets x-axis scaling, with no documented return
value.
http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.xlabel
If, as seems reasonable to assume, it returns None, the value of the
expression is 'None if x else None', which is to say, None.
> if k in [0, len(n_trials) - 1]:
> plt.xlabel("$p$, probability of heads")
--
Terry Jan Reedy
More information about the Python-list
mailing list