Afrikaans
Akan
Albanian
Amharic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
1
Here's a question. Have you ever sat in your bath and wondered,
2
why is it that no matter how forgetful I am,
3
my bath never overflows like this?
4
This is something I was thinking about the other day and I realized that it's
5
not because I'm particularly diligent. I forget stuff all the time. I mean,
6
pizza, anyone? This is what my pizzas usually look like. Um,
7
but the reason why a bathtub or the sink doesn't overflow is because of this
8
fantastic piece of engineering, the overflow.
9
So this means that whenever the water reaches beyond a certain level about here,
10
the water starts overflowing
11
so the bathtub doesn't overflow and annoy your neighbors downstairs.
12
In fact, we could represent this mechanism with a conditional statement.
13
When the water level is say greater than 80 centimeters,
14
then it should drain the water.
15
But if the water level is not greater than 80 centimeters,
16
it should continue filling up the tub.
17
This type of conditional statement is known as an if/else statement.
18
Depending on a particular condition,
19
we would do either A or B.
20
And when we want to write Python code to represent this,
21
it looks something like this.
22
There's the keyword if, and then the condition that we're testing for and then a
23
colon and after the colon we've got an indented block of code which should be
24
executed if this condition is met, if it's true.
25
But if it's not true,
26
then we will skip to the else block and it's just the else keyword with a colon
27
and then this code block would execute if the condition is false.
28
So we could represent that previous bathtub situation with code that looks a bit
29
like this.
30
Let's say our water level is at 50 centimeters and then we would test if the
31
water level is greater than 80 centimeters. Well, if that is the case,
32
then we should drain the water.
33
But if it's not greater than 80 centimeters,
34
in other words else, well in this case,
35
we should just continue filling up the bathtub.
36
Let's put this into practice with a real life problem.
37
Now let's say that you've gotten a job at a theme park and your first job of the
38
day is to write some code that replaces the ticket box.
39
Now there's a couple of things that you'll need to think about. Firstly,
40
in order for somebody to actually purchase a ticket to go on the rollercoaster
41
ride, they will need to be over 120 centimeters.
42
So we have to check what their height is because if they're too short,
43
then we won't be able to sell them a ticket anyways. In the course resources,
44
I've included a link to this flow chart that I've created on draw.io.
45
This is a really,
46
really useful tool for creating any sort of flow charts or diagrams and it's
47
really easy to use. Now, if we take a look at this flow chart,
48
this is basically the logic that we have to program using our,
49
if and else statements.
50
If the person who's trying to purchase a ticket is not over 120 centimeters,
51
then they can't ride on the rollercoaster.
52
But if their height is greater than 120 centimeters, then they can ride.
53
So let's try it out. If you head over to the starting Repl.it,
54
you'll find some starting code in there and you can go ahead and fork this
55
Repl.it.
56
All that I've got here is a print statement that says "Welcome to the
57
rollercoaster" as well as an input asking the user for their height in
58
centimeters and then converting the string into a whole number, an integer, and
59
then I'm storing it inside this variable called height.
60
Now we're going to check whether if the height that the user has typed in is
61
greater than 120. So we use the keyword if and then we check if the height is
62
greater than 120 and then we add the colon and now when I hit enter,
63
you'll notice that the code editor has automatically indented me slightly over.
64
I'm not over here because in Python the spacing and indentation is really,
65
really important.
66
It tells the computer that the code that I'm about to write is what should be
67
executed when this condition is met.
68
So what should happen? If your height is over 120 then we will just print
69
You can ride the rollercoaster!.
70
But what should we print if height is not greater than 120? Well,
71
in this case, we would use the else statement to catch when that happens.
72
And it's really important that you don't write the else here because this is
73
indented over. Instead,
74
you want it to be at the same indentation level as the if statement.
75
These two are essentially a pair, if and else. Now,
76
after the else keyword,
77
we again add a colon and then we hit enter and we're now indented again.
78
And here we can write the code that should happen if this condition is false.
79
"Sorry, you have to grow taller before you can ride."
80
So the really important things here are the condition which we're testing for, is
81
the value of height greater than 120, the syntax of this code
82
so the keywords
83
if and else as well as the colons that come after each of these lines.
84
And finally also the indentation.
85
Everything that is indented after the
86
if is a block of code.
87
So this is indented and it's effectively inside this
88
if. So
89
this line of code lives inside the else statement.
90
This block of code lives inside the if statement. And if you mess up on the
91
indentation then you're probably going to get an indentation error telling you
92
that this line 5 probably should be indented.
93
But when we do actually correct it and we hit run,
94
let's say that our height is 130 centimeters,
95
then we get back, you can ride the rollercoaster.
96
But if our height was say 90 centimeters,
97
then we get a different outcome. We get, sorry,
98
you have to grow taller before you can ride. So by using,
99
if and else statements we're able to get our code to do different things,
100
either printing this line or printing this line depending on a condition that
101
we're testing for. Now, when we use this greater than sign,
102
effectively what we're saying is that is the height greater than 120 which means
103
that it does not include 120. In fact,
104
if I run this code and if I type in my height as 120 then actually goes into the
105
else block and prints this.
106
So if we wanted to include 120 centimeters so that all the people who are
107
exactly 120 centimeters can ride the rollercoaster,
108
then instead of just using a greater than symbol,
109
we have to write greater than or equals.
110
So these two symbols have to be next to each other.
111
And now when I run my code and I write 120, you'll see that it's now falling into
112
this block of code and it's telling me that I can ride the roller coaster. Now,
113
these are called comparison operators and we've already seen greater than, so
114
lesser than is pretty self explanatory.
115
But we've also seen greater than or equal to and lesser than or equal to.
116
So if you want to include a particular number, a particular value,
117
when you do these comparisons, you would be using these instead.
118
Now in this table, there's also the equal to and not equal to.
119
For example,
120
if you wanted to check if somebody's height is equal to precisely 120 then you
121
would use two equal signs. And very often it gets a little bit confusing,
122
especially if you're new to programming when you're typing equal signs,
123
because sometimes we're typing one and other times we're typing two.
124
It's important to remember that when you have one equal sign,
125
it means that you're assigning this value to this variable.
126
But when you have two equal signs,
127
you are checking to see if the value on the left is equal to the value on the
128
right and they're completely different. Now,
129
the good thing is that when you get it wrong,
130
usually you'll get enough clues in the error to actually hint to you,
131
Hey, maybe there's something that's wrong here, right?
132
Cause the syntax doesn't look right. For this to be a condition,
133
it has to be something that evaluates to true or to false.
134
And height =120 does not evaluate to either.
135
So by changing it to this,
136
we're saying if height is equal to 120 then we'll execute this line of code
137
or any other lines which are in the same block of code.
138
But if the height is not equal to 120 then we're going to execute this block of
139
code. So if I write 120 then that works.
140
But if I write 121 then it does not work.
141
Similarly, you can also check for not equals to, which just flips it around.
142
But in our case, it makes sense to say if the height is greater than or equals
143
120 then you can ride the rollercoaster.
144
But otherwise you cannot ride the rollercoaster. By using,
145
if and else statements,
146
we can get our computer to do different things and respond differently depending
147
on different conditions. So in the next lesson,
148
I've got a coding exercise for you. I'll see you there.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.