Conditionals & Control Flow
Lesson 1 of 2
1. 1
Just like in real life, sometimes weâ€™d like our code to be able to make decisions. The Python programs weâ€™ve written so far have had one-track minds: they can add two numbers or print something, bâ€¦
2. 2
Letâ€™s start with the simplest aspect of control flow: comparators. There are six: Equal to (==) >>> 2 == 2 True >>> 2 == 5 False Not equal to (!=) >>> 2 != 5 True >>> 2 != 2 False â€¦
3. 3
Excellent! It looks like youâ€™re comfortable with basic expressions and comparators. But what about extreme expressions and comparators?
4. 4
Comparisons result in either True or False, which are booleans as we learned before in this exercise . # Make me true! bool_one = 3 < 5 Letâ€™s switch it up: weâ€™ll give the boolean, and youâ€™ll wrâ€¦
5. 5
Boolean operators compare statements and result in boolean values. There are three boolean operators: 1. and, which checks if both the statements are True; 2. or, which checks if at least one â€¦
6. 6
The boolean operator and returns True when the expressions on both sides of and are true. For instance: * 1 3 is False.
7. 7
The boolean operator or returns True when at least one expression on either side of or is true. For example: * 1 3 is True; * 1 > 2 or 2 > 3 is False.
8. 8
The boolean operator not returns True for false statements and False for true statements. For example: * not False will evaluate to True, while not 41 > 40 will return False.
9. 9
Boolean operators arenâ€™t just evaluated from left to right. Just like with arithmetic operators, thereâ€™s an order of operations for boolean operators: 1. not is evaluated first; 2. and is evaluateâ€¦
10. 10
Great work! Weâ€™re almost done with boolean operators. # Make me false bool_one = (2 <= 2) and â€śAlphaâ€ť == â€śBravoâ€ť
11. 11
if is a conditional statement that executes some specified code after checking if its expression is True. Hereâ€™s an example of if statement syntax: if 8 < 9: print â€śEight is less than nine!â€ť â€¦
12. 12
Letâ€™s get some practice with if statements. Remember, the syntax looks like this: if some_function(): # block line one # block line two # et cetera Looking at the example above, in the eveâ€¦
13. 13
The else statement complements the if statement. An if/else pair says: â€śIf this expression is true, run this indented code block; otherwise, run this code after the else statement.â€ť Unlike if, elsâ€¦
14. 14
elif is short for â€śelse if.â€ť It means exactly what it sounds like: â€śotherwise, if the following expression is true, do this!â€ť if 8 > 9: print â€śI donâ€™t get printed!â€ť elif 8 < 9: print â€śI get prâ€¦
15. 15
Really great work! Hereâ€™s what youâ€™ve learned in this unit: Comparators 3 = 5 10 == 10 12 != 13 Boolean operators True or False (3 = 5) this() and not that() **Conditional statemeâ€¦

## What you'll create

Portfolio projects that showcase your new skills

## How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory