#100DaysOfWeb in Python Transcripts
Lecture: Conditionals and control flow
0:00 This is a quick video on conditionals
0:03 and a break in continue statements.
0:05 First, let's demo a very simple if else.
0:09 Let's use our constant from the previous lesson
0:13 of mean age close to 18.
0:16 And that set my age to 17.
0:22 Now what if you want to determine
0:24 if at my age, I can drive
0:27 being the driving age, the mean age of 18?
0:31 Well, it's very simple to write an if statement.
0:34 So if my age, is greater than
0:38 or equal than mean age I can drive.
0:53 And that doesn't return anything, because I'm not 18 yet.
0:57 So we can add an else block, to print a different message.
1:09 And now, that gets me, cannot drive as expected.
1:17 using a ternary statement. So in that case
1:21 I'm doing the if condition in parentheses.
1:27 And look again at the nice auto complete
1:29 of the Chrome DevTools console.
1:33 So the ternary, I put my conditions in parentheses
1:36 then to question mark
1:37 then you put the code in that hits when it's true.
1:42 Then you do a colon, and you put a code
1:44 that hits when it's false.
1:50 So instead of five lines of code
1:52 I can do this just on one line.
1:53 That looks pretty nice.
1:55 Next, let's do something a bit more advanced.
1:57 So we're going to make a list of names.
1:59 We're going to print the names
2:01 ignoring the ones that start with B
2:03 and stopping when we hit a name that starts with Q.
2:07 So here is an array of names.
2:10 Let me use what I had before
2:13 but add a few names starting with B
2:17 one that starts with Q, where we want to stop
2:21 and two names at the end.
2:28 So an array of seven names
2:30 I'm going to look over with a new syntax.
2:36 Right, that gets them all seven.
2:39 And next I'm going to
2:42 ignore the ones that start with B.
2:48 So we can write an if statement.
2:52 If name starts with
2:57 then we should recognize that from Python.
3:11 We only get the ones with B.
3:14 One in this case. I want to continue.
3:17 So I can use the continue statement.
3:24 So now I got five names
3:26 because the one starting with B are omitted.
3:34 Lastly we said, if we hit a name with Q
3:37 we stopped a loop, and we can use a break.
3:40 So if you have a second condition
3:42 we can use else if.
3:46 And that condition will be
3:49 the name starts with Q.
4:03 I'm going to break out of the loop.
4:06 And now we have only two names
4:08 because Bob was ignored, Tuni and Mike were printed
4:12 Berta was ignored.
4:13 Then we found Quintin and we broke out of the loop.
4:16 So again, as simple as else.
4:21 If my age is greater and equals and mean age they can drive.
4:24 Otherwise I cannot drive.
4:26 And as my age is less than mean age
4:29 cannot drive was printed.
4:32 I can write the shorter using ternary.
4:38 puts condition in parentheses, followed by a question mark.
4:42 Then you have your true code.
4:44 Colon decoded hits when it's false.
4:50 An example of using if else and for loop.
4:53 I got a bunch of names.
4:54 I'm going to ignore names
4:56 that start with B
4:57 and stop when we hit a name starting with Q.
5:01 First we have our loop.
5:04 Then we have an if else if else.
5:07 And actually my console lock defaulted after here.
5:11 But here I put it in an else.
5:12 Should have the same effect.
5:15 If the name starts with B
5:17 I will have to continue to look without doing anything.
5:19 Else if, the name starts with Q
5:21 I'm going to break out to look
5:23 and effectively stops this program.
5:26 If it's neither of those I have to print a name.
5:31 And we saw that Mike and Jillian are the only ones left.
5:35 It's just an example to show you how you can
5:38 combine four if else with continue upgrade
5:41 to manage, to control flow.
5:43 Now there are two major building blocks left
5:48 which are functions and object.
5:50 And I will cover those in the next two videos.