1 00:00:00,000 --> 00:00:04,170 Let's now improve that if structure. So what 2 00:00:04,170 --> 00:00:07,410 we've seen, we can execute this block of code if 3 00:00:07,410 --> 00:00:10,410 that condition is True. But what if you want to 4 00:00:10,410 --> 00:00:14,070 execute a different set of instruction, if the 5 00:00:14,070 --> 00:00:16,950 condition is False. So you want to test if it's 6 00:00:16,950 --> 00:00:19,650 True, execute something. And if it's False, you 7 00:00:19,650 --> 00:00:23,250 execute something else. Well, let's see that. So 8 00:00:23,280 --> 00:00:25,980 first, I'm going to come back to the first 9 00:00:26,610 --> 00:00:29,990 statement here to make it very simple. Okay, 10 00:00:29,990 --> 00:00:34,380 user_age greater or equal than 18, You're an 11 00:00:34,380 --> 00:00:37,260 adult. And then I'm going to come back. So I press 12 00:00:37,260 --> 00:00:40,980 Enter, and I go back to the first indentation and 13 00:00:40,980 --> 00:00:45,840 I write else with a colon. I press Enter, I have a 14 00:00:45,840 --> 00:00:53,100 new indentation, print, You're not an adult yet. So 15 00:00:53,100 --> 00:00:56,670 in this case, so first it's going to check if that 16 00:00:56,670 --> 00:00:59,220 is True. If that is True, it is going to execute 17 00:00:59,250 --> 00:01:03,030 the next indented block of code. And if that is 18 00:01:03,030 --> 00:01:06,450 not True, so if this is False, we go to the else. 19 00:01:06,750 --> 00:01:10,890 And then we can execute that block of code. So now 20 00:01:10,890 --> 00:01:14,550 let's run this. Okay, so we still have the Summer 21 00:01:14,550 --> 00:01:17,730 time, End of program, but you have You're an adult, 22 00:01:17,970 --> 00:01:20,880 because user_age is 30. Now you're gonna need to 23 00:01:20,880 --> 00:01:25,770 put 17, and you can see You're not an adult yet. 24 00:01:26,130 --> 00:01:30,060 So you can use if, and else, and you can also add 25 00:01:30,150 --> 00:01:34,860 intermediate conditions with else if, for in Python, 26 00:01:34,890 --> 00:01:39,360 it's named elif. And let's see that, at this 27 00:01:39,540 --> 00:01:43,800 structure here. So I'm first going to modify this, 28 00:01:43,800 --> 00:01:46,260 okay, because Summer time is actually, so from the 29 00:01:46,260 --> 00:01:50,100 north hemisphere, it's June, July and August. 30 00:01:50,130 --> 00:01:57,660 Okay. So let's say if month in June, July, 31 00:01:57,660 --> 00:01:59,650 [Author typing] 32 00:01:59,650 --> 00:02:03,870 August. So we use the test here to check if one 33 00:02:03,870 --> 00:02:08,639 value is in one list. Okay. So if the value is 34 00:02:08,639 --> 00:02:11,880 June or July or August its going to work and print 35 00:02:11,880 --> 00:02:16,011 Summer time. Let's just test that. Okay, Summer time. 36 00:02:17,430 --> 00:02:20,760 Now, what if I want to test if the month is 37 00:02:20,760 --> 00:02:24,900 actually in the Winter time or the Spring time or 38 00:02:24,960 --> 00:02:29,370 the Autumn time. What I can do, I can use else, 39 00:02:29,460 --> 00:02:33,630 okay, not else if, okay, else if can be in other 40 00:02:33,630 --> 00:02:37,745 languages. But for Python, this is going to be elif 41 00:02:38,460 --> 00:02:45,000 month in let's say, we want to test September, and 42 00:02:45,000 --> 00:02:53,310 then October, then November, we can print Autumn 43 00:02:53,310 --> 00:02:56,904 [Author typing] 44 00:02:56,944 --> 00:03:00,570 time. So what's going to happen is first is going 45 00:03:00,570 --> 00:03:03,420 to check if this condition is True. If yes, it's 46 00:03:03,420 --> 00:03:06,720 going to execute that block of code and then exit 47 00:03:06,750 --> 00:03:09,150 Okay, once the condition is True, a block of code 48 00:03:09,150 --> 00:03:13,020 is executed and then the if will exit. If this 49 00:03:13,020 --> 00:03:16,650 is not True, is going to go to the next elif if 50 00:03:16,680 --> 00:03:19,350 there is one and check if that is True. If that is 51 00:03:19,350 --> 00:03:22,950 True, it is going to execute that. And if it's 52 00:03:22,950 --> 00:03:25,530 False, it's not going to execute anything and then 53 00:03:25,710 --> 00:03:28,110 any of the block here will not be executed and we 54 00:03:28,110 --> 00:03:31,440 continue the execution here. Okay, now I'm going 55 00:03:31,440 --> 00:03:36,300 to add another elif month in, so let's do all 56 00:03:36,300 --> 00:03:40,290 here, so November, we have December, and then 57 00:03:40,860 --> 00:03:49,290 January, and then February. If the month is in that 58 00:03:49,350 --> 00:03:54,300 range, we are going to print Winter time and then 59 00:03:54,600 --> 00:03:57,750 so here we still have three months in the year 60 00:03:57,750 --> 00:04:02,070 okay, we have March, April, and May. I could do if 61 00:04:02,070 --> 00:04:05,430 month in March, April, May, but I can just do else 62 00:04:05,670 --> 00:04:08,460 because well that's the only possibility that's 63 00:04:08,460 --> 00:04:15,420 left. So else print Spring time. We first check if 64 00:04:15,420 --> 00:04:18,329 that is True, we print Summer time, we exit. If that 65 00:04:18,329 --> 00:04:22,890 is False, we check this if, so if this is True, we 66 00:04:22,890 --> 00:04:26,520 print Autumn time and exit. If this is still 67 00:04:26,760 --> 00:04:30,090 False, we continue with this and if this is True, 68 00:04:30,090 --> 00:04:33,810 we print Winter time. If this is still False, we go 69 00:04:33,840 --> 00:04:38,310 to the else and then we print Spring time. Okay, so we 70 00:04:38,310 --> 00:04:41,280 have July which is Summer time. Let's test with 71 00:04:41,310 --> 00:04:46,500 September. You can see Autumn time, okay because 72 00:04:46,500 --> 00:04:49,260 this is False. This is True. So we execute that 73 00:04:49,320 --> 00:04:54,180 and then we skip all the rest. Now let's say May. 74 00:04:54,180 --> 00:04:56,430 [No audio] 75 00:04:56,430 --> 00:04:59,430 You can see Spring time okay, we are in the else. 76 00:05:00,000 --> 00:05:03,510 So you can test here with different values okay to 77 00:05:03,510 --> 00:05:07,620 see where you end up in a structure. All right now 78 00:05:07,620 --> 00:05:10,740 you are able to use a structure to choose what to 79 00:05:10,740 --> 00:05:14,490 execute depending on different conditions. This 80 00:05:14,490 --> 00:05:17,770 will allow you to make your programs much more dynamic.