All language subtitles for 027 Control Flow with if _ else and Conditional Operators.en

af Afrikaans
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
tl Filipino
fi Finnish
fr French Download
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,340 --> 00:00:04,180 Here's a question. Have you ever sat in your bath and wondered, 2 00:00:04,540 --> 00:00:07,750 why is it that no matter how forgetful I am, 3 00:00:08,140 --> 00:00:10,450 my bath never overflows like this? 4 00:00:10,690 --> 00:00:14,230 This is something I was thinking about the other day and I realized that it's 5 00:00:14,260 --> 00:00:19,180 not because I'm particularly diligent. I forget stuff all the time. I mean, 6 00:00:19,300 --> 00:00:24,190 pizza, anyone? This is what my pizzas usually look like. Um, 7 00:00:24,490 --> 00:00:29,490 but the reason why a bathtub or the sink doesn't overflow is because of this 8 00:00:30,490 --> 00:00:34,270 fantastic piece of engineering, the overflow. 9 00:00:34,810 --> 00:00:39,810 So this means that whenever the water reaches beyond a certain level about here, 10 00:00:40,810 --> 00:00:43,240 the water starts overflowing 11 00:00:43,270 --> 00:00:47,740 so the bathtub doesn't overflow and annoy your neighbors downstairs. 12 00:00:48,340 --> 00:00:53,110 In fact, we could represent this mechanism with a conditional statement. 13 00:00:53,680 --> 00:00:57,820 When the water level is say greater than 80 centimeters, 14 00:00:58,240 --> 00:01:00,670 then it should drain the water. 15 00:01:01,030 --> 00:01:04,420 But if the water level is not greater than 80 centimeters, 16 00:01:04,600 --> 00:01:07,240 it should continue filling up the tub. 17 00:01:07,660 --> 00:01:12,520 This type of conditional statement is known as an if/else statement. 18 00:01:13,000 --> 00:01:15,340 Depending on a particular condition, 19 00:01:15,580 --> 00:01:18,460 we would do either A or B. 20 00:01:18,910 --> 00:01:22,420 And when we want to write Python code to represent this, 21 00:01:22,600 --> 00:01:23,950 it looks something like this. 22 00:01:24,280 --> 00:01:29,280 There's the keyword if, and then the condition that we're testing for and then a 23 00:01:30,280 --> 00:01:35,280 colon and after the colon we've got an indented block of code which should be 24 00:01:35,800 --> 00:01:39,370 executed if this condition is met, if it's true. 25 00:01:40,120 --> 00:01:41,680 But if it's not true, 26 00:01:42,010 --> 00:01:47,010 then we will skip to the else block and it's just the else keyword with a colon 27 00:01:47,800 --> 00:01:52,090 and then this code block would execute if the condition is false. 28 00:01:52,840 --> 00:01:57,490 So we could represent that previous bathtub situation with code that looks a bit 29 00:01:57,490 --> 00:01:58,150 like this. 30 00:01:58,150 --> 00:02:03,150 Let's say our water level is at 50 centimeters and then we would test if the 31 00:02:03,220 --> 00:02:07,690 water level is greater than 80 centimeters. Well, if that is the case, 32 00:02:07,750 --> 00:02:09,910 then we should drain the water. 33 00:02:10,360 --> 00:02:13,060 But if it's not greater than 80 centimeters, 34 00:02:13,120 --> 00:02:15,400 in other words else, well in this case, 35 00:02:15,430 --> 00:02:17,830 we should just continue filling up the bathtub. 36 00:02:18,280 --> 00:02:21,370 Let's put this into practice with a real life problem. 37 00:02:21,940 --> 00:02:26,350 Now let's say that you've gotten a job at a theme park and your first job of the 38 00:02:26,350 --> 00:02:31,330 day is to write some code that replaces the ticket box. 39 00:02:31,990 --> 00:02:35,800 Now there's a couple of things that you'll need to think about. Firstly, 40 00:02:35,980 --> 00:02:40,060 in order for somebody to actually purchase a ticket to go on the rollercoaster 41 00:02:40,060 --> 00:02:44,050 ride, they will need to be over 120 centimeters. 42 00:02:44,650 --> 00:02:49,060 So we have to check what their height is because if they're too short, 43 00:02:49,120 --> 00:02:53,740 then we won't be able to sell them a ticket anyways. In the course resources, 44 00:02:53,830 --> 00:02:58,830 I've included a link to this flow chart that I've created on draw.io. 45 00:02:59,890 --> 00:03:00,700 This is a really, 46 00:03:00,700 --> 00:03:04,960 really useful tool for creating any sort of flow charts or diagrams and it's 47 00:03:04,960 --> 00:03:09,550 really easy to use. Now, if we take a look at this flow chart, 48 00:03:09,820 --> 00:03:14,260 this is basically the logic that we have to program using our, 49 00:03:14,350 --> 00:03:15,850 if and else statements. 50 00:03:16,360 --> 00:03:21,360 If the person who's trying to purchase a ticket is not over 120 centimeters, 51 00:03:23,020 --> 00:03:26,020 then they can't ride on the rollercoaster. 52 00:03:26,530 --> 00:03:30,670 But if their height is greater than 120 centimeters, then they can ride. 53 00:03:30,850 --> 00:03:34,870 So let's try it out. If you head over to the starting Repl.it, 54 00:03:35,560 --> 00:03:39,310 you'll find some starting code in there and you can go ahead and fork this 55 00:03:39,310 --> 00:03:40,143 Repl.it. 56 00:03:40,780 --> 00:03:44,470 All that I've got here is a print statement that says "Welcome to the 57 00:03:44,470 --> 00:03:49,470 rollercoaster" as well as an input asking the user for their height in 58 00:03:49,930 --> 00:03:54,930 centimeters and then converting the string into a whole number, an integer, and 59 00:03:56,020 --> 00:03:58,780 then I'm storing it inside this variable called height. 60 00:03:59,440 --> 00:04:04,330 Now we're going to check whether if the height that the user has typed in is 61 00:04:04,330 --> 00:04:09,330 greater than 120. So we use the keyword if and then we check if the height is 62 00:04:10,960 --> 00:04:15,960 greater than 120 and then we add the colon and now when I hit enter, 63 00:04:17,410 --> 00:04:22,410 you'll notice that the code editor has automatically indented me slightly over. 64 00:04:23,050 --> 00:04:28,050 I'm not over here because in Python the spacing and indentation is really, 65 00:04:29,560 --> 00:04:30,400 really important. 66 00:04:30,790 --> 00:04:35,080 It tells the computer that the code that I'm about to write is what should be 67 00:04:35,080 --> 00:04:38,620 executed when this condition is met. 68 00:04:39,340 --> 00:04:44,340 So what should happen? If your height is over 120 then we will just print 69 00:04:44,980 --> 00:04:46,720 You can ride the rollercoaster!. 70 00:04:47,470 --> 00:04:52,470 But what should we print if height is not greater than 120? Well, 71 00:04:52,930 --> 00:04:57,930 in this case, we would use the else statement to catch when that happens. 72 00:04:58,840 --> 00:05:03,840 And it's really important that you don't write the else here because this is 73 00:05:04,120 --> 00:05:06,820 indented over. Instead, 74 00:05:07,030 --> 00:05:11,470 you want it to be at the same indentation level as the if statement. 75 00:05:11,950 --> 00:05:15,610 These two are essentially a pair, if and else. Now, 76 00:05:15,700 --> 00:05:16,990 after the else keyword, 77 00:05:16,990 --> 00:05:21,820 we again add a colon and then we hit enter and we're now indented again. 78 00:05:22,180 --> 00:05:27,180 And here we can write the code that should happen if this condition is false. 79 00:05:28,930 --> 00:05:30,910 "Sorry, you have to grow taller before you can ride." 80 00:05:31,900 --> 00:05:36,900 So the really important things here are the condition which we're testing for, is 81 00:05:38,290 --> 00:05:43,290 the value of height greater than 120, the syntax of this code 82 00:05:44,470 --> 00:05:45,520 so the keywords 83 00:05:45,550 --> 00:05:50,550 if and else as well as the colons that come after each of these lines. 84 00:05:51,850 --> 00:05:54,310 And finally also the indentation. 85 00:05:54,880 --> 00:05:57,740 Everything that is indented after the 86 00:05:57,950 --> 00:06:00,440 if is a block of code. 87 00:06:01,070 --> 00:06:06,070 So this is indented and it's effectively inside this 88 00:06:07,640 --> 00:06:08,840 if. So 89 00:06:08,840 --> 00:06:12,740 this line of code lives inside the else statement. 90 00:06:13,160 --> 00:06:18,160 This block of code lives inside the if statement. And if you mess up on the 91 00:06:19,340 --> 00:06:24,140 indentation then you're probably going to get an indentation error telling you 92 00:06:24,140 --> 00:06:28,250 that this line 5 probably should be indented. 93 00:06:28,910 --> 00:06:32,180 But when we do actually correct it and we hit run, 94 00:06:32,780 --> 00:06:36,200 let's say that our height is 130 centimeters, 95 00:06:36,680 --> 00:06:39,410 then we get back, you can ride the rollercoaster. 96 00:06:40,010 --> 00:06:43,520 But if our height was say 90 centimeters, 97 00:06:43,790 --> 00:06:47,180 then we get a different outcome. We get, sorry, 98 00:06:47,180 --> 00:06:50,510 you have to grow taller before you can ride. So by using, 99 00:06:50,600 --> 00:06:55,310 if and else statements we're able to get our code to do different things, 100 00:06:55,580 --> 00:07:00,580 either printing this line or printing this line depending on a condition that 101 00:07:01,130 --> 00:07:05,660 we're testing for. Now, when we use this greater than sign, 102 00:07:05,960 --> 00:07:10,960 effectively what we're saying is that is the height greater than 120 which means 103 00:07:13,160 --> 00:07:17,030 that it does not include 120. In fact, 104 00:07:17,030 --> 00:07:22,030 if I run this code and if I type in my height as 120 then actually goes into the 105 00:07:23,420 --> 00:07:25,760 else block and prints this. 106 00:07:26,480 --> 00:07:31,480 So if we wanted to include 120 centimeters so that all the people who are 107 00:07:31,730 --> 00:07:35,810 exactly 120 centimeters can ride the rollercoaster, 108 00:07:36,230 --> 00:07:38,840 then instead of just using a greater than symbol, 109 00:07:38,960 --> 00:07:41,330 we have to write greater than or equals. 110 00:07:41,780 --> 00:07:44,570 So these two symbols have to be next to each other. 111 00:07:45,470 --> 00:07:50,470 And now when I run my code and I write 120, you'll see that it's now falling into 112 00:07:51,980 --> 00:07:56,630 this block of code and it's telling me that I can ride the roller coaster. Now, 113 00:07:56,630 --> 00:08:01,630 these are called comparison operators and we've already seen greater than, so 114 00:08:01,730 --> 00:08:04,160 lesser than is pretty self explanatory. 115 00:08:04,430 --> 00:08:09,140 But we've also seen greater than or equal to and lesser than or equal to. 116 00:08:09,470 --> 00:08:13,580 So if you want to include a particular number, a particular value, 117 00:08:13,910 --> 00:08:17,270 when you do these comparisons, you would be using these instead. 118 00:08:17,990 --> 00:08:22,580 Now in this table, there's also the equal to and not equal to. 119 00:08:23,450 --> 00:08:24,050 For example, 120 00:08:24,050 --> 00:08:29,050 if you wanted to check if somebody's height is equal to precisely 120 then you 121 00:08:30,860 --> 00:08:35,860 would use two equal signs. And very often it gets a little bit confusing, 122 00:08:36,350 --> 00:08:40,370 especially if you're new to programming when you're typing equal signs, 123 00:08:40,430 --> 00:08:44,060 because sometimes we're typing one and other times we're typing two. 124 00:08:44,630 --> 00:08:47,420 It's important to remember that when you have one equal sign, 125 00:08:47,510 --> 00:08:52,400 it means that you're assigning this value to this variable. 126 00:08:53,000 --> 00:08:54,950 But when you have two equal signs, 127 00:08:55,050 --> 00:08:59,640 you are checking to see if the value on the left is equal to the value on the 128 00:08:59,640 --> 00:09:02,790 right and they're completely different. Now, 129 00:09:02,790 --> 00:09:04,650 the good thing is that when you get it wrong, 130 00:09:04,680 --> 00:09:09,680 usually you'll get enough clues in the error to actually hint to you, 131 00:09:09,750 --> 00:09:13,380 Hey, maybe there's something that's wrong here, right? 132 00:09:13,500 --> 00:09:17,220 Cause the syntax doesn't look right. For this to be a condition, 133 00:09:17,250 --> 00:09:20,760 it has to be something that evaluates to true or to false. 134 00:09:21,180 --> 00:09:24,600 And height =120 does not evaluate to either. 135 00:09:24,990 --> 00:09:26,520 So by changing it to this, 136 00:09:26,580 --> 00:09:31,580 we're saying if height is equal to 120 then we'll execute this line of code 137 00:09:32,250 --> 00:09:35,100 or any other lines which are in the same block of code. 138 00:09:35,640 --> 00:09:40,640 But if the height is not equal to 120 then we're going to execute this block of 139 00:09:41,010 --> 00:09:45,000 code. So if I write 120 then that works. 140 00:09:45,330 --> 00:09:50,100 But if I write 121 then it does not work. 141 00:09:51,000 --> 00:09:55,770 Similarly, you can also check for not equals to, which just flips it around. 142 00:09:56,220 --> 00:10:01,220 But in our case, it makes sense to say if the height is greater than or equals 143 00:10:01,350 --> 00:10:05,700 120 then you can ride the rollercoaster. 144 00:10:06,120 --> 00:10:11,120 But otherwise you cannot ride the rollercoaster. By using, 145 00:10:11,220 --> 00:10:12,570 if and else statements, 146 00:10:12,780 --> 00:10:17,780 we can get our computer to do different things and respond differently depending 147 00:10:18,060 --> 00:10:21,330 on different conditions. So in the next lesson, 148 00:10:21,390 --> 00:10:24,420 I've got a coding exercise for you. I'll see you there. 14766

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.