Quick review on if statements, boolean expressions, and college-board pseudo code
Data-type that can either be true or false
boolean = True
print(boolean)
boolean = False
print(boolean)
boolean = 1
## boolean is not longer a boolean, now an integer
print(type(boolean))
True
False
<class 'int'>
Self, explanatory, runs a portion of code if expression/condition is satisfied.
if (EXPRESSION):
print('code to run here')
number = int(input('Enter a number: '))
# Collegeboard talks about the modulo operation even in this section so here it is
# % is the modulo operation
# Easier way to think about it is the remainder when two numbers are divided
# Ex. 14 % 6 = remainder of 14/6 = 2
if (number % 2 == 0):
print('Number is even')
else:
print('Number is odd')
Number is even
Used to compare two variables, returns a boolean
a = 7
b = 5
if a == b:
print('a equals b')
else:
print('a does not equal b')
if a > b:
print('a is greater than b')
elif a < b:
print('a is less than b')
a does not equal b
a is greater than b
Create a program that takes two numbers from the user (use input()) and displays the larger one. If the numbers are equal, state that they are equal. Challenge: Use relational operators to compare two words and display whichever one comes first alphabetically (Can you apply it to a list?)
## Program Here
num1 = int(input("enter a number"))
num2 = int(input("enter another number"))
if num1 > num2:
print("num1 is greater")
elif num1 < num2:
print("num2 is greater")
else:
print("they're equal")
Logic gates combine different boolean (true/false or 1/0) values into a single boolean value. As computers are composed of a bunch of binary/boolean values, logic gates are the “logic” of the computer (the ability to code).
The basic operators in Boolean algebra are AND, OR, and NOT. The secondary operators are eXclusive OR (often called XOR) and eXclusive NOR (XNOR, sometimes called equivalence). They are secondary in the sense that they can be composed from the basic operators.
The AND of two values is true only whenever both values are true. It is written as AB or A⋅B.
| A | B | A AND B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
![]()
Real-life example of AND: If it’s sunny (A) AND it’s a weekend (B), then I will go to the beach :)
- Add an “else” statement to the current if-statement, and display an appropriate response for each block
- Rename the two variables to a realistic scenario (i.e. sunny and weekend example above)
- Change the two variables between True and False to see what output you’ll get!
- (CHALLENGE): Make the code more user-friendly with “input()”
# CB Pseudo Code
A ← true
B ← true
IF (A AND B) {
DISPLAY("It's true!")
}
# Python
A = input("Enter true or false")
B = input("Enter true or false")
if A == "true":
val1 = True
else:
val1 = False
if B == "false":
val2 = False
else:
val2 = True
if val1 and val2: # Bitwise syntax: A & B
print("It's true!")
else:
print("It's false")
It's true!
The OR of two values is true whenever either or both values are true. It is written as A+B. The values of or for all possible inputs is shown in the following truth table
| A | B | A OR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
![]()
Real-life example of OR: If it’s my birthday (A) OR it’s Christmas (B), then I will get a present :D
- Add an else statement to create output appropriately
- Try different combinations of A and B
# CB Pseudo Code
A ← true
B ← false
IF (A OR B) {
DISPLAY("It's true!")
}
A = input("Enter true or false")
B = input("Enter true or false")
if A == "true":
val1 = True
else:
val1 = False
if B == "false":
val2 = False
else:
val2 = True
if val1 or val2: # Bitwise syntax: A & B
print("It's true!")
else:
print("It's false")
It's true!
True
The NOT of a value is its opposite; that is, the not of a true value is false whereas the not of a false value is true. It is written as [x with a dash above] or ¬x.
| A | NOT A |
|---|---|
| 0 | 1 |
| 1 | 0 |
![]()
Real-life example of NOT: If I do NOT eat dessert (A), then I will be sad ;-;
- Follow the “AND in Python” hack to add an else statement to output when gate returns false, and change the program to apply in real life
- Try different combinations of A
- (CHALLENGE): Combine the NOT logic gate with the AND logic gate and the OR logic gate from above
# CB Pseudo Code
A ← false
IF (NOT A) {
DISPLAY("It's true!")
}
# Python
A = False
if not A: # No equivalent bitwise syntax in python
print("It's true!")
It's true!
The XOR of two values is true whenever the values are different. It uses the ⊕ operator, and can be built from the basic operators: x⊕y = x(not y) + (not x)y
| A | B | A XOR B |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
![]()
Real-life example of XOR: If I play video games (A) XOR I watch a movie (B), I will be entertained :O
Another example: There is a light connected to two switches. If the first switch is on (A) XOR the second switch is on (B), then the light will turn on. Note here that flipping either switch (changing either input) changes the output.
The XOR gate is often use in binary calculators as well, because if two ones results in a zero, and an AND gate can be used to calculate the next bit.
- Follow the “AND in Python” hack to add else output
- Try different combinations of A and B
- (CHALLENGE): Assuming True is 1 and False is 0, write an expression that takes two inputs and outputs the result of a XOR gate
# CB Pseudo Code
A ← false
B ← true
IF (A XOR B) {
DISPLAY("It's true!")
}
# Python
A = input("Enter true or false")
B = input("Enter true or false")
if A == "true":
val1 = 1
else:
val1 = 0
if B == "false":
val2 = 0
else:
val2 = 1
if val1 + val2 % 2 == 1: # Only bitwise syntax, no "xor" keyword
print("It's true!")
else:
print("false")
false
NAND is the NOT of the result of AND. In other words, the result of NAND is true if at least one value is false.
| A | B | A NAND B |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
![]()
Real-life example of NAND: If I forget my computer (A) NAND I forget my phone (B), I can access the internet :P
- Follow the “AND in Python” hack to make the code applicable to a real-life example (e.g. forgetting your phone NAND forgetting your computer).
- Try different combinations of A and B
- Follow the “AND in Python” hack to add else output
# CB Pseudo Code
A ← false
B ← true
IF (NOT (A AND B)) {
DISPLAY("It's true!")
}
# Python
A = False
B = True
if not (A and B):
print("It's true!")
It's true!
NOR is the NOT of the result of OR. In other words, the result of NOR is true if and only if both values are false.
| A | B | A NOR B |
|---|---|---|
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 0 |
![]()
Real-life example of NOR: If there’s a fire (A) NOR there’s an earthquake (B) then I’ll be ok :S
- Follow the “AND in Python” hack to make the code applicable to a real-life example (e.g. there’s a fire NOR there’s an earthquake).
- Try different combinations of A and B
- Follow the “AND in Python” hack to add else output
# CB Pseudo Code
A ← false
B ← true
IF (NOT (A OR B)) {
DISPLAY("It's true!")
}
# Python
A = False
B = True
if not (A or B):
print("It's true!")
It's true!
An OR (AND) expression that is negated is equal to the AND (OR) of the negation of each term.
NOT (A AND B) = (NOT A) OR (NOT B)
NOT (A OR B) = (NOT A) AND (NOT B)
There is one more logic gate, known as an XNOR, or exclusive-NOR gate. It is a combination of an XOR and a NOT gate. Create a table like the ones above to demonstrate input and output values of this gate.
Create python functions for three of the above logic gates. Implement two of them in an interactive program (with input) with a purpose.
Bob is grading homework from a peer teaching project. He needs to mark a student’s homework as incomplete if they did NOT complete all the problems, OR did NOT submit it on time. Unfortunately, his teammate Cob did not give him any other information, and is now on vacation. Bob needs to do this using only TWO logic gates (don’t ask why). Help Bob write a program to grade his class’s homework!
isComplete = {'Student 1': 1, 'Student 2': 1, 'Student 3': 0, 'Student 4': 1, 'Student 5': 0, 'Student 6': 1}
isOnTime = {'Student 1': 1, 'Student 2': 0, 'Student 3': 0, 'Student 4': 1, 'Student 5': 1, 'Student 6': 1}
## Program Here
#1.)
##############################
# A # B # A XNOR B #
##############################
# 0 # 0 # 1 #
##############################
# 0 # 1 # 0 #
##############################
# 1 # 0 # 0 #
##############################
# 1 # 1 # 1 #
##############################
#2.)
def xor(one, two):
return bool(one ^ two)
def not_gate(one):
return not one
def xnor(one, two):
return not(xor(one, two))
req = input("Which function would you like to do: XNOR(1) / NOT(2) / XOR(3)")
if (req == "1"):
bool1 = input("Enter bool 1: ")
bool2 = input("Enter another bool 2: ")
print(xnor(bool(bool1), bool(bool2)))
elif (req == "2"):
bool1 = input("Enter bool 1: ")
print(not_gate(bool(bool1)))
elif (req == "3"):
bool1 = input("Enter bool 1: ")
bool2 = input("Enter another bool 2: ")
print(xor(bool(bool1), bool(bool2)))
## Example output: {'Student 1': 'Incomplete', 'Student 2': 'Incomplete', 'Student 3': 'Incomplete', 'Student 4': 'Incomplete', 'Student 5': 'Incomplete', 'Student 6', 'Complete'}
## Extra: Format the output nicely
#3.)
if len(isComplete) == len(isOnTime):
for i in range(len(isComplete)):
incomplete = not(isComplete[list(isComplete.keys())[i]] and isOnTime[list(isComplete.keys())[i]])
if incomplete:
print("Student " + str(i+1) + " was a very bad boy/girl")
else:
print("GOOOOOOOOOOOOOD Student " + str(i+1) + " GOOOOOOOOOOOOOOOOOOOOOOD")
True
GOOOOOOOOOOOOOD Student 1 GOOOOOOOOOOOOOOOOOOOOOOD
Student 2 was a very bad boy/girl
Student 3 was a very bad boy/girl
GOOOOOOOOOOOOOD Student 4 GOOOOOOOOOOOOOOOOOOOOOOD
Student 5 was a very bad boy/girl
GOOOOOOOOOOOOOD Student 6 GOOOOOOOOOOOOOOOOOOOOOOD
def nandgate(a, b):
if not (a and b):
return True
elif not (a and b):
return False
def andgate(a, b):
if a and b:
return True
elif not (a and b):
return False
def orgate(a, b):
if a or b:
return True
else:
return False
def xorgate(a, b):
return andgate(nandgate(a, b), orgate(a, b))
print(xorgate(True, True))
print(xorgate(True, False))
print(xorgate(False, True))
print(xorgate(False, False))
False
True
True
False