Week2 Lecture2
Week2 Lecture2
Coffee
Break
Making Choices
statements
statements
Sequential structure
Conditional structure
True
statements condition
statements statements
False
statements
Boolean Type
§ Python type bool has only two possible values: True and False
Greater than or equal to >= 3>=4 False Python uses == for equality,
because = is used for assignment
Not equal to != 3!=4 True
Using Python as your (bool) Calculator
§ Let’s take a look at how this works in
Python!
§ Boolean type
§ Relational (or comparison) operators Open your
notebook
Click Link:
1. Introducing
Booleans
Logical Operators
§ Take Boolean operands and evaluate to Boolean values
Operator Precedence
not highest
and
or lowest
All the Operators!
1. Arithmetic (+, -, /, etc.)
2. Relational (<, ==, etc.)
3. Logical/Boolean (not, and, or)
Click Link:
2. Logical Operators
Binary Operators
§ Rules for evaluation
1. Evaluate the left operand (i.e. expression) to a value and
replace that operand expression with that value
POSSIBLE SHORT CIRCUIT
2. Evaluate the right operand (i.e. expression) to a value and
replace that operand expression with that value
3. Apply the operator to the two resultant values
Short-Circuit (Lazy) Evaluation
§ The or operator evaluates to True if and
only if at least one operand is True
§ If the first operand is True, the second
condition will not be checked!
§ The and operator evalutes to False if and
only if at least one operator is False
§ If the first operand is False, the second
condition will not be checked!
§ Similar to how in a Multiple Choice
Question on a test, if you for sure know
the answer is A, you can save time not
reading B, C, D, and E!
Coding Time!
§ Let’s go experiment with some of
what we just saw
§ Short-circuit (lazy) evaluation
Open your
notebook
Click Link:
3. Lazy Evaluation
Making Choices
statements
statements
Sequential structure
Conditional structure
? True
statements condition
statements statements
False
statements
The if statement
§ A general form of an if statement is as follows:
if expression:
body
if expression:
body1
else:
body2
§ ONLY 1 of body1 or body2 will be executed.
§ if statement is True, executes body1
§ if statement is False, executes body2
if-else Statement Example
grade = 51
if grade <= 50:
print(“You failed APS106…”)
else:
print(“Hooray you passed!”)
Click Link:
4. Introducing if
statements
Booleans, Logic, & Conditional “if” Statements.
Week 2 | Lecture 2 (2.2)