## Lab 04 Pre Lab

### Logic

Mastery of logic is essential to understanding conditional statements. It is used in pretty much any program that you will ever write. Comparisons are the heart of logical statements. When we write programs, we often want to compare two pieces of information, testing to see if that comparison evaluates to True or False. Then, we alter the way our program operates depending on the result of that comparison.

### Comparison operators

We can make those comparisons using any of the following comparison operators, which compare two pieces of information:

• <    (less than)
• >    (greater than)
• <=   (less than or equal to)
• >=   (greater than or equal to)
• ==   (equivalent to)
• !=   (not equivalent to)

For example:

• num = 500              # Set the value of num
• num < 1000             # This evaluates to True
• 1456 >= num            # This evaluates to True
• 300 != 300             # This evaluates to False
• "hello" == "goodbye"   # This evaluates to False

Notice how you can mix variables and "raw" data and still make valid comparisons. Unlike the assignment operator (=), it doesn't matter what goes on the left hand or right hand side of a comparison operator.

### Logical operators

You can also combine two or more comparison statements by using:

• and    Both comparisons must be True for this to evaluate to True
• or     At least one comparison must be True for this to evaluate to True

For example:

• num = 500
• (500 <= num) and (num <= 1000)              # True
• ("hello" == "hello") and ("dog" == "cat")   # False
• num > 487 or num <= 342                     # True
• "hello" == "hello" or "dog" == "cat"        # True

You do not have to use parentheses around a comparison statement, but it does have the benefit of making your code clearer and easier to read.

A third logical operator available to you is called not. This operates on one logical statement, flipping the truth value of that statement. So, a logical statement that is True will be flipped to False, and a logical statement that is False will be flipped to True.

For example:

• isDog = True
• not isDog             # False
• (4 > 5)               # False
• not (4 > 5)           # True
• 5 > 4                 # True
• not (5 > 4)           # False
• not ("dog" == "cat")  # True

### Conditional statements

Being able to make comparisons is only the first part of conditional statements. We also need a structure to execute different code based on the value of a comparison. There are three such structures available: "if", "if-else", and "if-elif-else". These structures combine with one or more logical statements to form a conditional statement.

### if

A basic "if" statement looks like this:

• if age >= 65:
•     print("If you are", age, "you are a senior.")
The print statement is only executed if the value of the variable age is larger than or equal to 65. Whatever is "inside" the if statement (meaning one indentation level in) will be executed if the logical statement used evaluates to True.

You must have an "if" statement before you use any "elif" statements or an "else" statement.

### if-else

What if you want something different to happen if the logical statement is not True? To do this, just use an "else" statement right after an "if" like so:

• if age >= 65:
•     print("If you are", age, "you are a senior.")
• else:
•     print("If you are", age, "you are young.")

### if-elif-else

What if there are several related logical statements you need to test? Simply use an "elif" in sequence with an "if".

• if age >= 65:
•     print("If you are", age, "you are a senior.")
• elif age >= 45:
•     print("If you are", age, "you are middle aged.")
• elif age >= 25:
•     print("If you are", age, "you are a young adult.")
• else:
•     print("If you are", age, "you are young.")

Important: The very first logical statement that evaluates to True will have its associated code executed, and everything else will be skipped over. This means that only one of the code statments will be executed.