3.5 Booleans Expressions

Vocab

Booleans

Booleans have 2 values: True and False.

Note: All of the code cells on this blog demonstrate boolean expressions and selections. Check a JavaScript version to see boolean expressions and iteration.

Relational Operators

==, !=, >, <, >=, <=

Relational/comparison operators can compare numbers, but they can also be used to compare strings. The later a letter is, the greater its value.

For example, as you can see in the code below, print(b > a) outputs as true, because in alphabetical order, b is after a.

a = "a"
b = "b"

print(b > a)
True

Logical operators

and, or, not

In Python, and is &&, or is ||, and not is !

x = 5 
y = 5 
z = 3

if x == 5 and y == 5:
    print("x and y are equal to 5")

if x == 5 or z == 5:
    print("Either x or z is equal to 5")
    
if not x == 3:
    print("x is not equal to 3")
x and y are equal to 5
Either x or z is equal to 5
x is not equal to 3


You can create truth tables with logical operators.

A truth table lists all the possible True/False values that two booleans (P and Q is the convention) can take. These True/False values are then used to evaluate a third expression to see if all cases are true.

An example below is an application of DeMorgan's Law.

def checkDeMorgans():
    if not a or not b == (not (a and b)): 
        print("DeMorgan's Law is true")

a = True
b = True

checkDeMorgans()

a = False
b = True 
checkDeMorgans()

a = True
b = False 
checkDeMorgans()

a = False
b = False
checkDeMorgans()
DeMorgan's Law is true
DeMorgan's Law is true
DeMorgan's Law is true
DeMorgan's Law is true

Order of operations

Relational operators are evaluated first (ex: >, <), while logical operators are evaluated afterwards (ex: and, or, not)

3.6 Conditionals

Vocab

Algorithm: A set of instructions to do a certain task

Selection: Determines which parts of codes are ran based on if they evaluate to true or false. Used with if statements.

Conditional: Different code statements are executed based of if they evaluate to true or false (boolean expressions (See 3.5))

Conditionals

Below is an example of an if else statement that relates to binary

This should be pretty straightforward in demonstrating what an if statement is, so I'm not going to do a code example for an if statement. Check the code cell below the next to see an example of a elif.

binaryString = input("Enter your binary string in 8 digits")

if binaryString[0:1] == 1:
    print("Your binary number is greater than or equal to 128")
else:
    print("Your binary number is less than 128")
Your binary number is less than 128
weather = "rainy"

if weather == "sunny":
    print("Remember to bring a hat")
elif weather == "windy":   
    print("Go fly a kite!")
else: 
    print("Remember to bring an umbrella")
Remember to bring an umbrella

3.7 Nested Conditionals

Nested conditionals are essentially conditionals within conditionals.

Below is an example of nested if statements in Python

num = int(input("Enter a number"))
print("Enter a number: " + str(num))

if num > 0:
    print("Your number is positive")
    
    if num > 50: 
        print("Your number is also greater than 50")
        
else: 
    print("Your number is negative")
    
    if num < -50: 
        print("Your number is also less than -50")
Enter a number: 60
Your number is positive
Your number is also greater than 50