
Corso di Python #6 i valori booleani
Sesta parte del corso python
Vediamo i
VALORI BOOLEANI
Premessa:
Approfondite se volete il concetto di algebra di Boole:
https://it.wikipedia.org/wiki/Algebra_di_Boole
Approfondite se volete il concetto di algebra di Boole:
https://it.wikipedia.org/wiki/Algebra_di_Boole
Vediamo anzitutto il suo datatype:
>>> num = True >>> print(type(num)) >>> <class 'bool'>
Facciamo qualche esempio sull’algebra booleana:
>>> num = 9 > 8 >>> print(um) >>> True
E’ vero che 9 sia maggiore di 8, quindi รจ vero.
>>> num = 5 > 8 >>> print(um) >>> False
NON รจ vero che 5 sia maggiore di 8, quindi รจ falso
Adesso mescoliamo le condizioni di veritร :
Approfondimento: https://it.wikipedia.org/wiki/Tabella_della_verit%C3%A0
Tratto da wikipedia:
| โง | โจ | โง | โจ | โ | โ | ||
| F | F | F | F | F | V | V | V |
| F | V | F | V | V | F | V | F |
| V | F | F | V | V | F | F | V |
| V | V | V | V | F | V | V | V |
Legenda:
- V = vero, F = falso
- โง = AND (congiunzione logica)
- โจ = OR (disgiunzione logica)
- โง = XOR (OR esclusivo)
- โจ = XNOR (NOR esclusivo)
- โ = “se-allora” (implicazione logica)
- โ = “(allora)-se” (controimplicazione logica)
- <โ>: se e soltanto se รจ logicamente equivalente a <โจ>: XNOR (NOR esclusivo).
PโงQ (P AND Q)
>>> p = False >>> q = False >>> res = p and q >>> print(res) >>> False
Come vediamo qui:
| โง | โจ | โง | โจ | โ | โ | ||
| F | F | F | F | F | V | V | V |
| F | V | F | V | V | F | V | F |
| V | F | F | V | V | F | F | V |
| V | V | V | V | F | V | V | V |
P v Q (P OR Q)
>>> p = False >>> q = True >>> res = p and q >>> print(res) >>> True
Come vediamo qui:
| โง | โจ | โง | โจ | โ | โ | ||
| F | F | F | F | F | V | V | V |
| F | V | F | V | V | F | V | F |
| V | F | F | V | V | F | F | V |
| V | V | V | V | F | V | V | V |
P โง Q (P XOR Q)
>>> p = False >>> q = True >>> res = p ^ q >>> print(res) >>> True
Come vediamo qui:
| โง | โจ | โง | โจ | โ | โ | ||
| F | F | F | F | F | V | V | V |
| F | V | F | V | V | F | V | F |
| V | F | F | V | V | F | F | V |
| V | V | V | V | F | V | V | V |
E cosi via:
P โจ Q (P XNOR Q)
Cogliamo tuttavia la palla al balzo per introdurre il concetto di if:
p = False
q = False
res = (p ^ q) #xor
print(res)
if res == False: # ovvero = se non รจ xor, quindi รจ xnor
print("Xnor")
else:
print('Non xnor')
>>> Xnor
