Boolean logic & truth tables · Lógica booleana e tabelas-verdade
Boolean values
- A Boolean value is either
TrueorFalse. - A condition such as
age >= 18produces a Boolean. - Computers build everything from these two values.
Valores Booleanos
- Um valor Booleano é ou
TrueouFalse. - Uma condição como
age >= 18produz um Booleano. - Computadores constroem tudo a partir desses dois valores.
AND, OR, NOT
andis True only when both sides are True.oris True when at least one side is True.notflips True to False, and False to True.
AND, OR, NOT
andé True apenas quando ambos os lados são True.oré True quando pelo menos um lado é True.notinverte True para False, e False para True.
print(True and False) # False
print(True or False) # True
print(not True) # False
A truth table
- A truth table lists every possible input and its output.
- Two inputs (A, B) give four rows: 00, 01, 10, 11.
Uma tabela verdade
- Uma tabela verdade lista todas as entradas possíveis e sua saída.
- Duas entradas (A, B) dão quatro linhas: 00, 01, 10, 11.
Truth table — try every input · Tabela-verdade — tente toda entrada
and is true only when both · ambos are true; or when at least one is. · and é verdadeiro apenas quando ambos são verdadeiros; or quando pelo menos um é.
Build a truth table with loops
- A nested loop over
False, Truewalks through all the combinations.
Construir uma tabela verdade com laços
- Um laço aninhado sobre
False, Truepercorre todas as combinações.
for a in [False, True]:
for b in [False, True]:
print(a, b, a and b)
More gates: NAND, NOR, XOR
- NAND is
not (a and b); NOR isnot (a or b). - XOR is True when the inputs are different:
a != b.
Mais portas: NAND, NOR, XOR
- NAND é
not (a and b); NOR énot (a or b). - XOR é True quando as entradas são diferentes:
a != b.
In the exam: logic gates
- Know the gates NOT, AND, OR, NAND, NOR, XOR and their truth tables.
Na prova: portas lógicas
- Conheça as portas NOT, AND, OR, NAND, NOR, XOR e suas tabelas verdade.
X ← A AND B
Y ← NOT (A OR B)
Common mistakes
ANDis true only when both inputs are true;ORis true when at least one is.NOTflips the value.- A truth table lists every combination of the inputs.
Erros comuns
ANDé true apenas quando ambas as entradas são true;ORé true quando pelo menos uma é.NOTinverte o valor.- Uma tabela verdade lista cada combinação das entradas.
Now you try
- Each function returns the gate's Boolean output.
- Press Check answer to test your code.
Agora você tenta
- Cada função retorna a saída Booleana da porta.
- Clique em Check answer para testar seu código.
Write AND_gate(a, b) that returns True only when both · ambos inputs are True. · Escreva AND_gate(a, b) que retorne True apenas quando ambos os entradas forem True.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Write XOR_gate(a, b) that returns True when the two inputs are different, and False when they are the same. · Escreva XOR_gate(a, b) que retorne True quando as duas entradas forem diferentes, e False quando forem iguais.
Click Run to see the output here. · Clique em Executar para ver a saída aqui.
Write NAND_gate(a, b) that gives the opposite of AND: True unless both inputs are True. Use not (a and b). · Escreva NAND_gate(a, b) que dê o oposto de AND: True exceto quando ambas as entradas forem True. Use not (a and b).
Click Run to see the output here. · Clique em Executar para ver a saída aqui.