The three-mesh operator is the ternary operator
The ternary expressions of some languages (such as Java) are as follows:
A result when a condition is true: the result of a false decision
Result=x if X
Python's ternary expression has the following several ways of writing:
if __name__ = = ' __main__ ':
a = '
b = ' True '
c = ' False '
#方法一: Results of true if the result if the condition else is false
D = B if a El Se c
Print (' Method one output: ' + d ')
#方法二: The result of the condition and true when the result or is false
d = A and B or C
print (' method Two output result: ' + d ')
#以上 Both methods are equivalent to if ... else ...
If a:
d = b
else:
d = C
print (the output of the ' if statement: ' + d ')
Output results:
Description
Judgment condition: A is an empty string, so the judgment condition is false
The result of the judgment when the condition is true: D = b
The result when the condition is false: D = C
x = [x for x-in range (1,10)]
print (x)
y =[] result
= True if it isn't in x else False # This is the best way
print (Result) Result
= True if not of x else False # This way just like as ' (not in X '
print (Result)
Print (x is y)
print (x isn't y) # this is ' Best way
print (not X was Y) # this way just like as ' (not x) is Y " , so upper are the best way result
= 2 if 1 < 2 else 5 if 4 > 5 Else 6 # just as 1 > 2? 2:4 > 5? 5:6< C11/>print (Result)
Not specific use and meaning in Python
Name= ' While not
name:
name=raw_input (U ' Please enter name: ')
print name
What does not in Python specifically represent:
In Python, not is a logical word, used for Boolean true and False,not true to false,not false to True, and the following are several common uses of not:
(1) Not and the logical judgment sentence if used to execute the statement after the colon, when the expression after not is false. Like what:
A = False
if not a: (here because A is False, not A is true)
print "Hello"
Here we can output the results hello
(2) to determine whether an element is in a list or a dictionary, if a isn't in B,a is an element, B is a list or a dictionary, meaning that if a is not in list B, then execute the statement following the colon, such as:
A = 5
b = [1, 2, 3]
if a not in B:
print "Hello"
We can also output the results here Hello
Not x means equivalent to if X is false, then True, else false
There are three main ways in which a variable is always judged to be none:
The first is "if X is None";
The second type is ' if not x: ';
The third is ' if not ' (this is a clearer understanding of ' if not ' (x is None) ').
If you think it makes no difference, then you should be careful, there is a hole in it. Let's take a look at the code:
>>> x = 1
>>> not x
false
>>> x = [1]
>>> not x
False
>& gt;> x = 0
>>> not x
True
>>> x = [0] # you don ' t want to fall
in this one. >>> not x
False
For more information, refer to this article: http://www.jb51.net/article/93165.htm