1
True and False Python are always confusing. If you accidentally use it, you will make a mistake. This article summarizes three error-prone points, namely logical inversion, if conditional and pandas.DataFrame.loc slice The conditional formula.

1. The logic of True and False is reversed

logic of True and False, 1611077180491d ~ not used, but not .
Because in Python, not is the logical inversion, and ~ is the bitwise inversion. The corresponding values ​​of True and False are 1 and 0, ~True is equivalent to the bitwise negation of 1, and the result is -2, and the result of not True is False.

print(True)
print(~True)
print(not True)

turn out:

True
-2
False

Similarly, the result of ~False is 1, and the result of not False is True

print(False)
print(~False)
print(not False)

turn out:

False
-1
True

Note: In Python ~ bitwise inversion is inverted according to the complement of the number, namely:

1 => Complement code 00000001 => ~Invert by bit => Complement code 11111110 => 2

The result of double negation is like this

print(not not True)
print(~~True)
print(not ~True)
print(~(not True))

The result is:

True
1
False
-1

Double negation of False

print(not not False)
print(~~False)
print(not ~False)
print(~(not False))

The result is:

False
0
False
-2

2. True and False in the if conditional statement

In the Python language, any non-zero and non-empty (null) values ​​after the if are True, and 0 or null is False. This is different from other languages, and it is easy to confuse when using multiple programming languages. So even if the judgment condition is a negative number, it is processed in accordance with True, and the else branch will not be executed. Look at the example:

if (-2):
    print('a')
else:
    print('b')

The result is:

a

If you use ~ to reverse True or False, you will not get the desired result:

if (~True): # ~True == -2
    print('a')
else:
    print('b')

The result is:

a

Only by using not to negate, can the effect of logical negation be achieved:

if not True:
    print('a')
else:
    print('b')

The result is:

b

3. Negation in pandas.DataFrame.loc

The pandas.DataFrame.loc official document says so
Access a group of rows and columns by label(s) or a boolean array.
You can use a Boolean list as input, including using a conditional expression to return a Boolean list, for example:

First create a DataFrame

import pandas as pd

df = pd.DataFrame([[1, 2], [4, 5], [7, 8]],
    index=['cobra', 'viper', 'sidewinder'],
    columns=['max_speed', 'shield'])

df


Use conditional expressions to filter out shield greater than 6

df.loc[df['shield'] > 6]


​Filter out shield domain is less than or equal to 6, you can

df.loc[df['shield'] <= 6]


Can also be used

~ df.loc[~(df['shield'] > 6)]

Another example, filter out index does not contain the two letters of er data

df.loc[~df.index.str.contains('er')]

It should be noted that when df.index.str.contains('er') is used as a condition to filter here, pd.Series is returned.
In pd.Series, the ~ operator is overloaded with . Its processing of Boolean data and numeric data is logical inversion and bitwise inversion, respectively.
df.index.str.contains('er')

The result is:

array([False, True, True])

For the Boolean type , use ~ invert, which is logical inversion

~pd.Series([False, True, False])

The result is

True
False
True
dtype: bool

And if you ~ the pd.Series numeric type 16110771804ced, then is bitwise inverted

~pd.Series([1,2,3])

The result is

-2
-3
-4
dtype: int64

Summarize

  1. In Python, when logically negating True and False, you don't use ~, but use not.
  2. In the if conditional, any non-zero and non-null (null) are True, and 0 or null is False.
  3. In pandas.DataFrame.loc slicing operation, the ~ operator is overloaded, which has a different meaning from native Python.

My python version

>>> import sys
>>> print(sys.version)
3.7.6 (default, Jan  8 2020, 13:42:34) 
[Clang 4.0.1 (tags/RELEASE_401/final)]

_流浪猫猫_
144 声望16 粉丝

个人订阅号Python拾贝,不定期更新