icearith / blog Goto Github PK
View Code? Open in Web Editor NEWwhy join the navy when you can be a pirate
why join the navy when you can be a pirate
In Java and C, the expression <condition> ? <expression True> : expression False
is widely used as an inline format. It could make the logic more clear than multi-line format for guys who understand this convention. Actually, there is a logically equivalent expression in Python, which is <expression True> if <condition> else <expression False>
. However, I found a number of articles that have partial misconception, in which, they hold the argument that the expression <expression True> if <condition> else <expression False>
is logically equivalent to the expression <condition> and <expression True> or <expression False>
. The aim of this short post is to conduct a comparison between the two expressions and explain why the second expression is not logically equivalent to the first one.
This is the link to the Documentation of Boolean operations in Python3.
For convenience, I put the quotation here.
The expression x and y first evaluates x; if x is false, its value is returned; otherwise, y is evaluated and the resulting value is returned.
The expression x or y first evaluates x; if x is true, its value is returned; otherwise, y is evaluated and the resulting value is returned.
In this case, it seems that the expression <condition> and <expression True> or <expression False>
can be used as a ternary operator. However, there is still an exception. Let's look at the code block.
>>> (2 > 1) and 0 or 1
1
>>> (2 > 1) and 2 or 1
2
>>> (2 > 1) and 3 or 2
3
Obviously, the expression (2 > 1)
returns True
, the first line should output 0
if this expression is logically equivalent to the expression <expression True> if <condition> else <expression False>
. However, the output of the second line and the third line seem reasonable.
In the first experiment (2 > 1) and 0 or 1
, after calculating the value of (2 > 1)
, the Python interpreter evaluates the first operand of the and
operator, which is True
and then returns the resulting value, which is False
. Since 0 and True
returns False
. After that, the interpreter evaluates the expression False or 1
, which returns 1
.
The logical expression <condition> and <expression True> or <expression False>
could be used as a tricky way as a ternary operator. However, the edge case must be taken into consideration. The recommend way of handling this scenario is to use the expression <expression True> if <condition> else <expression False>
, which is also officially recommended. However, the returning rules could also be used for other cases, for example or
operator can be use to assign a default value, an example is as follows:
# when user.get_user_name() returns None, user_name will be assigned a default value.
user_name = user.get_user_name() or "Anonymous User"
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.