Welcome back, everyone.
-Bonnie
1-1:17 -> Review (check Post 2 for details)
1:17-1:23 -> Review Quiz
1:23-1 -> Boolean Expressions*
1:28-1:31 -> If Statements
1:31-1:34 -> Else Statements
1:34-136 -> Elif Statements
1:36-1:40 -> Comparison Operators
1:50-1:58 -> Sample Problem
1:58-2:30 -> Individual Practice
*Today's class was recorded, starting from Boolean expressions.
REVIEW QUIZ
Integer (Floor is used, so there are no floats in this expression, only whole numbers)
Boolean (Booleans are True or False)
String (Denoted by "")
Float (Numbers with decimals)
BOOLEAN EXPRESSIONS
An expression is a combination of values and operations in a program. They can be of any data type. 1 + 2 * 3 is an expression with value 7.
A boolean expression is just an expression that evaluates to a boolean. There are special operators we use for this called logical operators - and two that we will introduce now are "and" and "or". In Python, we just write the words "and" and "or" directly into our code. In other languages, you might have to use specific symbols.
AND is an operator that compares two clauses. If both clauses are true, it will return True. If one of both are false, it will return False.
print(1 * 10 == 10 and 2 == 2) print(10 == 1 and 10 == 10) print(1 == 2 and 1 == 3)
----------------------------------- ----------------------------------- ------------------------------
True False False
OR is an operator that also compares two clauses, but it is more inclusive than and. If at least one clause is true, it will return True. Only when both are false will it return False. (So of course is both are true, it returns True.)
print(1 * 10 == 10 and 2 == 2) print(10 == 1 and 10 == 10) print(1 == 2 and 1 == 3)
----------------------------------- ----------------------------------- ------------------------------
True True False
IF STATEMENTS
If is a keyword that runs some code if a given condition is met. This condition is where we often use the logical operators we covered above.
if (condition):
code
If the condition is not met, then the code inside the if statement will not run, and the program will just continue on to the code under the if statement. Code inside the if statement needs to be specially formatted so that the program understands it belongs to the if statement - we use indents for this.
Press TAB on your keyboard at the beginning of a single line or highlight all the lines you want indented and hit TAB to indent your work. If you've indented too far, highlight the code and press Shift+TAB to reverse indent your code.
ELSE STATEMENTS
Else is a keyword similar to If. Else statements are attached to if statements and will run if the condition in the if statement is not met. It will only run as the very last resort in any chain of if/elif statements and will be canceled if any of the statements before it run.
if False:
code A
else:
code B
^ In this pseudocode (summarized, simplified version of real code), the condition in the if statement equates to False, so Code A will not run. The program will then default to an else statement, if there is one, and since there is one, Code B will run.
ELSE IF (ELIF) STATEMENTS
Elif is the final piece to the if statement trifecta. Elif stands for Else If, but Python has just abbreviated it. Else if statements run if the if statements and the other else if statements before it have conditions that are not met. In terms of priority, the program will first check the if statement, then the else if statements in the order they are written, then the else statement.
if False:
code A
elif False:
code B
elif True:
code C -> after running Code C, the program exits this block of if statements and
continues on to the rest of the program.
else: -> skipped
code D -> skipped
^ In this example, as the if and first elif statements are unable to run, Code C is executed. This means the else statement is not run.
if True:
code A
elif True:
code B
else:
code C
^In this example, while the elif statement has its condition met, it will not run, as the if statement has priority. The else statement also does not run. So in this example, only Code A is run.
COMPARISON OPERATORS
== - equals
< - less than
> - greater than
<= - less than or equal to
>= - greater than or equal to
We can use these operators in if statements to determine if some conditions are met.
PRACTICE PROBLEM
^ The order of the conditions doesn't matter - as long as you are using one if, one elif, and one else statement, you are all good.
INDIVIDUAL WORK TIME
Q1 - Given an integer A, check if it is odd or even. If A is odd, print “odd”, and print “even” if it is even. (We did this question in class together.)
^ The % modulus operator (mod for short) was covered in Class 1. It is a division operation that returns the remainder of the division rather than the standard quotient. (So 16 % 4 is 0.)
Q4 - Given 2 integers A and B, check if the product of A and B is even without multiplying them. If it is, print "Yes", else, print "No".
^ If at least one of the two integers is even, their product will be even. This is basically a math question - you just needed to notice that this was true. We can use modulus to determine whether the numbers are odd or even.
ANSWER 6.
a = int(input())
print(a)
b = int( input())
c = int( input())
if (a < b < c):
print("Yes")
elif (a > b > c):
print("Yes")
else:
print ("No")
ANSWER 7.
a = input()
print(a)
b = input()
c = input()
if (len(a) == len(b) == len(c)):
print("Yes")
else:
print ("No")