Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
- Python Data Types and Math Operations
- Numbers 4 questions
- Booleans 7:51
- Logic 4 questions
- If, Else and Elif 11:12
- Use an if 1 objective
- Functions 11:37
- Reviewing Functions 3 questions
- Expecting Exceptions 7:15
- Exception Flow 2 questions
- While Loops 9:32
- Manners 2 questions
- The Random Library
- The Number Guessing Game
Preview
Video Player
00:00
00:00
00:00
- 2x 2x
- 1.75x 1.75x
- 1.5x 1.5x
- 1.25x 1.25x
- 1.1x 1.1x
- 1x 1x
- 0.75x 0.75x
- 0.5x 0.5x
Let's explore conditional branching
Learn more
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Now that we can do some Boolean reasoning,
0:00
we should look at how to use it
to our advantage in scripts.
0:02
We make choices all the time.
0:06
If it's raining outside,
I'm gonna get an umbrella.
0:07
If I'm hungry, I'm gonna eat some food.
0:10
If it's late and I'm tired, I go to bed.
0:12
Well, unless I'm stuck on a coding
problem that I just have to fix.
0:15
I should always go to bed at that point.
0:18
The answer The answer
usually comes in my sleep.
0:19
All those scenarios start
with the word if, and
0:22
that is the first way that we are gonna
control the flow of our application.
0:24
If you want to learn more,
keep watching this video.
0:29
In our script here, we ask for
a name, we say hello,
0:33
and then we say the person
is learning Python.
0:36
That's not always necessarily true, is it?
0:40
So let's do this to fix it.
0:43
If the name inputted is actually your
name, then we'll print that message.
0:45
So in order to do that,
we're going to need to check and
0:50
see if two values are actually equal.
0:54
You can compare two objects
using the double equal sign.
0:56
Let's take a look real quick in the show.
1:00
So here's an example,
we'll say fruit equals apples.
1:04
And so we've assigned apples to
fruit with a single equal sign,
1:09
and then we'll check equality
by using the double equal signs.
1:13
We'll say, fruit does that equal apples.
1:17
True, and
then if we compare apples to oranges,
1:24
which you're not supposed to do,
you get back false.
1:28
And that's because fruit is apples,
and apples do not equal oranges.
1:33
So note how the single equal
sign is used for assignment and
1:38
the double equals sign is used for
comparison.
1:42
Accidental assigning by using just
a single equals is a super common problem,
1:46
and that's probably because when we
say fruit equals oranges in English,
1:52
not fruit equals equals oranges.
1:57
So when comparing, make sure that
you double-check for double equals.
2:00
So in our code, we want to only show
the learning Python bit if it's us.
2:05
So I'm gonna add a new line here and
start it with a new keyword
2:10
that you haven't seen yet,
and it's It's called if.
2:15
So if, and
then what follows next is an expression.
2:20
So let's see, we have first name already.
2:25
So if first name is equal to,
note the double equals there,
2:27
Craig, double check
the double equals always.
2:34
And then we add a colon,
which is a new character for us.
2:40
What happens is the colon opens
up the body of our if statement.
2:44
Now the body is what will run
if this expression is true.
2:49
The way that you group the code together
that will run when the expression is
2:54
true is by indenting each line.
2:58
Now you can indent your code with a tab or
a series of spaces.
3:01
The only requirement is
that all indentation in
3:05
your file is exactly the same.
3:08
As you can imagine,
that might cause some problems.
3:10
So in order to standardize
that decision for you,
3:14
there is a very strong movement to use
four spaces to indent your Python code.
3:17
So as you can see down here in the editor,
3:22
this says we're gonna use
spaces instead of tabs.
3:24
So we're gonna use spaces.
3:27
And then this two here is saying
the number of spaces, and
3:29
I just said that we wanna use four, so
I'm gonna change the default to be four.
3:33
Four spaces is the default in
most text editors for Python,
3:40
and now we've just changed ours.
3:44
More in the teacher's notes.
3:46
So you'll notice that when I press Enter
after this if statement, you'll see
3:48
that the cursor is automatically
indented to where the code should go.
3:53
Here you'll see that we are on line four,
which is true, line four, and
3:57
we're in column five,
which is four spaces, right?
4:01
There's four blank spaces,
and then we start writing.
4:04
Awesome, but actually what we really
want is this line here, right?
4:07
We want this line here up there.
4:13
So what I'm gonna do is I'm gonna
move to the front of this and
4:15
I'm gonna press backspace,
4:18
and you'll see that it automatically
indented into our if block there.
4:19
You can finish this grouping or
block of code, as we call it, block.
4:23
You can end that by going back to
the original indentation level.
4:28
So if we come to the end here and
we press Enter, it's waiting for
4:32
us there, I can press backspace back here.
4:36
So let's just add a new closing line so
that we can see it.
4:39
So we'll say print,
we'll say have a great day.
4:42
Let's use our format string.
4:47
We'll use the name there one more time.
4:49
And we'll say format,
we'll pass our first name.
4:50
Do you see how the indentation
should stop right here?.
4:55
It shows that this is clearly
not part of the if save it.
4:59
So let's run it and see if it's working.
5:03
I'm gonna drop out of here.
5:04
I'm gonna make sure that this is saved.
5:06
There's no red markup there.
5:08
We'll save clear here.
5:09
Let's go ahead and say python hello.py.
5:11
We've got an error.
5:17
Let's see what's happening.
5:18
Did you spot it?
5:19
Unexpected end of file while parsing.
5:22
So here we go.
5:24
Look what I forgot.
5:26
I forgot the final peren there.
5:27
So there we go.
5:30
So it was just waiting for us to finish.
5:30
That's awesome,
that's exactly the error I showed.
5:32
See, it happens.
5:34
Here we go.
5:36
What is your first name?
5:37
My name is Craig.
5:40
And so there we see Craig is learning
Python because it went into the if block
5:42
and checked it.
5:46
And now let's test the other side of it.
5:48
Let's run it again without the name
Craig or without your name.
5:50
So how about my buddy Kenneth?
5:54
He's a great teacher and Pythonista and
5:57
you'll meet him in a future course,
I'm sure.
5:58
Hello, Kenneth, have a great day Kenneth.
6:01
So there we go.
6:04
We don't have the output.
6:04
Kenneth is learning Python because
our if condition here, right?
6:06
This condition first name equals Craig,
Kenneth does not equal Craig.
6:09
So that was false.
6:13
You know what though, I'm kind of feeling
like everybody should probably start
6:15
learning Python, I mean, don't you?
6:18
So how about everybody that isn't you
gets a message about learning Python, and
6:20
this way you can run it for your family or
friends and give them a little motivation,
6:25
a little wink, wink, nudge, nudge.
6:30
So you can very easily do this.
6:33
And the keyword is else.
6:35
So at the same indentation level as
your if, you use the keyword else and
6:37
then a colon,
because elses also have bodies.
6:43
So the next body of code and
see how it's indented automatically there.
6:47
We'll say print,
you should totally learn Python,
6:52
placeholder, exclamation point.
6:58
Go ahead and close that string.
7:03
We'll format, push in our first name, and
7:05
then this time I'm going to remember
to close my print statement.
7:08
There we go.
7:12
So what happens is, we come in here,
we check the first name.
7:13
If this is true, this runs,
otherwise, this block runs.
7:17
Do you see how the indentation really
just makes the code kind of jut out?
7:24
It's kind of like branches of a tree.
7:28
This is why it's called branching.
7:30
Now this spacing that you see is
also referred to as white space.
7:32
One thing to do before we
verify that this is working is
7:37
to talk about our final branching keyword.
7:40
It's one of the only Python keywords
that isn't actually a word in English.
7:42
It's called elif, and it's
a combination of the words else and if.
7:47
And what this allows you to do
is chain different conditions.
7:52
So let's do this.
7:55
Now, I told you before that
you're not alone in your journey.
7:57
Your fellow students are taking
this course right now.
8:00
So let's go to the community and
get some names.
8:02
So here I am on the treehouse site,
8:05
it's most likely gonna look different
because we're constantly improving things.
8:07
But up here in the header,
there is a link to the community link and
8:10
this by default is all of the topic.
8:16
But we can narrow that, so
let's narrow that down here to Python.
8:19
Let's take a look at this first one here.
8:26
Is there a reason we are not setting
the condition in the while statement?
8:27
We're not quite there yet, but
we're going to see that here in a second.
8:30
So let's go here.
8:33
Let's take a look at this.
8:35
And this is max a Maximiliane.
8:36
Let's grab her name here.
8:38
Let's say Maximiliane Quel, if I can.
8:41
Maximiliane, so I'm gonna copy that,
and you'll see here, she's gone and
8:45
she's asked, and she's put some some
questions on here, she got an answer back,
8:48
and there's an answer
there from Stephen Parker.
8:52
And she was still a good job on on
what it was that she was asking.
8:55
We'll get to this here just in a bit.
8:58
So I'm gonna pop back over and we're gonna
use Maximiliane in our example here.
9:01
So if it's me we wanna see
this is learning Python.
9:06
But if it's specifically Maximiliane
because we know she's in there, we'll say
9:11
if first name equals and I hope I'm saying
your name right, Maximilliane, maybe.
9:17
Then we'll come in here and
9:24
we'll say print first name is learning
9:28
with fellow students community.
9:33
You know what, me too.
9:38
And there we go.
9:42
We added another branch.
9:43
So let's go ahead and let's run it for
Maximiliane, that's gonna be,
9:45
First name is M-A-X-I-M-I, Maximiliane.
9:52
Maximiliane is learning with fellow
students in In the community, me too.
9:58
Cool, that's awesome that
you're gonna hang out there.
10:02
Helping others is a wonderful way to
make sure your learning sticks, I'm so
10:05
proud of you.
10:07
So looking at the code,
we see that it checks this If statement.
10:09
Is Maximilian equal to Craig,
that is not true.
10:14
Else if, Maximilian is equal
to Maximilian, print this.
10:17
And because that happened,
we're not gonna hit this otherwise block.
10:20
So, let's run one that does
hit the otherwise block.
10:23
Let's do that real quick.
10:26
We'll say python, and let's use one
of our JavaScript teachers here,
10:26
treasure And there it says,
hello, Treasure.
10:31
You should totally learn Python, Treasure.
10:37
I think she is learning Python.
10:39
Actually, she's kind of a polyglot.
10:41
She talks a whole bunch of these
different programming languages.
10:42
So that equality comparison operator,
the double equals,
10:46
is just one of many ways that
we can compare our objects.
10:50
So if you would like to learn more
about other comparison operators,
10:54
then watch the upcoming video.
10:59
Else if you are feeling like you
wanna check out the community forum,
11:01
then you totally should.
11:05
Else, you should take a break and
then come back refreshed and
11:07
ready to compare things.
11:10
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up