Une table de vérité.
a, b et c désignent trois booléens. Dresser la table de vérité de (a and b) or c :
- à la main,
- puis à l'aide d'un programme python.
a, b et c désignent trois booléens. Dresser la table de vérité de (a and b) or c :
a | b | c | a and b | (a and b) or c |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 0 | 1 | 0 | 1 |
0 | 1 | 0 | 0 | 0 |
0 | 1 | 1 | 0 | 1 |
1 | 0 | 0 | 0 | 0 |
1 | 0 | 1 | 0 | 1 |
1 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 1 |
Code python pour générer la table :
for a in (0,1) :
for b in (0,1) :
for c in (0, 1) :
print( " ({} and {}) or {} = {}.".format(a,b,c,(a and b) or c ) )
(0 and 0) or 0 = 0. (0 and 0) or 1 = 1. (0 and 1) or 0 = 0. (0 and 1) or 1 = 1. (1 and 0) or 0 = 0. (1 and 0) or 1 = 1. (1 and 1) or 0 = 1. (1 and 1) or 1 = 1.
a, b et c désignent trois booléens. Dresser la table de vérité de a and (b or c) :
a | b | c | b or c | a and (b or c) |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 0 | 1 | 1 | 0 |
0 | 1 | 0 | 1 | 0 |
0 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 0 | 0 |
1 | 0 | 1 | 1 | 1 |
1 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 1 |
Code python pour générer la table :
for a in (0,1) :
for b in (0,1) :
for c in (0, 1) :
print( " {} and ({} or {}) = {}.".format(a,b,c,a and (b or c) ) )
0 and (0 or 0) = 0. 0 and (0 or 1) = 0. 0 and (1 or 0) = 0. 0 and (1 or 1) = 0. 1 and (0 or 0) = 0. 1 and (0 or 1) = 1. 1 and (1 or 0) = 1. 1 and (1 or 1) = 1.
Quels seront les affichages dans les cas suivants ?
def v() :
print("A")
return True
def f() :
print("B")
return False
print("Affichage 1 : ")
f() or v()
print("Affichage 2 : ")
v() or f()
print("Affichage 3 : ")
f() and v()
print("Affichage 4 : ")
v() and f()
Affichage 1 : B A Affichage 2 : A Affichage 3 : B Affichage 4 : A B
Python n'évalue que le premier booléen s'il peut en déduire la valeur de l'expression globale. Dans certains cas, la seconde fonction ne sera donc pas exécutée.