#Python(061)邏輯運算子
'''
and:和
or :或
not:不對
'''
x = (10 > 8)and(20 >= 10)
print('(10 > 8)和(20 >= 10)對嗎?')
print(x)
print('\n')
x = (10 < 8)and(20 >= 10)
print('(10 < 8)和(20 >= 10)對嗎?')
print(x)
print('\n')
x = (10 > 8)or(20 >= 10)
print('(10 > 8)或(20 >= 10)對嗎?')
print(x)
print('\n')
x = (10 < 8)or(20 >= 10)
print('(10 > 8)或(20 >= 10)對嗎?')
print(x)
print('\n')
x = (10 < 8)or(20 <= 10)
print('(10 < 8)或(20 <= 10)對嗎?')
print(x)
print('\n')
x = not(10 > 8)
print('(10 > 8)不對對嗎?')
print(x)
print('\n')
x = not(10 < 8)
print('(10 < 8)不對對嗎?')
print(x)
print('\n')
'''
Python的邏輯運算中0被視為False,
而其他值則被視為True。
下列以False開始的and運算將返回前項值。
'''
print('下列以False開始的and運算將返回前項值。')
x = False and True
print("False and True:")
print(x)
print('\n')
x = False and 5
print("False and 5:")
print(x)
print('\n')
x = 0 and 1
print("0 and 1:")
print(x)
print('\n')
#下列以True開始的and運算將返回後項值
print('下列以True開始的and運算將返回後項值。')
x = True and False
print("True and False:")
print(x)
print('\n')
x = True and 5
print("True and 5:")
print(x)
print('\n')
x = -5 and 5
print("-5 and 5:")
print(x)
print('\n')
#下列以False開始的or運算將返回後項值
print('下列以False開始的or運算將返回後項值。')
x = False or True
print("False or True:")
print(x)
print('\n')
x = False or 5
print("False or 5:")
print(x)
print('\n')
x = 0 or 1
print("0 or 1:")
print(x)
print('\n')
#下列以True開始的or運算將返回前項值
print('下列以True開始的or運算將返回前項值。')
x = True or False
print("True or False:")
print(x)
print('\n')
x = True or 5
print("True or 5:")
print(x)
print('\n')
x = -5 or 5
print("-5 or 5:")
print(x)
print('\n')
#not運算傳回相反的布林值
print('以下為not運算傳回相反的布林值。')
x = not 5
print('not 5')
print(x)
print('\n')
x = not -5
print('not -5')
print(x)
print('\n')
x = not 0
print('not 0')
print(x)
print('\n')
結果為:
留言列表