Python邏輯運算子: and or xor(^) not:
對布林值做and or xor(^) not有何效果?
numpy.bool_ (尾巴有_):

尾巴無_的np.bool 已廢棄:
DeprecationWarning: `np.bool` is a deprecated alias for the builtin `bool`. To silence this warning, use `bool` by itself. Doing this will not modify any behavior and is safe. If you specifically wanted the numpy scalar type, use `np.bool_` here.
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
np.bool(True)

對於np.bool_ 做加法等效於or:

1+1=2 ; bool(2)=True
1+0=1 ; bool(1)=True
0+1=1 ; bool(1)=True
0+0=0 ; bool(0)=False
三種情況皆為True,
只有一種情況False
跟or一樣(只有兩邊都False
才會return False)
bool(任何非0的數字)皆為True

bool() 空list: [],空tuple: (),
空dict: {},空set: set(), 空字串: “”
皆為False:

對於python內建的bool做加法
等效於對1, 0做加法:

對於np.bool_ 做乘法等效於and:

1*1=1 (布林中1代表True)
1*0=0 (布林中0代表False)
0*1=0 (布林中0代表False)
0*0=0 (布林中0代表False)
三種情況皆為False,
只有一種情況True
跟and一樣(只有兩邊都True
才會return True)
對於np.bool_做除法:

1/1=1.0 (沒有換算成bool的True)
0/1=0.0 (沒有換算成bool的False)
1/0=inf 無限大
0/0=nan (Not a Number)
python內建的True/False 會出現
ZeroDivisionError: division by zero

npTrue/npTrue =1.0 (float64):

numpy.bool_不支援減法:

對於numpy.bool_
not 與 ~同效果:

但對於python內建的bool
not是邏輯運算子
~是對1,0做逐位元運算

推薦hahow線上學習python: https://igrape.net/30afN

![Python: pandas.DataFrame 如何對某些欄做格式化字串? apply(); applymap() ; map() 的差別? df[‘Salary’] = df[‘Salary’].map( ‘${:,.2f}’ .format) Python: pandas.DataFrame 如何對某些欄做格式化字串? apply(); applymap() ; map() 的差別? df[‘Salary’] = df[‘Salary’].map( ‘${:,.2f}’ .format)](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2023/05/20230527091636_49.png?quality=90&zoom=2&ssl=1&resize=350%2C233)


![Python: matplotlib如何控制legend的位置? ax.legend(handles=[patch], loc=’upper left’, bbox_to_anchor=(6/10, 3/5) Python: matplotlib如何控制legend的位置? ax.legend(handles=[patch], loc=’upper left’, bbox_to_anchor=(6/10, 3/5)](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2023/05/20230502163945_79.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

![Python: 如何對 pandas.DataFrame 兩欄位運算後,增加到最後一欄? df[‘sum_AB’] = df.apply(sum_ab, axis=1) ; lambda函式 Python: 如何對 pandas.DataFrame 兩欄位運算後,增加到最後一欄? df[‘sum_AB’] = df.apply(sum_ab, axis=1) ; lambda函式](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2023/03/20230314200417_4.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

![Python: list.index() 只能找到第一個元素的index,若元素有重複,如何找出所有index? indexes = [index for index, value in enumerate(my_list) if value == target] Python: list.index() 只能找到第一個元素的index,若元素有重複,如何找出所有index? indexes = [index for index, value in enumerate(my_list) if value == target]](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2024/10/20241010101216_0_12b6c0.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

近期留言