Boolean logic & truth tables · Lógica booleana y tablas de verdad
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
- Un valor Booleano es
True(Verdadero) oFalse(Falso). - Una condición como
age >= 18produce un valor Booleano. - Las computadoras construyen todo a partir de estos dos 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
andes Verdadero solo cuando ambos lados son Verdaderos.ores Verdadero cuando al menos uno de los lados es Verdadero.notinvierte Verdadero a Falso, y Falso a Verdadero.
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.
Tabla de verdad
- Una tabla de verdad enumera cada entrada posible y su salida.
- Dos entradas (A, B) dan cuatro filas: 00, 01, 10, 11.
Truth table — try every input · Tabla de verdad — prueba cada entrada
and is true only when both · ambas are true; or when at least one is. · and es verdadero solo cuando ambos son verdaderos; or cuando al menos uno lo es.
Build a truth table with loops
- A nested loop over
False, Truewalks through all the combinations.
Construir una tabla de verdad con bucles
- Un bucle anidado sobre
False, Truerecorre todas las combinaciones.
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.
Más puertas lógicas: NAND, NOR, XOR
- NAND es
not (a and b); NOR esnot (a or b). - XOR es Verdadero cuando las entradas son diferentes:
a != b.
In the exam: logic gates
- Know the gates NOT, AND, OR, NAND, NOR, XOR and their truth tables.
En el examen: puertas lógicas
- Conoce las puertas NOT, AND, OR, NAND, NOR, XOR y sus tablas de verdad.
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.
Errores comunes
ANDes verdadero solo cuando ambas entradas son verdaderas;ORes verdadero cuando al menos una lo es.NOTinvierte el valor.- Una tabla de verdad lista cada combinación de las entradas.
Now you try
- Each function returns the gate's Boolean output.
- Press Check answer to test your code.
Ahora tú intentas
- Cada función devuelve la salida Booleana de la puerta lógica.
- Presiona Check answer para probar tu código.
Write AND_gate(a, b) that returns True only when both · ambas inputs are True. · Escribe AND_gate(a, b) que devuelva True solo cuando ambas entradas sean True.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Write XOR_gate(a, b) that returns True when the two inputs are different, and False when they are the same. · Escribe XOR_gate(a, b) que devuelva True cuando las dos entradas sean diferentes, y False cuando sean iguales.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Write NAND_gate(a, b) that gives the opposite of AND: True unless both inputs are True. Use not (a and b). · Escribe NAND_gate(a, b) que dé el opuesto de AND: True a menos que ambas entradas sean True. Usa not (a and b).
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.