All language subtitles for 1. If Elif and Else Statements in Python

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
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
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

Welcome back everyone in this lecture.

We're going to discuss if.

Elif.

Else.

Statements.

So as I mentioned in this lecture we're going to discuss control flow in general and control flow basically

allows us to use logic to execute code only when we want to.

So often you have a larger piece of code and you only want certain code to be executed when a particular

condition has been met.

For example let's imagine that I'm trying to program a robot to feed my dogs that I could say if my

dog is hungry.

So that's the condition my dog being hungry then I'll have the robot feed the dog.

They'll have the actual code execute or perform some action.

So in order to control this flow of logic we use some keywords and the keywords we're going to be introducing

in this lecture are if.

Elif.

Else.

So let's see the syntax for these three keywords in order to understand the syntax.

We have to understand that control flow syntax in Python makes use of colons and indentation otherwise

known as whitespace.

And this indentation system is absolutely crucial to Python.

And it's really what sets it apart from other programming language.

This use of whitespace and in the notation allows Python code to be easily readable and very quick to

prototype.

So here's the syntax of a basic IF statement we're going to say.

If so that's a keyword some condition so some condition is usually some sort of comparison operation

that we just saw in the previous section of the course.

So that could be something like If hungry is equal to true colon and then notice that that blue line

is indented further than the if statement.

So that says anything along that indentation is going to be executed if that condition happens to be

true.

Now on top of an IF statement we can add an else to this.

So let's say that condition doesn't happen to be true.

We can have another block of code execute.

So in this logic we say if some condition happens to be true we execute some code else meaning that

condition that it has to be true.

We do something else.

And notice how the else doesn't have a condition attached to it.

It only actually executes if the condition is above that it happens to be true.

You should also notice that the if else in the notation lies are lined up with each other.

If you want to check for multiple conditions before that else statement executes you can have an elf

statement or E L I F statement and basically you say if some condition that executes some code Elif

some other condition do something different and you can have as many of these LFA as you want.

And then finally all the way at the end you can have an else statement do something else OK.

Let's explore all these concepts by actually coding out some examples in a Jupiter notebook to begin

all of this.

We're going to start with the simplest example we can do just a single line of an IF statement with

just a boolean condition we're saying if true colon and note what happens when I hit enter.

I have this indentation automatically done for me.

And if using any text editor and you have defined the file as a PI script already you should see this

in the notation occur automatically for you as well.

Then we're going to say Prince it's true.

So then we're going to run this and we see if true print it's true.

So notice we're saying if some condition is true print it's true.

Now typically you won't have just a boolean like this.

Otherwise you'll always be printing that instead what you may have is something like a comparison operation.

So we'll say if three is greater than to print it's true.

We run that and we get back it's true.

And then to make this even more realistic we'll say hungry set a variable there will say Hungry is equal

to true and then I'll say if hungry Prince feed me.

And if you run that we see that we get feed me.

We can also then get hungry to false.

Now if I run this code again notice I don't get back anything else.

So I have some condition and it happened to be false meaning this block of code that execute what I

could do is add in an else statement to execute.

If this condition doesn't happen to be true.

So he hit enter again and then we hit backspace in order to lineup up or else block with the if statement.

So if we want this else to be lined up with this if they need to be at the same indentation in our code

in a lot of times when you're working if any text editor will kind of automatically line things up for

you.

So keep that in mind.

So else does it have any other conditions attached to it.

Because we're only going to execute.

Else if none of the conditions above happened to be true.

So right now we're saying if you're hungry print's feed me otherwise.

Prince I'm not hungry.

I'll run this right now because Hungary was equal to false.

We're getting back.

I'm not hungry.

If we change hungry to be true we get back.

Feed me.

What's also important to notice is that I'm just passing right here hungry by itself as a bullion.

I don't actually need to do something like this.

Check that Hungary is equal to true because hungry by itself is already a boolean.

And we'll explore that example later on in more detail.

So if hungry print's feed me else Prince I'm not hungry.

OK.

Now let's discuss multiple branches using if Ellefson else.

So let's look at another example.

I'm going to say EHLO see which stands for location.

And I'm going to set that equal to bank.

So have a location.

It's equal to bank and I'm going to say if my location is equal to an auto shop I will for it.

Cars are cool.

Else I'll Prince.

I do not know much.

So when I run this it says I do not know much because the location was bank and we have location equal

to our shop print cards.

Cool.

So that's an excuse.

So then we have else printing.

I don't know much what we can do is check for other conditions using elif.

So let's pass in another condition here we'll say if the location is equal to the bank.

Then Prince money is cool.

As I'm sure everyone at the bank says and then we run this when we get back.

Money is cool.

So here we can stack on as many conditions using an if statement so we can add and more lives for more

conditions.

We can say Alosi is equal to store Colin Prince Welcome to the store we run that and we still get back.

Money is cool.

But as soon as we start changes condition let's change it to auto shop.

We run that.

We get back.

Cars are cool.

If we change it to store we get back.

Welcome to the store.

And if we change it to something else that's not in any of these conditions then we'll have the else

block could x.

So let's see what that looks like.

Let's say we're going to some game and it says I do not know much.

Perfect.

All right.

Just to drive this point home of indentation and white.

We're going to do one last very simple example.

Pretty much exactly the same as the last one.

Let's define a name we'll say Sammy and I will say if this person's name is equal to let's say Frankie

will print out Hello Frankie then we'll say well if we have some other name if elif name is equal to

Sammy print out Hello Sammy and untypically for your L's condition it's going to be something where

none of the other conditions were met.

So good thing to do here would be ask the person what their really.

What is your name.

And later on in another lecture we'll actually learn how to get input from the user.

But for now let's focus on a couple of things here.

Note the indentation and know how.

If Elif and else are all lined up for each other and their respective blocks are all indented and then

we also have this colon at the end of these conditions.

So now when I read this we should expect to see Hello Sammy and if we change this to Franki and run

it again I get back Villefranche if it changes Tony and it's not there because they like it back.

What is your name.

All right that's the basics.

If elephant.

Else they seem pretty simple and hopefully they're pretty straight forward to you.

Later on we're going to use them to create really nice large pieces of code that can execute more complex

tasks.

We'll see you at the next lecture.

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