1 00:00:00,710 --> 00:00:01,460 Hello. 2 00:00:01,670 --> 00:00:04,340 Welcome to another lecture of our python course. 3 00:00:04,340 --> 00:00:05,850 And my name is Stephan. 4 00:00:05,870 --> 00:00:12,950 In this lecture you will learn about conditions, but you will also learn if Elif and else chain. 5 00:00:12,950 --> 00:00:17,420 So often you will need to test more than just two possible situations. 6 00:00:17,420 --> 00:00:23,240 And to evaluate these you can use python's if Elif else syntax. 7 00:00:23,240 --> 00:00:31,760 So Python executes only one block in an if Elif else chain, so it runs each conditional test in order 8 00:00:31,760 --> 00:00:33,590 until one passes. 9 00:00:33,590 --> 00:00:40,280 So when a test passes, the code following that test is executed and Python skips the rest of the tests. 10 00:00:40,280 --> 00:00:44,180 So many real world situations involve more than two possible conditions. 11 00:00:44,180 --> 00:00:51,350 For example, consider a bus ticket, a mechanism bus ticket technology that charges different rates 12 00:00:51,350 --> 00:00:53,180 for different age groups. 13 00:00:53,180 --> 00:00:59,930 For example, bus tickets for anyone under age eight is free bus ticket for anyone between the ages 14 00:00:59,930 --> 00:01:02,880 of eight and 18 is $50. 15 00:01:02,880 --> 00:01:08,730 Bus ticket from anyone age 18 or older is $90. 16 00:01:08,730 --> 00:01:15,240 So how can we use an If statement to determine a person's bus ticket rate? 17 00:01:15,240 --> 00:01:18,270 So this code we will write here. 18 00:01:18,270 --> 00:01:24,060 For example, let's make the my age here, for example. 19 00:01:24,060 --> 00:01:31,710 My age is 25 for and we will change this age to test different conditions. 20 00:01:32,250 --> 00:01:40,320 If our age is here, let's actually let's actually write the first statement here. 21 00:01:41,400 --> 00:01:46,920 So if a ticket for anyone, a bus ticket for anyone under age is free. 22 00:01:47,040 --> 00:01:54,240 And so if age is less than eight, then we will print. 23 00:01:55,020 --> 00:01:55,950 Your. 24 00:01:58,470 --> 00:02:00,750 You need to pay. 25 00:02:02,320 --> 00:02:03,370 $0. 26 00:02:05,360 --> 00:02:05,600 Or. 27 00:02:05,600 --> 00:02:11,930 Yeah, let's actually, instead of writing that your ticket is free here. 28 00:02:12,460 --> 00:02:13,210 And. 29 00:02:13,830 --> 00:02:17,090 Here of my age. 30 00:02:17,100 --> 00:02:27,300 And after that we will add else if I will explain all of this here else if Elif here, Elif my age and 31 00:02:27,300 --> 00:02:29,970 here, let's let's actually take this. 32 00:02:31,810 --> 00:02:32,170 Here. 33 00:02:32,290 --> 00:02:34,090 So we did this. 34 00:02:34,090 --> 00:02:34,600 Now. 35 00:02:34,600 --> 00:02:35,500 Now we are. 36 00:02:35,920 --> 00:02:37,570 We need to do the second here. 37 00:02:37,570 --> 00:02:44,140 So bus ticket for anyone between the ages of eight and 18 is $50. 38 00:02:44,170 --> 00:02:45,550 Now we will write that. 39 00:02:45,550 --> 00:02:50,620 So if my age is less than. 40 00:02:56,720 --> 00:02:57,260 Here. 41 00:02:57,290 --> 00:03:03,350 Anyone between the ages of four and 18 is $50. 42 00:03:08,340 --> 00:03:11,410 And here we will write 18. 43 00:03:12,660 --> 00:03:13,320 My age. 44 00:03:13,320 --> 00:03:13,680 18. 45 00:03:13,680 --> 00:03:15,450 So you will explain here. 46 00:03:15,450 --> 00:03:20,610 You might be wondering why we didn't included eight here, which I will explain now after writing this 47 00:03:20,610 --> 00:03:29,010 code and you will understand because here print we will use your ticket. 48 00:03:29,130 --> 00:03:30,870 Ticket is. 49 00:03:33,700 --> 00:03:36,850 Bicycle, $50, $50. 50 00:03:37,270 --> 00:03:42,700 And after that, we will also add clouds here and here. 51 00:03:43,580 --> 00:03:44,210 Print. 52 00:03:45,130 --> 00:03:49,750 So your ticket is $100. 53 00:03:52,940 --> 00:03:55,500 So also we did this section. 54 00:03:55,520 --> 00:03:56,990 Mark This one. 55 00:03:58,600 --> 00:03:59,160 That's it. 56 00:03:59,170 --> 00:04:07,720 So firstly, the if test checks whether a person is under eight years old. 57 00:04:07,720 --> 00:04:15,820 So when test passes, an appropriate message is printed and Python skips the rest of the tests. 58 00:04:15,820 --> 00:04:19,780 And here, let's try this for example, if my age was five. 59 00:04:20,300 --> 00:04:28,280 And here, as you can see, your ticket is free and the life line is really another if test so which 60 00:04:28,280 --> 00:04:32,630 runs only if this previous test fails. 61 00:04:32,630 --> 00:04:36,980 So in this case it didn't fail here and this code didn't run. 62 00:04:37,990 --> 00:04:44,200 So at this point in the chain, we know that the person is at least eight years old because the first 63 00:04:44,200 --> 00:04:44,890 test. 64 00:04:45,960 --> 00:04:48,840 Uh, is let's actually, uh, make here. 65 00:04:50,750 --> 00:04:56,810 Now, in this case, we know the person is at least eight years old because the first test didn't run 66 00:04:56,810 --> 00:04:57,380 right. 67 00:04:57,770 --> 00:05:08,780 So if the person is under 18, an appropriate message is printed and Python will not execute the else 68 00:05:08,780 --> 00:05:09,140 block. 69 00:05:09,140 --> 00:05:19,910 Now, Python will now execute because our Elif here, our Elif is executed, but also our if didn't 70 00:05:19,910 --> 00:05:26,630 execute because it didn't meet meet the conditions here because my age was ten. 71 00:05:27,440 --> 00:05:28,160 And. 72 00:05:29,080 --> 00:05:36,790 Here if my age under eight, then it didn't print because my age is not under eight. 73 00:05:36,940 --> 00:05:43,990 You learned that in expressions previous lectures, and here we have Elif, which is my age, is under 74 00:05:43,990 --> 00:05:47,740 18 and here we have ten here. 75 00:05:47,830 --> 00:05:56,260 So in this example here, the if test, let's actually yes, here in this example, the if test evaluates 76 00:05:56,260 --> 00:05:59,740 false so it's code block is not executed. 77 00:05:59,740 --> 00:06:06,400 However, the Elif test evaluates true which is the ten is less than 18. 78 00:06:06,400 --> 00:06:06,640 Right. 79 00:06:06,640 --> 00:06:08,920 So its code is executed. 80 00:06:08,920 --> 00:06:16,420 So the output is one sentence here informing the user that admission cost is $50. 81 00:06:16,420 --> 00:06:18,280 So your ticket is $50. 82 00:06:18,280 --> 00:06:27,700 Any age greater than 17 would cause the first the if and Elif tests to fail in this situations here, 83 00:06:27,700 --> 00:06:35,030 let's actually make it 20 In this situations the else block will be executed here and as you can see 84 00:06:35,030 --> 00:06:37,010 here else block is executed. 85 00:06:37,010 --> 00:06:44,030 This block is in the if and Elif didn't execute because they didn't meet the condition here and here 86 00:06:44,030 --> 00:06:47,180 we have the else block executed here. 87 00:06:48,010 --> 00:06:51,250 And we printed this message here. 88 00:06:52,760 --> 00:06:59,570 So rather than the printing the ticket price with the if else block, it would be more concise to just 89 00:06:59,570 --> 00:07:04,160 set the price inside the if else chain. 90 00:07:04,160 --> 00:07:09,300 And then we have a single print call that runs after the chain has been evaluated. 91 00:07:09,320 --> 00:07:14,320 So now we will let's delete this and we will do another example with that. 92 00:07:14,330 --> 00:07:20,390 So here, if my age is 20, remember that the. 93 00:07:21,910 --> 00:07:26,610 For under a under age of eight, it will be free for bus ticket. 94 00:07:26,620 --> 00:07:33,850 So my age will be or and we will also add price here. 95 00:07:34,650 --> 00:07:36,240 Price is going to. 96 00:07:37,360 --> 00:07:42,950 Zero and my age eight while my age is other than 18. 97 00:07:42,970 --> 00:07:45,610 Then the price is going to be price. 98 00:07:46,300 --> 00:07:57,730 Price is going to be 25, the 50 and else if our age is greater than 18, we will praise we will use 99 00:07:57,730 --> 00:08:00,400 the price 19 here. 100 00:08:01,100 --> 00:08:06,770 And after that we will print here we will use format. 101 00:08:06,800 --> 00:08:14,690 Your bus ticket cost is here and we will use this price. 102 00:08:14,690 --> 00:08:15,590 That's it. 103 00:08:15,740 --> 00:08:18,110 So here, let's run this. 104 00:08:18,110 --> 00:08:19,570 And here we are. 105 00:08:19,570 --> 00:08:24,320 Our age is 20 and our else block is executed here. 106 00:08:24,320 --> 00:08:32,930 So the indented lines here you see here, these are the indented lines set the value of price according 107 00:08:32,960 --> 00:08:36,230 to the person's age, as in the previous example. 108 00:08:36,410 --> 00:08:48,470 So after the price is set by the if here, if Elif and else conditions as separate on indented print 109 00:08:48,470 --> 00:08:52,250 call uses this value the price value. 110 00:08:52,970 --> 00:08:54,290 Um, two. 111 00:08:54,920 --> 00:08:56,870 So price value. 112 00:08:58,110 --> 00:08:59,850 To display this message, right? 113 00:09:02,250 --> 00:09:08,690 And so this here, this code produces the same output as the previous example. 114 00:09:08,700 --> 00:09:14,550 But the purpose of the if Elif else chain is narrower. 115 00:09:14,550 --> 00:09:21,870 So instead of determining a price and displaying a message, it simply determines the ticket price. 116 00:09:22,020 --> 00:09:29,220 And in addition to being more efficient, the reveals this code here is easier to modify than the original 117 00:09:29,220 --> 00:09:33,620 approach that we did in earlier in this lecture. 118 00:09:33,630 --> 00:09:39,960 So to change the text of the ultimate message, you will need to change only this print call rather 119 00:09:39,960 --> 00:09:44,760 than three separate print calls that was here before.