1 00:00:00,410 --> 00:00:06,350 Often you will want to take one action when a conditional test passes and different action in other 2 00:00:06,350 --> 00:00:07,070 cases. 3 00:00:07,990 --> 00:00:11,710 Python's if else syntax makes this possible. 4 00:00:11,740 --> 00:00:17,710 The if else block is similar to a simple if statement, but the else statement allows you to define 5 00:00:17,710 --> 00:00:26,200 an action or a set of actions that are executed and run when the conditional test fails. 6 00:00:26,560 --> 00:00:30,820 When the if test fails, and in this case, we will execute else. 7 00:00:30,820 --> 00:00:33,220 So we will display some message here. 8 00:00:33,250 --> 00:00:43,210 Now let's let's write something that, for example, we have a car, a car here. 9 00:00:44,590 --> 00:00:47,680 And of sexual stroke here. 10 00:00:53,350 --> 00:01:00,550 And we don't this we don't want this car to, for example, older than ten years. 11 00:01:00,550 --> 00:01:01,270 Right. 12 00:01:05,350 --> 00:01:07,020 So let's make this. 13 00:01:07,910 --> 00:01:08,540 An. 14 00:01:09,710 --> 00:01:10,580 Years. 15 00:01:11,680 --> 00:01:15,820 So we want this car to less than ten years. 16 00:01:15,820 --> 00:01:18,820 So we will want a new car, Right. 17 00:01:18,850 --> 00:01:25,180 And that's why we're going to do here that let's make, for example, car model. 18 00:01:25,930 --> 00:01:27,970 Car model here. 19 00:01:27,970 --> 00:01:33,160 It's in this case, let's make it BMW and car here. 20 00:01:33,310 --> 00:01:35,380 In this case, let's make it. 21 00:01:39,420 --> 00:01:41,310 In 2015. 22 00:01:41,610 --> 00:01:46,020 And here we will add if our. 23 00:01:48,180 --> 00:01:49,070 Car model. 24 00:01:49,080 --> 00:01:54,000 So car year -2023in this case. 25 00:01:54,000 --> 00:01:57,690 Of course, today's year is 2023. 26 00:01:57,900 --> 00:01:59,820 And actually, let's delete this for now. 27 00:02:04,520 --> 00:02:05,660 So delete this. 28 00:02:07,220 --> 00:02:08,600 And now. 29 00:02:11,910 --> 00:02:15,720 Career -2020 23. 30 00:02:17,870 --> 00:02:18,710 Is. 31 00:02:22,930 --> 00:02:31,090 So our career, for example, in this case, our career is going to be eight, while eight is less than 32 00:02:31,090 --> 00:02:33,210 ten, then it will evaluate three. 33 00:02:33,220 --> 00:02:33,700 Right? 34 00:02:33,700 --> 00:02:39,490 So in this case, print your car is. 35 00:02:42,820 --> 00:02:44,800 And we will also use format here. 36 00:02:44,830 --> 00:02:51,130 Format your your car is a car here. 37 00:02:51,760 --> 00:02:59,770 The -273 years old and it's almost new. 38 00:02:59,770 --> 00:03:00,430 Right. 39 00:03:00,880 --> 00:03:03,580 And here we will also add else statement. 40 00:03:03,580 --> 00:03:12,850 If our car is has age of more than ten, we will write this here else print. 41 00:03:13,980 --> 00:03:15,090 Sorry. 42 00:03:15,240 --> 00:03:18,120 I don't want or sorry. 43 00:03:19,160 --> 00:03:20,600 Our company. 44 00:03:21,730 --> 00:03:22,450 Doesn't. 45 00:03:24,520 --> 00:03:26,470 Buys cars. 46 00:03:26,890 --> 00:03:28,960 Buy cars. 47 00:03:29,230 --> 00:03:32,650 That more that. 48 00:03:33,800 --> 00:03:36,950 Age is more than ten. 49 00:03:36,950 --> 00:03:43,420 And after I will print again and your your cars. 50 00:03:43,430 --> 00:03:44,690 Your car. 51 00:03:44,780 --> 00:03:46,220 Age is. 52 00:03:49,900 --> 00:03:54,760 Of course we need to add F here format per year. 53 00:03:54,970 --> 00:03:58,780 Per year -2023. 54 00:03:58,840 --> 00:04:02,680 Your car or let's actually instead of car we can also write BMW. 55 00:04:02,680 --> 00:04:03,130 Right. 56 00:04:03,130 --> 00:04:04,990 So or car model. 57 00:04:05,020 --> 00:04:10,450 Car model is years old and it's almost new. 58 00:04:11,670 --> 00:04:14,280 And we can buy it. 59 00:04:14,280 --> 00:04:18,870 And here also our company doesn't buy BMW. 60 00:04:18,960 --> 00:04:21,450 Let's actually add another format here. 61 00:04:21,690 --> 00:04:25,260 So we will add F here and format. 62 00:04:26,370 --> 00:04:26,970 Car. 63 00:04:28,510 --> 00:04:32,570 Model that age is more than ten and career here. 64 00:04:32,590 --> 00:04:33,940 Now let's run this. 65 00:04:34,060 --> 00:04:37,320 And as you can see here, your BMW oops. 66 00:04:37,330 --> 00:04:40,660 Actually, we need to do this opposite here. 67 00:04:42,820 --> 00:04:44,110 Sorry for this. 68 00:04:45,260 --> 00:04:49,880 We got right numbers, but we got negative here, so we need to do this. 69 00:04:50,150 --> 00:04:55,100 We need to subtract our career from 2023. 70 00:04:56,740 --> 00:04:58,050 Let's do the same here. 71 00:04:58,060 --> 00:04:59,260 And now. 72 00:04:59,260 --> 00:05:03,130 Your BMW is eight years old and it's almost new. 73 00:05:03,160 --> 00:05:06,340 We can buy it, but in LCR. 74 00:05:08,290 --> 00:05:15,430 A else comment here is didn't executed because if here evaluated true. 75 00:05:15,460 --> 00:05:25,270 If we make our car here for example 2005 then it will give your BMW is 18 years old and it almost no 76 00:05:25,870 --> 00:05:27,040 car here. 77 00:05:27,070 --> 00:05:29,380 Of course we need to change it again. 78 00:05:29,530 --> 00:05:32,050 2023 minus car year. 79 00:05:32,050 --> 00:05:34,030 And here we will get else here. 80 00:05:34,060 --> 00:05:37,330 Sorry, our company doesn't buy that buy BMW. 81 00:05:37,330 --> 00:05:41,470 That age is more than ten and your car age is 18. 82 00:05:41,470 --> 00:05:42,280 So. 83 00:05:43,340 --> 00:05:47,480 First in first example, actually, let's try again 2015. 84 00:05:48,020 --> 00:05:58,820 So in this example, our if conditional test passes and the first block of this print here call is executed. 85 00:05:58,820 --> 00:06:06,680 So if the test evaluates to false, then the else block is executed, for example, 2005. 86 00:06:06,680 --> 00:06:13,580 And in this case, our false will not be executed because it is here because. 87 00:06:15,490 --> 00:06:17,170 It is false, right? 88 00:06:17,170 --> 00:06:18,770 So it will not execute. 89 00:06:18,790 --> 00:06:22,630 Instead, our LS command here will execute here. 90 00:06:23,290 --> 00:06:29,260 So this code works because it has only two possible situations to evaluate. 91 00:06:29,740 --> 00:06:33,620 A car is either old enough or not. 92 00:06:33,640 --> 00:06:34,060 Right. 93 00:06:34,060 --> 00:06:42,570 So the if else structures works well in situations in which you want Python to always execute one of 94 00:06:42,580 --> 00:06:44,470 possible situations or actions. 95 00:06:44,470 --> 00:06:52,570 So in simple if else chain like this, one of two actions will always be executed. 96 00:06:52,570 --> 00:06:56,850 No matter if our car here is 1000 or so here, as you can see here. 97 00:06:56,860 --> 00:07:01,360 So our company owned by BMW age is more than ten.