1 00:00:00,000 --> 00:00:03,000 Let's now improve that if structure. 2 00:00:03,001 --> 00:00:09,000 So with what we've seen, we can execute this block of code if that condition is true. 3 00:00:09,001 --> 00:00:14,000 But what if you want to execute a different 4 00:00:14,001 --> 00:00:16,000 set of instructions if the condition is false? 5 00:00:16,001 --> 00:00:18,000 So you want to test if it's true, execute something, 6 00:00:18,001 --> 00:00:21,000 and if it's false, you execute something else. 7 00:00:21,001 --> 00:00:23,000 Well, let's see that. 8 00:00:23,001 --> 00:00:28,001 So first I'm going to come back to the first statement here to make it very simple. 9 00:00:30,000 --> 00:00:31,000 OK. 10 00:00:31,001 --> 00:00:35,000 User age greater or equal than 18, you are an adult. 11 00:00:35,001 --> 00:00:37,000 And then I'm going to come back. 12 00:00:37,001 --> 00:00:38,000 So I press Enter. 13 00:00:38,001 --> 00:00:41,000 And I go back to the first indentation. 14 00:00:41,001 --> 00:00:44,000 And I write else with a color. 15 00:00:44,001 --> 00:00:45,000 I press Enter. 16 00:00:45,001 --> 00:00:47,000 I have a new indentation. 17 00:00:47,001 --> 00:00:52,000 Print you're not an adult yet. 18 00:00:52,001 --> 00:00:57,000 So in this case-- so first is going to check if that is true. 19 00:00:57,001 --> 00:01:02,000 If that is true, it's going to execute the next indented block of code. 20 00:01:02,001 --> 00:01:07,000 And if that is not true-- so if this is false-- we go to the else. 21 00:01:07,001 --> 00:01:10,000 And then we can execute that block of code. 22 00:01:10,001 --> 00:01:12,000 So now let's run this. 23 00:01:12,001 --> 00:01:13,000 OK. 24 00:01:13,001 --> 00:01:16,000 So we still have the same time and end of program. 25 00:01:16,001 --> 00:01:20,000 But you have you're an adult because user age is 30. 26 00:01:20,001 --> 00:01:22,000 Now you're going to put 17. 27 00:01:22,001 --> 00:01:26,000 And you can see you are not an adult yet. 28 00:01:26,001 --> 00:01:28,000 So you can use if and else. 29 00:01:28,001 --> 00:01:32,000 And you can also add intermediate conditions 30 00:01:32,001 --> 00:01:37,000 with else if or in Python it's named elif. 31 00:01:37,001 --> 00:01:40,000 And let's see that with this structure here. 32 00:01:40,001 --> 00:01:44,000 So I'm first going to modify this. 33 00:01:44,001 --> 00:01:45,000 OK. 34 00:01:45,001 --> 00:01:48,000 Because summertime is actually-- so from the north hemisphere-- 35 00:01:48,001 --> 00:01:50,000 it's June, July, and August. 36 00:01:50,001 --> 00:01:51,000 OK. 37 00:01:51,001 --> 00:02:00,000 So let's say if month in June, July, August. 38 00:02:00,001 --> 00:02:06,000 So we use the test here to check if one value is in one list. 39 00:02:06,001 --> 00:02:07,000 OK. 40 00:02:07,001 --> 00:02:13,000 So if the value is June or July or August, it's going to work and print summertime. 41 00:02:13,001 --> 00:02:14,000 Let's just test that. 42 00:02:14,001 --> 00:02:15,000 OK. 43 00:02:15,001 --> 00:02:16,001 Summertime. 44 00:02:17,000 --> 00:02:21,000 Now what if I want to test if the month is actually 45 00:02:21,001 --> 00:02:27,000 in the wintertime or the springtime or the Autumn time? 46 00:02:27,001 --> 00:02:29,000 What I can do, I can use elif. 47 00:02:29,001 --> 00:02:30,000 OK. 48 00:02:30,001 --> 00:02:31,000 Not else if. 49 00:02:31,001 --> 00:02:32,000 OK. 50 00:02:32,001 --> 00:02:34,000 Else if can be in other languages. 51 00:02:34,001 --> 00:02:37,000 But for Python, this is going to be elif. 52 00:02:37,001 --> 00:02:47,000 Month in-- let's say we want to test September and then October. 53 00:02:47,001 --> 00:02:57,000 And then November, we can print Autumn time. 54 00:02:57,001 --> 00:03:02,000 So what's going to happen is first, it's going to check that this condition is true. 55 00:03:02,001 --> 00:03:07,000 If yes, it's going to execute that block of code and then exit. 56 00:03:07,001 --> 00:03:08,000 OK. 57 00:03:08,001 --> 00:03:10,000 Once a condition is true, a block of code is executed. 58 00:03:10,001 --> 00:03:12,000 And then the if will exit. 59 00:03:12,001 --> 00:03:16,000 If this is not true, it's going to go to the next 60 00:03:16,001 --> 00:03:19,000 elif, if there is one, and check if that is true. 61 00:03:19,001 --> 00:03:22,000 If that is true, it's going to execute that. 62 00:03:22,001 --> 00:03:25,000 And if it's false, it's not going to execute anything. 63 00:03:25,001 --> 00:03:28,000 And then any of the block here will not be executed. 64 00:03:28,001 --> 00:03:30,000 And we continue the execution here. 65 00:03:30,001 --> 00:03:31,000 OK. 66 00:03:31,001 --> 00:03:33,000 Now I'm going to add another elif. 67 00:03:33,001 --> 00:03:36,000 Month in-- so let's do all the year. 68 00:03:36,001 --> 00:03:40,000 So November, we have December. 69 00:03:40,001 --> 00:03:41,000 And then January. 70 00:03:41,001 --> 00:03:45,000 And then February. 71 00:03:47,000 --> 00:03:48,000 OK. 72 00:03:48,001 --> 00:03:53,000 If the month is in that range, we are going to print wind time. 73 00:03:53,001 --> 00:03:58,000 And then-- so here we still have three months in the year. 74 00:03:58,001 --> 00:03:59,000 OK. 75 00:03:59,001 --> 00:04:01,000 We have March, April, and May. 76 00:04:01,001 --> 00:04:04,000 I could do elif month in March, April, and May. 77 00:04:04,001 --> 00:04:06,000 But I can just do els. 78 00:04:06,001 --> 00:04:09,000 Because, well, that's the only possibility that's left. 79 00:04:09,001 --> 00:04:13,000 So els print spring time. 80 00:04:14,000 --> 00:04:16,000 We first check if that is true. 81 00:04:16,001 --> 00:04:18,000 We print summer time with exit. 82 00:04:18,001 --> 00:04:19,000 If that is false, we check this. 83 00:04:19,001 --> 00:04:25,000 If this is true, we print out on time and exit. 84 00:04:25,001 --> 00:04:29,000 If this is still false, we continue with this. 85 00:04:29,001 --> 00:04:31,000 And if this is true, we print winter time. 86 00:04:31,001 --> 00:04:35,000 If this is still false, we go to the els. 87 00:04:35,001 --> 00:04:37,000 And then we print spring time. 88 00:04:37,001 --> 00:04:38,000 OK. 89 00:04:38,001 --> 00:04:40,000 So we have July, which is summer time. 90 00:04:40,001 --> 00:04:42,000 Let's test with September. 91 00:04:44,000 --> 00:04:45,000 You can see Autumn time. 92 00:04:45,001 --> 00:04:46,000 OK. 93 00:04:46,001 --> 00:04:47,000 Because this is false. 94 00:04:47,001 --> 00:04:48,000 This is true. 95 00:04:48,001 --> 00:04:49,000 So we execute that. 96 00:04:49,001 --> 00:04:52,000 And then we skip all the rest. 97 00:04:52,001 --> 00:04:54,000 Now let's say May. 98 00:04:54,001 --> 00:04:57,000 You can see spring time. 99 00:04:57,001 --> 00:04:58,000 OK. 100 00:04:58,001 --> 00:04:59,000 We are in the pets. 101 00:04:59,001 --> 00:05:03,000 So you can test here with different values. 102 00:05:03,001 --> 00:05:04,000 OK. 103 00:05:04,001 --> 00:05:06,000 To see where you end up in the if structure. 104 00:05:06,001 --> 00:05:07,000 All right. 105 00:05:07,001 --> 00:05:11,000 Now you are able to use if structure to choose 106 00:05:11,001 --> 00:05:14,000 what to execute depending on different conditions. 107 00:05:14,001 --> 00:05:17,000 This will allow you to make your programs much more dynamic.