How does this:Use backet to achieve if statement in Python? -
i saw usage of python, clean, don't understand usage, , can't search useful explanation either.
this normal method:
if < 0: b = 2 * else: b = 3 *
and realizes same purpose:
b = * (3,2) [a<0]
can tell me where official documentation above ?
b = * (3,2) [a<0]
a<0
status returning true or false
i.e. 1/0
(3,2)
tuple 3
, 2
elements
if status true
statement indexing element @ index 1
else indexing element 0
like
in [33]: (3,2)[0] out[33]: 3 in [34]: (3,2)[1] out[34]: 2
the status results in 0
or 1
because in python true means 1
, false means 0
in [35]: true == 1 out[35]: true in [36]: false == 0 out[36]: true
python if-statement
No comments:
Post a Comment